diff --git a/homeassistant/components/light/vera.py b/homeassistant/components/light/vera.py index bcf27eab5f7..42a5e7b7899 100644 --- a/homeassistant/components/light/vera.py +++ b/homeassistant/components/light/vera.py @@ -7,16 +7,15 @@ 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 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.2'] +REQUIREMENTS = ['pyvera==0.2.3'] _LOGGER = logging.getLogger(__name__) @@ -59,7 +58,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,5 +85,5 @@ class VeraLight(VeraSwitch): else: self.vera_device.switch_on() - self.last_command_send = time.time() - 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 c7b95ce5d53..b381974ab31 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__) @@ -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: @@ -85,18 +85,14 @@ 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. """ - _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): @@ -119,18 +115,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 +139,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 +156,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 91a83086570..4094fe61f4f 100644 --- a/homeassistant/components/switch/vera.py +++ b/homeassistant/components/switch/vera.py @@ -7,19 +7,21 @@ 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 ( 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.2'] +REQUIREMENTS = ['pyvera==0.2.3'] _LOGGER = logging.getLogger(__name__) @@ -60,7 +62,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 +77,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,19 +88,17 @@ class VeraSwitch(ToggleEntity): self._name = self.extra_data.get('name') 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._state = STATE_OFF - 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. """ - _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): @@ -113,18 +113,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 @@ -132,14 +132,14 @@ 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 + self._state = STATE_ON + self.update_ha_state() def turn_off(self, **kwargs): - self.last_command_send = time.time() self.vera_device.switch_off() - self.is_on_status = False + self._state = STATE_OFF + self.update_ha_state() @property def should_poll(self): @@ -149,13 +149,4 @@ class VeraSwitch(ToggleEntity): @property def is_on(self): """ True if device is on. """ - 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() - except RequestException: - _LOGGER.warning('Could not update status for %s', self.name) + return self._state == STATE_ON diff --git a/requirements_all.txt b/requirements_all.txt index 74cf5a1e993..f320408f138 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