Refactor ZHA listeners into channels (#21196)

* refactor listeners to channels

* update coveragerc
This commit is contained in:
David F. Mulcahey 2019-02-19 12:58:22 -05:00 committed by Paulus Schoutsen
parent fe4a2b5b31
commit 3be8178035
28 changed files with 1037 additions and 899 deletions

View file

@ -10,8 +10,8 @@ from homeassistant.components import light
from homeassistant.helpers.dispatcher import async_dispatcher_connect
import homeassistant.util.color as color_util
from .const import (
DATA_ZHA, DATA_ZHA_DISPATCHERS, ZHA_DISCOVERY_NEW, LISTENER_COLOR,
LISTENER_ON_OFF, LISTENER_LEVEL, SIGNAL_ATTR_UPDATED, SIGNAL_SET_LEVEL
DATA_ZHA, DATA_ZHA_DISPATCHERS, ZHA_DISCOVERY_NEW, COLOR_CHANNEL,
ON_OFF_CHANNEL, LEVEL_CHANNEL, SIGNAL_ATTR_UPDATED, SIGNAL_SET_LEVEL
)
from .entity import ZhaEntity
@ -67,24 +67,24 @@ class Light(ZhaEntity, light.Light):
_domain = light.DOMAIN
def __init__(self, unique_id, zha_device, listeners, **kwargs):
def __init__(self, unique_id, zha_device, channels, **kwargs):
"""Initialize the ZHA light."""
super().__init__(unique_id, zha_device, listeners, **kwargs)
super().__init__(unique_id, zha_device, channels, **kwargs)
self._supported_features = 0
self._color_temp = None
self._hs_color = None
self._brightness = None
self._on_off_listener = self.cluster_listeners.get(LISTENER_ON_OFF)
self._level_listener = self.cluster_listeners.get(LISTENER_LEVEL)
self._color_listener = self.cluster_listeners.get(LISTENER_COLOR)
self._on_off_channel = self.cluster_channels.get(ON_OFF_CHANNEL)
self._level_channel = self.cluster_channels.get(LEVEL_CHANNEL)
self._color_channel = self.cluster_channels.get(COLOR_CHANNEL)
if self._level_listener:
if self._level_channel:
self._supported_features |= light.SUPPORT_BRIGHTNESS
self._supported_features |= light.SUPPORT_TRANSITION
self._brightness = 0
if self._color_listener:
color_capabilities = self._color_listener.get_color_capabilities()
if self._color_channel:
color_capabilities = self._color_channel.get_color_capabilities()
if color_capabilities & CAPABILITIES_COLOR_TEMP:
self._supported_features |= light.SUPPORT_COLOR_TEMP
@ -139,10 +139,10 @@ class Light(ZhaEntity, light.Light):
"""Run when about to be added to hass."""
await super().async_added_to_hass()
await self.async_accept_signal(
self._on_off_listener, SIGNAL_ATTR_UPDATED, self.async_set_state)
if self._level_listener:
self._on_off_channel, SIGNAL_ATTR_UPDATED, self.async_set_state)
if self._level_channel:
await self.async_accept_signal(
self._level_listener, SIGNAL_SET_LEVEL, self.set_level)
self._level_channel, SIGNAL_SET_LEVEL, self.set_level)
async def async_turn_on(self, **kwargs):
"""Turn the entity on."""
@ -152,7 +152,7 @@ class Light(ZhaEntity, light.Light):
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_listener.move_to_color_temp(
success = await self._color_channel.move_to_color_temp(
temperature, duration)
if not success:
return
@ -162,7 +162,7 @@ class Light(ZhaEntity, light.Light):
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_listener.move_to_color(
success = await self._color_channel.move_to_color(
int(xy_color[0] * 65535),
int(xy_color[1] * 65535),
duration,
@ -174,7 +174,7 @@ class Light(ZhaEntity, light.Light):
if self._brightness is not None:
brightness = kwargs.get(
light.ATTR_BRIGHTNESS, self._brightness or 255)
success = await self._level_listener.move_to_level_with_on_off(
success = await self._level_channel.move_to_level_with_on_off(
brightness,
duration
)
@ -185,7 +185,7 @@ class Light(ZhaEntity, light.Light):
self.async_schedule_update_ha_state()
return
success = await self._on_off_listener.on()
success = await self._on_off_channel.on()
if not success:
return
@ -198,12 +198,12 @@ class Light(ZhaEntity, light.Light):
supports_level = self.supported_features & light.SUPPORT_BRIGHTNESS
success = None
if duration and supports_level:
success = await self._level_listener.move_to_level_with_on_off(
success = await self._level_channel.move_to_level_with_on_off(
0,
duration*10
)
else:
success = await self._on_off_listener.off()
success = await self._on_off_channel.off()
_LOGGER.debug("%s was turned off: %s", self.entity_id, success)
if not success:
return