2017-04-25 05:24:57 +00:00
|
|
|
"""
|
|
|
|
Lights on Zigbee Home Automation networks.
|
|
|
|
|
|
|
|
For more details on this platform, please refer to the documentation
|
|
|
|
at https://home-assistant.io/components/light.zha/
|
|
|
|
"""
|
|
|
|
import logging
|
2018-12-04 10:38:57 +00:00
|
|
|
|
2018-11-22 18:00:46 +00:00
|
|
|
from homeassistant.components import light
|
2019-01-16 19:06:22 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
import homeassistant.util.color as color_util
|
2019-02-06 18:33:21 +00:00
|
|
|
from .const import (
|
2019-02-19 17:58:22 +00:00
|
|
|
DATA_ZHA, DATA_ZHA_DISPATCHERS, ZHA_DISCOVERY_NEW, COLOR_CHANNEL,
|
|
|
|
ON_OFF_CHANNEL, LEVEL_CHANNEL, SIGNAL_ATTR_UPDATED, SIGNAL_SET_LEVEL
|
2019-02-06 18:33:21 +00:00
|
|
|
)
|
2019-01-26 13:54:49 +00:00
|
|
|
from .entity import ZhaEntity
|
2019-02-06 18:33:21 +00:00
|
|
|
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEPENDENCIES = ['zha']
|
|
|
|
|
2019-02-27 13:34:38 +00:00
|
|
|
DEFAULT_DURATION = 5
|
2017-07-26 15:22:31 +00:00
|
|
|
|
2018-01-11 21:56:00 +00:00
|
|
|
CAPABILITIES_COLOR_XY = 0x08
|
|
|
|
CAPABILITIES_COLOR_TEMP = 0x10
|
|
|
|
|
|
|
|
UNSUPPORTED_ATTRIBUTE = 0x86
|
|
|
|
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities,
|
2018-03-12 20:57:13 +00:00
|
|
|
discovery_info=None):
|
2018-11-27 20:21:25 +00:00
|
|
|
"""Old way of setting up Zigbee Home Automation lights."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up the Zigbee Home Automation light from config entry."""
|
|
|
|
async def async_discover(discovery_info):
|
|
|
|
await _async_setup_entities(hass, config_entry, async_add_entities,
|
|
|
|
[discovery_info])
|
|
|
|
|
|
|
|
unsub = async_dispatcher_connect(
|
|
|
|
hass, ZHA_DISCOVERY_NEW.format(light.DOMAIN), async_discover)
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS].append(unsub)
|
|
|
|
|
|
|
|
lights = hass.data.get(DATA_ZHA, {}).get(light.DOMAIN)
|
|
|
|
if lights is not None:
|
|
|
|
await _async_setup_entities(hass, config_entry, async_add_entities,
|
|
|
|
lights.values())
|
|
|
|
del hass.data[DATA_ZHA][light.DOMAIN]
|
|
|
|
|
|
|
|
|
|
|
|
async def _async_setup_entities(hass, config_entry, async_add_entities,
|
|
|
|
discovery_infos):
|
|
|
|
"""Set up the ZHA lights."""
|
|
|
|
entities = []
|
|
|
|
for discovery_info in discovery_infos:
|
2018-12-19 13:52:20 +00:00
|
|
|
zha_light = Light(**discovery_info)
|
|
|
|
entities.append(zha_light)
|
2018-11-27 20:21:25 +00:00
|
|
|
|
|
|
|
async_add_entities(entities, update_before_add=True)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
|
2018-11-22 18:00:46 +00:00
|
|
|
class Light(ZhaEntity, light.Light):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Representation of a ZHA or ZLL light."""
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
_domain = light.DOMAIN
|
|
|
|
|
2019-02-19 17:58:22 +00:00
|
|
|
def __init__(self, unique_id, zha_device, channels, **kwargs):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Initialize the ZHA light."""
|
2019-02-19 17:58:22 +00:00
|
|
|
super().__init__(unique_id, zha_device, channels, **kwargs)
|
2017-04-25 05:24:57 +00:00
|
|
|
self._supported_features = 0
|
|
|
|
self._color_temp = None
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs_color = None
|
2017-04-25 05:24:57 +00:00
|
|
|
self._brightness = None
|
2019-02-19 17:58:22 +00:00
|
|
|
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)
|
2019-02-06 18:33:21 +00:00
|
|
|
|
2019-02-19 17:58:22 +00:00
|
|
|
if self._level_channel:
|
2017-04-25 05:24:57 +00:00
|
|
|
self._supported_features |= light.SUPPORT_BRIGHTNESS
|
2017-07-26 15:22:31 +00:00
|
|
|
self._supported_features |= light.SUPPORT_TRANSITION
|
2017-04-25 05:24:57 +00:00
|
|
|
self._brightness = 0
|
2019-02-06 18:33:21 +00:00
|
|
|
|
2019-02-19 17:58:22 +00:00
|
|
|
if self._color_channel:
|
|
|
|
color_capabilities = self._color_channel.get_color_capabilities()
|
2018-01-11 21:56:00 +00:00
|
|
|
if color_capabilities & CAPABILITIES_COLOR_TEMP:
|
2017-08-31 05:18:01 +00:00
|
|
|
self._supported_features |= light.SUPPORT_COLOR_TEMP
|
|
|
|
|
2018-01-11 21:56:00 +00:00
|
|
|
if color_capabilities & CAPABILITIES_COLOR_XY:
|
2018-03-18 22:00:29 +00:00
|
|
|
self._supported_features |= light.SUPPORT_COLOR
|
|
|
|
self._hs_color = (0, 0)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return true if entity is on."""
|
2018-05-12 12:41:44 +00:00
|
|
|
if self._state is None:
|
2017-04-25 05:24:57 +00:00
|
|
|
return False
|
2019-02-06 18:33:21 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light."""
|
|
|
|
return self._brightness
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return state attributes."""
|
|
|
|
return self.state_attributes
|
|
|
|
|
|
|
|
def set_level(self, value):
|
2019-02-27 13:34:38 +00:00
|
|
|
"""Set the brightness of this light between 0..254.
|
|
|
|
|
|
|
|
brightness level 255 is a special value instructing the device to come
|
|
|
|
on at `on_level` Zigbee attribute value, regardless of the last set
|
|
|
|
level
|
|
|
|
"""
|
|
|
|
value = max(0, min(254, value))
|
2019-02-06 18:33:21 +00:00
|
|
|
self._brightness = value
|
2019-02-07 23:09:47 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2019-02-06 18:33:21 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def hs_color(self):
|
|
|
|
"""Return the hs color value [int, int]."""
|
|
|
|
return self._hs_color
|
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self):
|
|
|
|
"""Return the CT color value in mireds."""
|
|
|
|
return self._color_temp
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return self._supported_features
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
def async_set_state(self, state):
|
2018-12-23 15:16:21 +00:00
|
|
|
"""Set the state."""
|
2019-02-06 18:33:21 +00:00
|
|
|
self._state = bool(state)
|
2018-12-23 15:16:21 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Run when about to be added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
await self.async_accept_signal(
|
2019-02-19 17:58:22 +00:00
|
|
|
self._on_off_channel, SIGNAL_ATTR_UPDATED, self.async_set_state)
|
|
|
|
if self._level_channel:
|
2019-02-06 18:33:21 +00:00
|
|
|
await self.async_accept_signal(
|
2019-02-19 17:58:22 +00:00
|
|
|
self._level_channel, SIGNAL_SET_LEVEL, self.set_level)
|
2019-02-06 18:33:21 +00:00
|
|
|
|
2018-03-12 20:57:13 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2017-04-25 05:24:57 +00:00
|
|
|
"""Turn the entity on."""
|
2019-02-27 13:34:38 +00:00
|
|
|
transition = kwargs.get(light.ATTR_TRANSITION)
|
|
|
|
duration = transition * 10 if transition else DEFAULT_DURATION
|
|
|
|
brightness = kwargs.get(light.ATTR_BRIGHTNESS)
|
|
|
|
|
|
|
|
if (brightness is not None or transition) and \
|
|
|
|
self._supported_features & light.SUPPORT_BRIGHTNESS:
|
|
|
|
if brightness is not None:
|
|
|
|
level = min(254, brightness)
|
|
|
|
else:
|
|
|
|
level = self._brightness or 254
|
|
|
|
success = await self._level_channel.move_to_level_with_on_off(
|
|
|
|
level,
|
|
|
|
duration
|
|
|
|
)
|
|
|
|
if not success:
|
|
|
|
return
|
|
|
|
self._state = bool(level)
|
|
|
|
if level:
|
|
|
|
self._brightness = level
|
|
|
|
|
|
|
|
if brightness is None or brightness:
|
|
|
|
success = await self._on_off_channel.on()
|
|
|
|
if not success:
|
|
|
|
return
|
|
|
|
self._state = True
|
2019-02-06 18:33:21 +00:00
|
|
|
|
2019-01-16 00:12:23 +00:00
|
|
|
if light.ATTR_COLOR_TEMP in kwargs and \
|
|
|
|
self.supported_features & light.SUPPORT_COLOR_TEMP:
|
2017-04-25 05:24:57 +00:00
|
|
|
temperature = kwargs[light.ATTR_COLOR_TEMP]
|
2019-02-19 17:58:22 +00:00
|
|
|
success = await self._color_channel.move_to_color_temp(
|
2019-02-06 18:33:21 +00:00
|
|
|
temperature, duration)
|
|
|
|
if not success:
|
2018-09-20 18:23:09 +00:00
|
|
|
return
|
2017-04-25 05:24:57 +00:00
|
|
|
self._color_temp = temperature
|
|
|
|
|
2019-01-16 00:12:23 +00:00
|
|
|
if light.ATTR_HS_COLOR in kwargs and \
|
|
|
|
self.supported_features & light.SUPPORT_COLOR:
|
2019-02-06 18:33:21 +00:00
|
|
|
hs_color = kwargs[light.ATTR_HS_COLOR]
|
|
|
|
xy_color = color_util.color_hs_to_xy(*hs_color)
|
2019-02-19 17:58:22 +00:00
|
|
|
success = await self._color_channel.move_to_color(
|
2019-02-06 18:33:21 +00:00
|
|
|
int(xy_color[0] * 65535),
|
|
|
|
int(xy_color[1] * 65535),
|
|
|
|
duration,
|
|
|
|
)
|
|
|
|
if not success:
|
2018-09-20 18:23:09 +00:00
|
|
|
return
|
2019-02-06 18:33:21 +00:00
|
|
|
self._hs_color = hs_color
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-03-12 20:57:13 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2017-04-25 05:24:57 +00:00
|
|
|
"""Turn the entity off."""
|
2018-12-23 11:15:54 +00:00
|
|
|
duration = kwargs.get(light.ATTR_TRANSITION)
|
2019-02-06 18:33:21 +00:00
|
|
|
supports_level = self.supported_features & light.SUPPORT_BRIGHTNESS
|
|
|
|
if duration and supports_level:
|
2019-02-19 17:58:22 +00:00
|
|
|
success = await self._level_channel.move_to_level_with_on_off(
|
2019-02-06 18:33:21 +00:00
|
|
|
0,
|
|
|
|
duration*10
|
|
|
|
)
|
|
|
|
else:
|
2019-02-19 17:58:22 +00:00
|
|
|
success = await self._on_off_channel.off()
|
2019-02-06 18:33:21 +00:00
|
|
|
_LOGGER.debug("%s was turned off: %s", self.entity_id, success)
|
|
|
|
if not success:
|
2018-03-19 21:12:53 +00:00
|
|
|
return
|
2019-02-06 18:33:21 +00:00
|
|
|
self._state = False
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|