Skip to content

Commit

Permalink
Update channel command wrapper to return the entire result
Browse files Browse the repository at this point in the history
This allows for return values other than result[1]
  • Loading branch information
presslab-us committed Jun 6, 2019
1 parent 33506ed commit 6ac908b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 36 deletions.
8 changes: 3 additions & 5 deletions homeassistant/components/zha/core/channels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def decorate_command(channel, command):
"""Wrap a cluster command to make it safe."""
@wraps(command)
async def wrapper(*args, **kwds):
from zigpy.zcl.foundation import Status
from zigpy.exceptions import DeliveryError
try:
result = await command(*args, **kwds)
Expand All @@ -54,17 +53,16 @@ async def wrapper(*args, **kwds):
"{}: {}".format("with args", args),
"{}: {}".format("with kwargs", kwds),
"{}: {}".format("and result", result))
if isinstance(result, bool):
return result
return result[1] is Status.SUCCESS
return result

except (DeliveryError, Timeout) as ex:
_LOGGER.debug(
"%s: command failed: %s exception: %s",
channel.unique_id,
command.__name__,
str(ex)
)
return False
return ex
return wrapper


Expand Down
34 changes: 17 additions & 17 deletions homeassistant/components/zha/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
ON_OFF_CHANNEL, LEVEL_CHANNEL, SIGNAL_ATTR_UPDATED, SIGNAL_SET_LEVEL
)
from .entity import ZhaEntity

from zigpy.zcl.foundation import Status

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -173,33 +173,33 @@ async def async_turn_on(self, **kwargs):
level = min(254, brightness)
else:
level = self._brightness or 254
success = await self._level_channel.move_to_level_with_on_off(
result = await self._level_channel.move_to_level_with_on_off(
level,
duration
)
t_log['move_to_level_with_on_off'] = success
if not success:
t_log['move_to_level_with_on_off'] = result
if not isinstance(result, list) or result[1] is not Status.SUCCESS:
self.debug("turned on: %s", t_log)
return
self._state = bool(level)
if level:
self._brightness = level

if brightness is None or brightness:
success = await self._on_off_channel.on()
t_log['on_off'] = success
if not success:
result = await self._on_off_channel.on()
t_log['on_off'] = result
if not isinstance(result, list) or result[1] is not Status.SUCCESS:
self.debug("turned on: %s", t_log)
return
self._state = True

if light.ATTR_COLOR_TEMP in kwargs and \
self.supported_features & light.SUPPORT_COLOR_TEMP:
temperature = kwargs[light.ATTR_COLOR_TEMP]
success = await self._color_channel.move_to_color_temp(
result = await self._color_channel.move_to_color_temp(
temperature, duration)
t_log['move_to_color_temp'] = success
if not success:
t_log['move_to_color_temp'] = result
if not isinstance(result, list) or result[1] is not Status.SUCCESS:
self.debug("turned on: %s", t_log)
return
self._color_temp = temperature
Expand All @@ -208,13 +208,13 @@ async def async_turn_on(self, **kwargs):
self.supported_features & light.SUPPORT_COLOR:
hs_color = kwargs[light.ATTR_HS_COLOR]
xy_color = color_util.color_hs_to_xy(*hs_color)
success = await self._color_channel.move_to_color(
result = await self._color_channel.move_to_color(
int(xy_color[0] * 65535),
int(xy_color[1] * 65535),
duration,
)
t_log['move_to_color'] = success
if not success:
t_log['move_to_color'] = result
if not isinstance(result, list) or result[1] is not Status.SUCCESS:
self.debug("turned on: %s", t_log)
return
self._hs_color = hs_color
Expand All @@ -227,14 +227,14 @@ async def async_turn_off(self, **kwargs):
duration = kwargs.get(light.ATTR_TRANSITION)
supports_level = self.supported_features & light.SUPPORT_BRIGHTNESS
if duration and supports_level:
success = await self._level_channel.move_to_level_with_on_off(
result = await self._level_channel.move_to_level_with_on_off(
0,
duration*10
)
else:
success = await self._on_off_channel.off()
self.debug("turned off: %s", success)
if not success:
result = await self._on_off_channel.off()
self.debug("turned off: %s", result)
if not isinstance(result, list) or result[1] is not Status.SUCCESS:
return
self._state = False
self.async_schedule_update_ha_state()
Expand Down
17 changes: 7 additions & 10 deletions homeassistant/components/zha/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
SIGNAL_ATTR_UPDATED
)
from .entity import ZhaEntity
from zigpy.zcl.foundation import Status

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -93,23 +94,19 @@ async def async_lock(self, **kwargs):
"""Lock the lock.
This method must be run in the event loop and returns a coroutine.
"""
success = await self._doorlock_channel.lock_door()
t_log = {}
t_log['lock'] = success
if not success:
self.debug("locked: %s", t_log)
result = await self._doorlock_channel.lock_door()
if not isinstance(result, list) or result[0] is not Status.SUCCESS:
_LOGGER.error("Error with lock_door: %s", result)
return
self.async_schedule_update_ha_state()

async def async_unlock(self, **kwargs):
"""Lock the lock.
This method must be run in the event loop and returns a coroutine.
"""
success = await self._doorlock_channel.unlock_door()
t_log = {}
t_log['unlock'] = success
if not success:
self.debug("unlocked: %s", t_log)
result = await self._doorlock_channel.unlock_door()
if not isinstance(result, list) or result[0] is not Status.SUCCESS:
_LOGGER.error("Error with unlock_door: %s", result)
return
self.async_schedule_update_ha_state()

Expand Down
9 changes: 5 additions & 4 deletions homeassistant/components/zha/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
SIGNAL_ATTR_UPDATED
)
from .entity import ZhaEntity
from zigpy.zcl.foundation import Status

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -66,16 +67,16 @@ def is_on(self) -> bool:

async def async_turn_on(self, **kwargs):
"""Turn the entity on."""
success = await self._on_off_channel.on()
if not success:
result = await self._on_off_channel.on()
if not isinstance(result, list) or result[1] is not Status.SUCCESS:
return
self._state = True
self.async_schedule_update_ha_state()

async def async_turn_off(self, **kwargs):
"""Turn the entity off."""
success = await self._on_off_channel.off()
if not success:
result = await self._on_off_channel.off()
if not isinstance(result, list) or result[1] is not Status.SUCCESS:
return
self._state = False
self.async_schedule_update_ha_state()
Expand Down

0 comments on commit 6ac908b

Please sign in to comment.