From b1bba3675d65141a948e5ed21b234b6d6f1aba22 Mon Sep 17 00:00:00 2001 From: Russell Cloran Date: Thu, 6 Jul 2017 22:59:17 -0700 Subject: [PATCH] zha light: Refresh at startup (#8310) * zha light: Refresh at startup * Add asyncio.coroutine annotation --- homeassistant/components/light/zha.py | 54 ++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/light/zha.py b/homeassistant/components/light/zha.py index 0c230877625..650d9f909b1 100644 --- a/homeassistant/components/light/zha.py +++ b/homeassistant/components/light/zha.py @@ -29,7 +29,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): except (AttributeError, KeyError): pass - async_add_devices([Light(**discovery_info)]) + async_add_devices([Light(**discovery_info)], update_before_add=True) class Light(zha.Entity, light.Light): @@ -99,16 +99,19 @@ class Light(zha.Entity, light.Light): duration ) self._state = 1 + self.hass.async_add_job(self.async_update_ha_state()) return yield from self._endpoint.on_off.on() self._state = 1 + self.hass.async_add_job(self.async_update_ha_state()) @asyncio.coroutine def async_turn_off(self, **kwargs): """Turn the entity off.""" yield from self._endpoint.on_off.off() self._state = 0 + self.hass.async_add_job(self.async_update_ha_state()) @property def brightness(self): @@ -129,3 +132,52 @@ class Light(zha.Entity, light.Light): def supported_features(self): """Flag supported features.""" return self._supported_features + + @asyncio.coroutine + def async_update(self): + """Retrieve latest state.""" + _LOGGER.debug("%s async_update", self.entity_id) + + @asyncio.coroutine + def safe_read(cluster, attributes): + """Swallow all exceptions from network read. + + If we throw during initialization, setup fails. Rather have an + entity that exists, but is in a maybe wrong state, than no entity. + """ + try: + result, _ = yield from cluster.read_attributes( + attributes, + allow_cache=False, + ) + return result + except Exception: # pylint: disable=broad-except + return {} + + result = yield from safe_read(self._endpoint.on_off, ['on_off']) + self._state = result.get('on_off', self._state) + + if self._supported_features & light.SUPPORT_BRIGHTNESS: + result = yield from safe_read(self._endpoint.level, + ['current_level']) + self._brightness = result.get('current_level', self._brightness) + + if self._supported_features & light.SUPPORT_COLOR_TEMP: + result = yield from safe_read(self._endpoint.light_color, + ['color_temperature']) + self._color_temp = result.get('color_temperature', + self._color_temp) + + if self._supported_features & light.SUPPORT_XY_COLOR: + result = yield from safe_read(self._endpoint.light_color, + ['current_x', 'current_y']) + if 'current_x' in result and 'current_y' in result: + self._xy_color = (result['current_x'], result['current_y']) + + @property + def should_poll(self) -> bool: + """Return True if entity has to be polled for state. + + False if entity pushes its state to HA. + """ + return False