Merge pull request #1550 from balloob/restructure_vera
Refactor vera into a platform.
This commit is contained in:
commit
fd27e4fc7d
11 files changed
+262
-253
No files matched your search
@@ -10,7 +10,7 @@ import csv
|
||||
|
||||
from homeassistant.components import (
|
||||
group, discovery, wemo, wink, isy994,
|
||||
zwave, insteon_hub, mysensors, tellstick)
|
||||
zwave, insteon_hub, mysensors, tellstick, vera)
|
||||
from homeassistant.config import load_yaml_config_file
|
||||
from homeassistant.const import (
|
||||
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE,
|
||||
@@ -66,6 +66,7 @@ DISCOVERY_PLATFORMS = {
|
||||
zwave.DISCOVER_LIGHTS: 'zwave',
|
||||
mysensors.DISCOVER_LIGHTS: 'mysensors',
|
||||
tellstick.DISCOVER_LIGHTS: 'tellstick',
|
||||
vera.DISCOVER_LIGHTS: 'vera',
|
||||
}
|
||||
|
||||
PROP_TO_ATTR = {
|
||||
|
||||
@@ -6,15 +6,15 @@ https://home-assistant.io/components/light.vera/
|
||||
"""
|
||||
import logging
|
||||
|
||||
from requests.exceptions import RequestException
|
||||
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.components.light import ATTR_BRIGHTNESS, Light
|
||||
from homeassistant.const import (
|
||||
ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED,
|
||||
EVENT_HOMEASSISTANT_STOP, STATE_OFF, STATE_ON)
|
||||
STATE_OFF, STATE_ON)
|
||||
from homeassistant.components.vera import (
|
||||
VeraDevice, VERA_DEVICES, VERA_CONTROLLER)
|
||||
|
||||
REQUIREMENTS = ['pyvera==0.2.8']
|
||||
DEPENDENCIES = ['vera']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -22,74 +22,17 @@ _LOGGER = logging.getLogger(__name__)
|
||||
# pylint: disable=unused-argument
|
||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
"""Setup Vera lights."""
|
||||
import pyvera as veraApi
|
||||
|
||||
base_url = config.get('vera_controller_url')
|
||||
if not base_url:
|
||||
_LOGGER.error(
|
||||
"The required parameter 'vera_controller_url'"
|
||||
" was not found in config"
|
||||
)
|
||||
return False
|
||||
|
||||
device_data = config.get('device_data', {})
|
||||
|
||||
vera_controller, created = veraApi.init_controller(base_url)
|
||||
|
||||
if created:
|
||||
def stop_subscription(event):
|
||||
"""Shutdown Vera subscriptions and subscription thread on exit."""
|
||||
_LOGGER.info("Shutting down subscriptions.")
|
||||
vera_controller.stop()
|
||||
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_subscription)
|
||||
|
||||
devices = []
|
||||
try:
|
||||
devices = vera_controller.get_devices([
|
||||
'Switch',
|
||||
'On/Off Switch',
|
||||
'Dimmable Switch'])
|
||||
except RequestException:
|
||||
# There was a network related error connecting to the vera controller.
|
||||
_LOGGER.exception("Error communicating with Vera API")
|
||||
return False
|
||||
|
||||
lights = []
|
||||
for device in devices:
|
||||
extra_data = device_data.get(device.device_id, {})
|
||||
exclude = extra_data.get('exclude', False)
|
||||
|
||||
if exclude is not True:
|
||||
lights.append(VeraLight(device, vera_controller, extra_data))
|
||||
|
||||
add_devices_callback(lights)
|
||||
add_devices_callback(
|
||||
VeraLight(device, VERA_CONTROLLER) for device in VERA_DEVICES['light'])
|
||||
|
||||
|
||||
class VeraLight(Light):
|
||||
class VeraLight(VeraDevice, Light):
|
||||
"""Representation of a Vera Light, including dimmable."""
|
||||
|
||||
def __init__(self, vera_device, controller, extra_data=None):
|
||||
def __init__(self, vera_device, controller):
|
||||
"""Initialize the light."""
|
||||
self.vera_device = vera_device
|
||||
self.extra_data = extra_data
|
||||
self.controller = controller
|
||||
if self.extra_data and self.extra_data.get('name'):
|
||||
self._name = self.extra_data.get('name')
|
||||
else:
|
||||
self._name = self.vera_device.name
|
||||
self._state = STATE_OFF
|
||||
|
||||
self.controller.register(vera_device, self._update_callback)
|
||||
self.update()
|
||||
|
||||
def _update_callback(self, _device):
|
||||
self.update_ha_state(True)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the light."""
|
||||
return self._name
|
||||
self._state = False
|
||||
VeraDevice.__init__(self, vera_device, controller)
|
||||
|
||||
@property
|
||||
def brightness(self):
|
||||
@@ -137,20 +80,13 @@ class VeraLight(Light):
|
||||
attr[ATTR_TRIPPED] = 'True' if tripped else 'False'
|
||||
|
||||
attr['Vera Device Id'] = self.vera_device.vera_device_id
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""No polling needed."""
|
||||
return False
|
||||
return attr
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if device is on."""
|
||||
return self._state == STATE_ON
|
||||
return self._state
|
||||
|
||||
def update(self):
|
||||
"""Called by the vera device callback to update state."""
|
||||
if self.vera_device.is_switched_on():
|
||||
self._state = STATE_ON
|
||||
else:
|
||||
self._state = STATE_OFF
|
||||
self._state = self.vera_device.is_switched_on()
|
||||
Reference in new issue
Block a user