Skip to content

Commit

Permalink
light.zha: Catch exceptions for all commands. (#16752)
Browse files Browse the repository at this point in the history
Catch exceptions for all operations which may fail because of device
reachibility
More verbose debug logging on operations
  • Loading branch information
Adminiuga authored and balloob committed Sep 20, 2018
1 parent c6ccbed commit 3ea8c25
Showing 1 changed file with 43 additions and 16 deletions.
59 changes: 43 additions & 16 deletions homeassistant/components/light/zha.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 3ea8c25

Please sign in to comment.