From d61eb93c033434e31c29c73591a6bc96a72e48e7 Mon Sep 17 00:00:00 2001 From: pavoni Date: Sat, 9 Jan 2016 16:16:41 +0000 Subject: [PATCH 1/5] Remove throttle doesn't play well with subscriptions. --- homeassistant/components/light/vera.py | 1 - homeassistant/components/switch/vera.py | 9 +-------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/homeassistant/components/light/vera.py b/homeassistant/components/light/vera.py index bcf27eab5f7..95e0d849762 100644 --- a/homeassistant/components/light/vera.py +++ b/homeassistant/components/light/vera.py @@ -86,5 +86,4 @@ class VeraLight(VeraSwitch): else: self.vera_device.switch_on() - self.last_command_send = time.time() self.is_on_status = True diff --git a/homeassistant/components/switch/vera.py b/homeassistant/components/switch/vera.py index 91a83086570..1f67d3cfa69 100644 --- a/homeassistant/components/switch/vera.py +++ b/homeassistant/components/switch/vera.py @@ -87,8 +87,6 @@ class VeraSwitch(ToggleEntity): else: self._name = self.vera_device.name self.is_on_status = False - # for debouncing status check after command is sent - self.last_command_send = 0 self.controller.register(vera_device) self.controller.on( @@ -132,12 +130,10 @@ class VeraSwitch(ToggleEntity): return attr def turn_on(self, **kwargs): - self.last_command_send = time.time() self.vera_device.switch_on() self.is_on_status = True def turn_off(self, **kwargs): - self.last_command_send = time.time() self.vera_device.switch_off() self.is_on_status = False @@ -152,10 +148,7 @@ class VeraSwitch(ToggleEntity): return self.is_on_status def update(self): - # We need to debounce the status call after turning switch on or off - # because the vera has some lag in updating the device status try: - if (self.last_command_send + 5) < time.time(): - self.is_on_status = self.vera_device.is_switched_on() + self.is_on_status = self.vera_device.is_switched_on() except RequestException: _LOGGER.warning('Could not update status for %s', self.name) From b64680e4a827c7191c24b05c373306e8e9b0f8f0 Mon Sep 17 00:00:00 2001 From: pavoni Date: Sat, 9 Jan 2016 21:13:34 +0000 Subject: [PATCH 2/5] Revise to depend on vera subscription data updates, rather than talking to device. --- homeassistant/components/sensor/vera.py | 10 ++++------ homeassistant/components/switch/vera.py | 12 +++++------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/sensor/vera.py b/homeassistant/components/sensor/vera.py index c7b95ce5d53..ef581f22dc3 100644 --- a/homeassistant/components/sensor/vera.py +++ b/homeassistant/components/sensor/vera.py @@ -119,18 +119,18 @@ class VeraSensor(Entity): attr[ATTR_BATTERY_LEVEL] = self.vera_device.battery_level + '%' if self.vera_device.is_armable: - armed = self.vera_device.refresh_value('Armed') + armed = self.vera_device.get_value('Armed') attr[ATTR_ARMED] = 'True' if armed == '1' else 'False' if self.vera_device.is_trippable: - last_tripped = self.vera_device.refresh_value('LastTrip') + last_tripped = self.vera_device.get_value('LastTrip') if last_tripped is not None: utc_time = dt_util.utc_from_timestamp(int(last_tripped)) attr[ATTR_LAST_TRIP_TIME] = dt_util.datetime_to_str( utc_time) else: attr[ATTR_LAST_TRIP_TIME] = None - tripped = self.vera_device.refresh_value('Tripped') + tripped = self.vera_device.get_value('Tripped') attr[ATTR_TRIPPED] = 'True' if tripped == '1' else 'False' attr['Vera Device Id'] = self.vera_device.vera_device_id @@ -143,7 +143,6 @@ class VeraSensor(Entity): def update(self): if self.vera_device.category == "Temperature Sensor": - self.vera_device.refresh_value('CurrentTemperature') current_temp = self.vera_device.get_value('CurrentTemperature') vera_temp_units = self.vera_device.veraController.temperature_units @@ -161,10 +160,9 @@ class VeraSensor(Entity): self.current_value = current_temp elif self.vera_device.category == "Light Sensor": - self.vera_device.refresh_value('CurrentLevel') self.current_value = self.vera_device.get_value('CurrentLevel') elif self.vera_device.category == "Sensor": - tripped = self.vera_device.refresh_value('Tripped') + tripped = self.vera_device.get_value('Tripped') self.current_value = 'Tripped' if tripped == '1' else 'Not Tripped' else: self.current_value = 'Unknown' diff --git a/homeassistant/components/switch/vera.py b/homeassistant/components/switch/vera.py index 1f67d3cfa69..0e29583bf5c 100644 --- a/homeassistant/components/switch/vera.py +++ b/homeassistant/components/switch/vera.py @@ -111,18 +111,18 @@ class VeraSwitch(ToggleEntity): attr[ATTR_BATTERY_LEVEL] = self.vera_device.battery_level + '%' if self.vera_device.is_armable: - armed = self.vera_device.refresh_value('Armed') + armed = self.vera_device.get_value('Armed') attr[ATTR_ARMED] = 'True' if armed == '1' else 'False' if self.vera_device.is_trippable: - last_tripped = self.vera_device.refresh_value('LastTrip') + last_tripped = self.vera_device.get_value('LastTrip') if last_tripped is not None: utc_time = dt_util.utc_from_timestamp(int(last_tripped)) attr[ATTR_LAST_TRIP_TIME] = dt_util.datetime_to_str( utc_time) else: attr[ATTR_LAST_TRIP_TIME] = None - tripped = self.vera_device.refresh_value('Tripped') + tripped = self.vera_device.get_value('Tripped') attr[ATTR_TRIPPED] = 'True' if tripped == '1' else 'False' attr['Vera Device Id'] = self.vera_device.vera_device_id @@ -133,6 +133,7 @@ class VeraSwitch(ToggleEntity): self.vera_device.switch_on() self.is_on_status = True + def turn_off(self, **kwargs): self.vera_device.switch_off() self.is_on_status = False @@ -148,7 +149,4 @@ class VeraSwitch(ToggleEntity): return self.is_on_status def update(self): - try: - self.is_on_status = self.vera_device.is_switched_on() - except RequestException: - _LOGGER.warning('Could not update status for %s', self.name) + self.is_on_status = self.vera_device.is_switched_on() From af21f72d179312b3295fbfc7249751e978a7cf80 Mon Sep 17 00:00:00 2001 From: pavoni Date: Sat, 9 Jan 2016 22:58:28 +0000 Subject: [PATCH 3/5] Update pyvera version. --- homeassistant/components/light/vera.py | 2 +- homeassistant/components/sensor/vera.py | 6 ++---- homeassistant/components/switch/vera.py | 6 ++---- requirements_all.txt | 2 +- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/light/vera.py b/homeassistant/components/light/vera.py index 95e0d849762..76718e71eb4 100644 --- a/homeassistant/components/light/vera.py +++ b/homeassistant/components/light/vera.py @@ -16,7 +16,7 @@ from homeassistant.components.light import ATTR_BRIGHTNESS from homeassistant.const import EVENT_HOMEASSISTANT_STOP -REQUIREMENTS = ['pyvera==0.2.2'] +REQUIREMENTS = ['pyvera==0.2.3'] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/sensor/vera.py b/homeassistant/components/sensor/vera.py index ef581f22dc3..6ef5d469c60 100644 --- a/homeassistant/components/sensor/vera.py +++ b/homeassistant/components/sensor/vera.py @@ -15,7 +15,7 @@ from homeassistant.const import ( ATTR_BATTERY_LEVEL, ATTR_TRIPPED, ATTR_ARMED, ATTR_LAST_TRIP_TIME, TEMP_CELCIUS, TEMP_FAHRENHEIT, EVENT_HOMEASSISTANT_STOP) -REQUIREMENTS = ['pyvera==0.2.2'] +REQUIREMENTS = ['pyvera==0.2.3'] _LOGGER = logging.getLogger(__name__) @@ -85,9 +85,7 @@ class VeraSensor(Entity): self.current_value = '' self._temperature_units = None - self.controller.register(vera_device) - self.controller.on( - vera_device, self._update_callback) + self.controller.register(vera_device, self._update_callback) def _update_callback(self, _device): """ Called by the vera device callback to update state. """ diff --git a/homeassistant/components/switch/vera.py b/homeassistant/components/switch/vera.py index 0e29583bf5c..0acc33bea4e 100644 --- a/homeassistant/components/switch/vera.py +++ b/homeassistant/components/switch/vera.py @@ -19,7 +19,7 @@ from homeassistant.const import ( ATTR_LAST_TRIP_TIME, EVENT_HOMEASSISTANT_STOP) -REQUIREMENTS = ['pyvera==0.2.2'] +REQUIREMENTS = ['pyvera==0.2.3'] _LOGGER = logging.getLogger(__name__) @@ -88,9 +88,7 @@ class VeraSwitch(ToggleEntity): self._name = self.vera_device.name self.is_on_status = False - self.controller.register(vera_device) - self.controller.on( - vera_device, self._update_callback) + self.controller.register(vera_device, self._update_callback) def _update_callback(self, _device): """ Called by the vera device callback to update state. """ diff --git a/requirements_all.txt b/requirements_all.txt index 5a31ff4c379..64792267213 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -59,7 +59,7 @@ tellcore-py==1.1.2 # homeassistant.components.light.vera # homeassistant.components.sensor.vera # homeassistant.components.switch.vera -pyvera==0.2.2 +pyvera==0.2.3 # homeassistant.components.wink # homeassistant.components.light.wink From 4b4fb038e3e8c3914448ac0a40d9a85f2e29235b Mon Sep 17 00:00:00 2001 From: pavoni Date: Sun, 10 Jan 2016 12:30:47 +0000 Subject: [PATCH 4/5] 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() From fca8ad5b0b85cde597ab9f3bcdf7acb82dce99d8 Mon Sep 17 00:00:00 2001 From: pavoni Date: Sun, 10 Jan 2016 12:48:36 +0000 Subject: [PATCH 5/5] Tidy. --- homeassistant/components/light/vera.py | 1 - homeassistant/components/switch/vera.py | 4 ---- 2 files changed, 5 deletions(-) diff --git a/homeassistant/components/light/vera.py b/homeassistant/components/light/vera.py index 59abbe6b29d..42a5e7b7899 100644 --- a/homeassistant/components/light/vera.py +++ b/homeassistant/components/light/vera.py @@ -7,7 +7,6 @@ For more details about this platform, please refer to the documentation at https://home-assistant.io/components/light.vera/ """ import logging -import time from requests.exceptions import RequestException from homeassistant.components.switch.vera import VeraSwitch diff --git a/homeassistant/components/switch/vera.py b/homeassistant/components/switch/vera.py index 3f5d2a08cb8..4094fe61f4f 100644 --- a/homeassistant/components/switch/vera.py +++ b/homeassistant/components/switch/vera.py @@ -7,11 +7,9 @@ For more details about this platform, please refer to the documentation at https://home-assistant.io/components/switch.vera/ """ import logging -import time 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 ( @@ -138,7 +136,6 @@ class VeraSwitch(SwitchDevice): self._state = STATE_ON self.update_ha_state() - def turn_off(self, **kwargs): self.vera_device.switch_off() self._state = STATE_OFF @@ -153,4 +150,3 @@ class VeraSwitch(SwitchDevice): def is_on(self): """ True if device is on. """ return self._state == STATE_ON -