From 4b4fb038e3e8c3914448ac0a40d9a85f2e29235b Mon Sep 17 00:00:00 2001 From: pavoni Date: Sun, 10 Jan 2016 12:30:47 +0000 Subject: [PATCH] Update for new library, slightly revise switch logic. --- homeassistant/components/light/vera.py | 7 +++--- homeassistant/components/sensor/vera.py | 6 ++--- homeassistant/components/switch/vera.py | 30 +++++++++++++++---------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/light/vera.py b/homeassistant/components/light/vera.py index 76718e71eb4..59abbe6b29d 100644 --- a/homeassistant/components/light/vera.py +++ b/homeassistant/components/light/vera.py @@ -14,7 +14,7 @@ from homeassistant.components.switch.vera import VeraSwitch from homeassistant.components.light import ATTR_BRIGHTNESS -from homeassistant.const import EVENT_HOMEASSISTANT_STOP +from homeassistant.const import EVENT_HOMEASSISTANT_STOP, STATE_ON REQUIREMENTS = ['pyvera==0.2.3'] @@ -59,7 +59,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): lights = [] for device in devices: - extra_data = device_data.get(device.deviceId, {}) + extra_data = device_data.get(device.device_id, {}) exclude = extra_data.get('exclude', False) if exclude is not True: @@ -86,4 +86,5 @@ class VeraLight(VeraSwitch): else: self.vera_device.switch_on() - self.is_on_status = True + self._state = STATE_ON + self.update_ha_state() diff --git a/homeassistant/components/sensor/vera.py b/homeassistant/components/sensor/vera.py index 6ef5d469c60..b381974ab31 100644 --- a/homeassistant/components/sensor/vera.py +++ b/homeassistant/components/sensor/vera.py @@ -56,7 +56,7 @@ def get_devices(hass, config): vera_sensors = [] for device in devices: - extra_data = device_data.get(device.deviceId, {}) + extra_data = device_data.get(device.device_id, {}) exclude = extra_data.get('exclude', False) if exclude is not True: @@ -89,12 +89,10 @@ class VeraSensor(Entity): 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) def __str__(self): - return "%s %s %s" % (self.name, self.vera_device.deviceId, self.state) + return "%s %s %s" % (self.name, self.vera_device.device_id, self.state) @property def state(self): diff --git a/homeassistant/components/switch/vera.py b/homeassistant/components/switch/vera.py index 0acc33bea4e..3f5d2a08cb8 100644 --- a/homeassistant/components/switch/vera.py +++ b/homeassistant/components/switch/vera.py @@ -12,12 +12,16 @@ from requests.exceptions import RequestException import homeassistant.util.dt as dt_util from homeassistant.helpers.entity import ToggleEntity +from homeassistant.components.switch import SwitchDevice + from homeassistant.const import ( ATTR_BATTERY_LEVEL, ATTR_TRIPPED, ATTR_ARMED, ATTR_LAST_TRIP_TIME, - EVENT_HOMEASSISTANT_STOP) + EVENT_HOMEASSISTANT_STOP, + STATE_ON, + STATE_OFF) REQUIREMENTS = ['pyvera==0.2.3'] @@ -60,7 +64,7 @@ def get_devices(hass, config): vera_switches = [] for device in devices: - extra_data = device_data.get(device.deviceId, {}) + extra_data = device_data.get(device.device_id, {}) exclude = extra_data.get('exclude', False) if exclude is not True: @@ -75,7 +79,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): add_devices(get_devices(hass, config)) -class VeraSwitch(ToggleEntity): +class VeraSwitch(SwitchDevice): """ Represents a Vera Switch. """ def __init__(self, vera_device, controller, extra_data=None): @@ -86,15 +90,17 @@ class VeraSwitch(ToggleEntity): self._name = self.extra_data.get('name') else: self._name = self.vera_device.name - self.is_on_status = False + self._state = STATE_OFF self.controller.register(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) + if self.vera_device.is_switched_on(): + self._state = STATE_ON + else: + self._state = STATE_OFF + self.update_ha_state() @property def name(self): @@ -129,12 +135,14 @@ class VeraSwitch(ToggleEntity): def turn_on(self, **kwargs): self.vera_device.switch_on() - self.is_on_status = True + self._state = STATE_ON + self.update_ha_state() def turn_off(self, **kwargs): self.vera_device.switch_off() - self.is_on_status = False + self._state = STATE_OFF + self.update_ha_state() @property def should_poll(self): @@ -144,7 +152,5 @@ class VeraSwitch(ToggleEntity): @property def is_on(self): """ True if device is on. """ - return self.is_on_status + return self._state == STATE_ON - def update(self): - self.is_on_status = self.vera_device.is_switched_on()