light.zha: Catch exceptions for all commands. (#16752)

Catch exceptions for all operations which may fail because of device
reachibility
More verbose debug logging on operations
This commit is contained in:
Alexei Chetroi 2018-09-20 14:23:09 -04:00 committed by Paulus Schoutsen
parent c6ccbed828
commit 3ea8c25e1f

View File

@ -81,40 +81,65 @@ class Light(zha.Entity, light.Light):
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs):
"""Turn the entity on.""" """Turn the entity on."""
from zigpy.exceptions import DeliveryError
duration = kwargs.get(light.ATTR_TRANSITION, DEFAULT_DURATION) duration = kwargs.get(light.ATTR_TRANSITION, DEFAULT_DURATION)
duration = duration * 10 # tenths of s duration = duration * 10 # tenths of s
if light.ATTR_COLOR_TEMP in kwargs: if light.ATTR_COLOR_TEMP in kwargs:
temperature = kwargs[light.ATTR_COLOR_TEMP] temperature = kwargs[light.ATTR_COLOR_TEMP]
await self._endpoint.light_color.move_to_color_temp( try:
temperature, duration) 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 self._color_temp = temperature
if light.ATTR_HS_COLOR in kwargs: if light.ATTR_HS_COLOR in kwargs:
self._hs_color = kwargs[light.ATTR_HS_COLOR] self._hs_color = kwargs[light.ATTR_HS_COLOR]
xy_color = color_util.color_hs_to_xy(*self._hs_color) xy_color = color_util.color_hs_to_xy(*self._hs_color)
await self._endpoint.light_color.move_to_color( try:
int(xy_color[0] * 65535), res = await self._endpoint.light_color.move_to_color(
int(xy_color[1] * 65535), int(xy_color[0] * 65535),
duration, 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: if self._brightness is not None:
brightness = kwargs.get( brightness = kwargs.get(
light.ATTR_BRIGHTNESS, self._brightness or 255) light.ATTR_BRIGHTNESS, self._brightness or 255)
self._brightness = brightness self._brightness = brightness
# Move to level with on/off: # Move to level with on/off:
await self._endpoint.level.move_to_level_with_on_off( try:
brightness, res = await self._endpoint.level.move_to_level_with_on_off(
duration 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._state = 1
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
return return
from zigpy.exceptions import DeliveryError
try: 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: 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 return
self._state = 1 self._state = 1
@ -124,9 +149,11 @@ class Light(zha.Entity, light.Light):
"""Turn the entity off.""" """Turn the entity off."""
from zigpy.exceptions import DeliveryError from zigpy.exceptions import DeliveryError
try: 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: 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 return
self._state = 0 self._state = 0