From bcb497b7106c8b30632c41dfd342fe385ad8b33d Mon Sep 17 00:00:00 2001 From: Alexei Chetroi Date: Wed, 19 Sep 2018 19:46:26 -0400 Subject: [PATCH] light.zha: Catch exceptions for all commands. Catch exceptions for all operations which may fail because of device reachibility More verbose debug logging on operations --- homeassistant/components/light/zha.py | 59 +++++++++++++++++++-------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/light/zha.py b/homeassistant/components/light/zha.py index dd675ee674dea..56a1e9e5169bb 100644 --- a/homeassistant/components/light/zha.py +++ b/homeassistant/components/light/zha.py @@ -81,40 +81,65 @@ def is_on(self) -> bool: async def async_turn_on(self, **kwargs): """Turn the entity on.""" + from zigpy.exceptions import DeliveryError + duration = kwargs.get(light.ATTR_TRANSITION, DEFAULT_DURATION) duration = duration * 10 # tenths of s if light.ATTR_COLOR_TEMP in kwargs: temperature = kwargs[light.ATTR_COLOR_TEMP] - await self._endpoint.light_color.move_to_color_temp( - temperature, duration) + try: + res = await self._endpoint.light_color.move_to_color_temp( + temperature, duration) + _LOGGER.debug("%s: moved to %i color temp: %s", + self.entity_id, temperature, res) + except DeliveryError as ex: + _LOGGER.error("%s: Couldn't change color temp: %s", + self.entity_id, ex) + return self._color_temp = temperature if light.ATTR_HS_COLOR in kwargs: self._hs_color = kwargs[light.ATTR_HS_COLOR] xy_color = color_util.color_hs_to_xy(*self._hs_color) - await self._endpoint.light_color.move_to_color( - int(xy_color[0] * 65535), - int(xy_color[1] * 65535), - duration, - ) + try: + res = await self._endpoint.light_color.move_to_color( + int(xy_color[0] * 65535), + int(xy_color[1] * 65535), + duration, + ) + _LOGGER.debug("%s: moved XY color to (%1.2f, %1.2f): %s", + self.entity_id, xy_color[0], xy_color[1], res) + except DeliveryError as ex: + _LOGGER.error("%s: Couldn't change color temp: %s", + self.entity_id, ex) + return if self._brightness is not None: brightness = kwargs.get( light.ATTR_BRIGHTNESS, self._brightness or 255) self._brightness = brightness # Move to level with on/off: - await self._endpoint.level.move_to_level_with_on_off( - brightness, - duration - ) + try: + res = await self._endpoint.level.move_to_level_with_on_off( + brightness, + duration + ) + _LOGGER.debug("%s: moved to %i level with on/off: %s", + self.entity_id, brightness, res) + except DeliveryError as ex: + _LOGGER.error("%s: Couldn't change brightness level: %s", + self.entity_id, ex) + return self._state = 1 self.async_schedule_update_ha_state() return - from zigpy.exceptions import DeliveryError + try: - await self._endpoint.on_off.on() + res = await self._endpoint.on_off.on() + _LOGGER.debug("%s was turned on: %s", self.entity_id, res) except DeliveryError as ex: - _LOGGER.error("Unable to turn the light on: %s", ex) + _LOGGER.error("%s: Unable to turn the light on: %s", + self.entity_id, ex) return self._state = 1 @@ -124,9 +149,11 @@ async def async_turn_off(self, **kwargs): """Turn the entity off.""" from zigpy.exceptions import DeliveryError try: - await self._endpoint.on_off.off() + res = await self._endpoint.on_off.off() + _LOGGER.debug("%s was turned off: %s", self.entity_id, res) except DeliveryError as ex: - _LOGGER.error("Unable to turn the light off: %s", ex) + _LOGGER.error("%s: Unable to turn the light off: %s", + self.entity_id, ex) return self._state = 0