Added support for event subscriptions

This commit is contained in:
pavoni 2015-12-30 19:44:02 +00:00
parent 586be7fad1
commit ae0dbbcfa5
2 changed files with 43 additions and 7 deletions

View file

@ -14,6 +14,8 @@ from homeassistant.components.switch.vera import VeraSwitch
from homeassistant.components.light import ATTR_BRIGHTNESS
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
REQUIREMENTS = ['https://github.com/pavoni/home-assistant-vera-api/archive/'
'efdba4e63d58a30bc9b36d9e01e69858af9130b8.zip'
'#python-vera==0.1.1']
@ -36,10 +38,19 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
device_data = config.get('device_data', {})
controller = veraApi.VeraController(base_url)
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 = controller.get_devices([
devices = vera_controller.get_devices([
'Switch',
'On/Off Switch',
'Dimmable Switch'])
@ -54,7 +65,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
exclude = extra_data.get('exclude', False)
if exclude is not True:
lights.append(VeraLight(device, extra_data))
lights.append(VeraLight(device, vera_controller, extra_data))
add_devices_callback(lights)

View file

@ -13,7 +13,11 @@ import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.const import (
ATTR_BATTERY_LEVEL, ATTR_TRIPPED, ATTR_ARMED, ATTR_LAST_TRIP_TIME)
ATTR_BATTERY_LEVEL,
ATTR_TRIPPED,
ATTR_ARMED,
ATTR_LAST_TRIP_TIME,
EVENT_HOMEASSISTANT_STOP)
REQUIREMENTS = ['https://github.com/pavoni/home-assistant-vera-api/archive/'
'efdba4e63d58a30bc9b36d9e01e69858af9130b8.zip'
@ -37,7 +41,16 @@ def get_devices(hass, config):
device_data = config.get('device_data', {})
vera_controller = veraApi.VeraController(base_url)
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([
@ -53,7 +66,8 @@ def get_devices(hass, config):
exclude = extra_data.get('exclude', False)
if exclude is not True:
vera_switches.append(VeraSwitch(device, extra_data))
vera_switches.append(
VeraSwitch(device, vera_controller, extra_data))
return vera_switches
@ -66,9 +80,10 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class VeraSwitch(ToggleEntity):
""" Represents a Vera Switch. """
def __init__(self, vera_device, extra_data=None):
def __init__(self, vera_device, controller, extra_data=None):
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:
@ -77,6 +92,16 @@ class VeraSwitch(ToggleEntity):
# for debouncing status check after command is sent
self.last_command_send = 0
self.controller.register(vera_device)
self.controller.on(
vera_device, self._update_callback)
def _update_callback(self, _device):
""" Called by the vera device callback to update state. """
_LOGGER.info(
'Subscription update for %s', self.name)
self.update_ha_state(True)
@property
def name(self):
""" Get the mame of the switch. """