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
|
|
|
|
from homeassistant.components.zha import helpers
|
2018-11-27 20:21:25 +00:00
|
|
|
from homeassistant.components.zha.const import (
|
2018-12-19 13:52:20 +00:00
|
|
|
DATA_ZHA, DATA_ZHA_DISPATCHERS, REPORT_CONFIG_ASAP, REPORT_CONFIG_DEFAULT,
|
|
|
|
REPORT_CONFIG_IMMEDIATE, ZHA_DISCOVERY_NEW)
|
2018-12-04 10:38:57 +00:00
|
|
|
from homeassistant.components.zha.entities import ZhaEntity
|
2018-12-23 15:16:21 +00:00
|
|
|
from homeassistant.components.zha.entities.listeners import (
|
|
|
|
OnOffListener, LevelListener
|
|
|
|
)
|
2018-12-04 10:38:57 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2018-03-18 22:00:29 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEPENDENCIES = ['zha']
|
|
|
|
|
2017-07-26 15:22:31 +00:00
|
|
|
DEFAULT_DURATION = 0.5
|
|
|
|
|
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:
|
|
|
|
endpoint = discovery_info['endpoint']
|
|
|
|
if hasattr(endpoint, 'light_color'):
|
|
|
|
caps = await helpers.safe_read(
|
|
|
|
endpoint.light_color, ['color_capabilities'])
|
|
|
|
discovery_info['color_capabilities'] = caps.get(
|
|
|
|
'color_capabilities')
|
|
|
|
if discovery_info['color_capabilities'] is None:
|
|
|
|
# ZCL Version 4 devices don't support the color_capabilities
|
|
|
|
# attribute. In this version XY support is mandatory, but we
|
|
|
|
# need to probe to determine if the device supports color
|
|
|
|
# temperature.
|
|
|
|
discovery_info['color_capabilities'] = \
|
|
|
|
CAPABILITIES_COLOR_XY
|
|
|
|
result = await helpers.safe_read(
|
|
|
|
endpoint.light_color, ['color_temperature'])
|
|
|
|
if (result.get('color_temperature') is not
|
|
|
|
UNSUPPORTED_ATTRIBUTE):
|
|
|
|
discovery_info['color_capabilities'] |= \
|
|
|
|
CAPABILITIES_COLOR_TEMP
|
2018-12-23 15:16:21 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Initialize the ZHA light."""
|
2017-04-25 05:24:57 +00:00
|
|
|
super().__init__(**kwargs)
|
|
|
|
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
|
2018-12-23 15:16:21 +00:00
|
|
|
from zigpy.zcl.clusters.general import OnOff, LevelControl
|
|
|
|
self._in_listeners = {
|
|
|
|
OnOff.cluster_id: OnOffListener(
|
|
|
|
self,
|
|
|
|
self._in_clusters[OnOff.cluster_id]
|
|
|
|
),
|
|
|
|
}
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-12-23 15:16:21 +00:00
|
|
|
if LevelControl.cluster_id in self._in_clusters:
|
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
|
2018-12-23 15:16:21 +00:00
|
|
|
self._in_listeners.update({
|
|
|
|
LevelControl.cluster_id: LevelListener(
|
|
|
|
self,
|
|
|
|
self._in_clusters[LevelControl.cluster_id]
|
|
|
|
)
|
|
|
|
})
|
|
|
|
import zigpy.zcl.clusters as zcl_clusters
|
2017-07-11 04:16:44 +00:00
|
|
|
if zcl_clusters.lighting.Color.cluster_id in self._in_clusters:
|
2018-01-11 21:56:00 +00:00
|
|
|
color_capabilities = kwargs['color_capabilities']
|
|
|
|
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
|
|
|
|
2018-12-19 13:52:20 +00:00
|
|
|
@property
|
|
|
|
def zcl_reporting_config(self) -> dict:
|
|
|
|
"""Return attribute reporting configuration."""
|
|
|
|
return {
|
|
|
|
'on_off': {'on_off': REPORT_CONFIG_IMMEDIATE},
|
|
|
|
'level': {'current_level': REPORT_CONFIG_ASAP},
|
|
|
|
'light_color': {
|
|
|
|
'current_x': REPORT_CONFIG_DEFAULT,
|
|
|
|
'current_y': REPORT_CONFIG_DEFAULT,
|
|
|
|
'color_temperature': REPORT_CONFIG_DEFAULT,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
return bool(self._state)
|
|
|
|
|
2018-12-23 15:16:21 +00:00
|
|
|
def set_state(self, state):
|
|
|
|
"""Set the state."""
|
|
|
|
self._state = state
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
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."""
|
2018-09-20 18:23:09 +00:00
|
|
|
from zigpy.exceptions import DeliveryError
|
|
|
|
|
2017-07-26 15:22:31 +00:00
|
|
|
duration = kwargs.get(light.ATTR_TRANSITION, DEFAULT_DURATION)
|
|
|
|
duration = duration * 10 # tenths of s
|
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]
|
2018-09-20 18:23:09 +00:00
|
|
|
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
|
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:
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs_color = kwargs[light.ATTR_HS_COLOR]
|
|
|
|
xy_color = color_util.color_hs_to_xy(*self._hs_color)
|
2018-09-20 18:23:09 +00:00
|
|
|
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
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
if self._brightness is not None:
|
2017-07-26 15:22:31 +00:00
|
|
|
brightness = kwargs.get(
|
|
|
|
light.ATTR_BRIGHTNESS, self._brightness or 255)
|
2017-04-25 05:24:57 +00:00
|
|
|
# Move to level with on/off:
|
2018-09-20 18:23:09 +00:00
|
|
|
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
|
2017-04-25 05:24:57 +00:00
|
|
|
self._state = 1
|
2019-01-16 00:12:23 +00:00
|
|
|
self._brightness = brightness
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-04-25 05:24:57 +00:00
|
|
|
return
|
2018-09-20 18:23:09 +00:00
|
|
|
|
2018-03-19 21:12:53 +00:00
|
|
|
try:
|
2018-09-20 18:23:09 +00:00
|
|
|
res = await self._endpoint.on_off.on()
|
|
|
|
_LOGGER.debug("%s was turned on: %s", self.entity_id, res)
|
2018-03-19 21:12:53 +00:00
|
|
|
except DeliveryError as ex:
|
2018-09-20 18:23:09 +00:00
|
|
|
_LOGGER.error("%s: Unable to turn the light on: %s",
|
|
|
|
self.entity_id, ex)
|
2018-03-19 21:12:53 +00:00
|
|
|
return
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
self._state = 1
|
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-03-19 21:12:53 +00:00
|
|
|
from zigpy.exceptions import DeliveryError
|
2018-12-23 11:15:54 +00:00
|
|
|
duration = kwargs.get(light.ATTR_TRANSITION)
|
2018-03-19 21:12:53 +00:00
|
|
|
try:
|
2018-12-23 11:15:54 +00:00
|
|
|
supports_level = self.supported_features & light.SUPPORT_BRIGHTNESS
|
|
|
|
if duration and supports_level:
|
|
|
|
res = await self._endpoint.level.move_to_level_with_on_off(
|
|
|
|
0, duration*10
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
res = await self._endpoint.on_off.off()
|
2018-09-20 18:23:09 +00:00
|
|
|
_LOGGER.debug("%s was turned off: %s", self.entity_id, res)
|
2018-03-19 21:12:53 +00:00
|
|
|
except DeliveryError as ex:
|
2018-09-20 18:23:09 +00:00
|
|
|
_LOGGER.error("%s: Unable to turn the light off: %s",
|
|
|
|
self.entity_id, ex)
|
2018-03-19 21:12:53 +00:00
|
|
|
return
|
|
|
|
|
2017-04-25 05:24:57 +00:00
|
|
|
self._state = 0
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..255."""
|
|
|
|
return self._brightness
|
|
|
|
|
2018-12-23 15:16:21 +00:00
|
|
|
def set_level(self, value):
|
|
|
|
"""Set the brightness of this light between 0..255."""
|
|
|
|
if value < 0 or value > 255:
|
|
|
|
return
|
|
|
|
self._brightness = value
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
2017-04-25 05:24:57 +00:00
|
|
|
@property
|
2018-03-18 22:00:29 +00:00
|
|
|
def hs_color(self):
|
|
|
|
"""Return the hs color value [int, int]."""
|
|
|
|
return self._hs_color
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
@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-07-07 05:59:17 +00:00
|
|
|
|
2018-03-12 20:57:13 +00:00
|
|
|
async def async_update(self):
|
2017-07-07 05:59:17 +00:00
|
|
|
"""Retrieve latest state."""
|
2018-11-22 18:00:46 +00:00
|
|
|
result = await helpers.safe_read(self._endpoint.on_off, ['on_off'],
|
|
|
|
allow_cache=False,
|
|
|
|
only_cache=(not self._initialized))
|
2017-07-07 05:59:17 +00:00
|
|
|
self._state = result.get('on_off', self._state)
|
|
|
|
|
|
|
|
if self._supported_features & light.SUPPORT_BRIGHTNESS:
|
2018-11-22 18:00:46 +00:00
|
|
|
result = await helpers.safe_read(self._endpoint.level,
|
|
|
|
['current_level'],
|
|
|
|
allow_cache=False,
|
|
|
|
only_cache=(
|
|
|
|
not self._initialized
|
|
|
|
))
|
2017-07-07 05:59:17 +00:00
|
|
|
self._brightness = result.get('current_level', self._brightness)
|
|
|
|
|
|
|
|
if self._supported_features & light.SUPPORT_COLOR_TEMP:
|
2018-11-22 18:00:46 +00:00
|
|
|
result = await helpers.safe_read(self._endpoint.light_color,
|
|
|
|
['color_temperature'],
|
|
|
|
allow_cache=False,
|
|
|
|
only_cache=(
|
|
|
|
not self._initialized
|
|
|
|
))
|
2017-07-07 05:59:17 +00:00
|
|
|
self._color_temp = result.get('color_temperature',
|
|
|
|
self._color_temp)
|
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
if self._supported_features & light.SUPPORT_COLOR:
|
2018-11-22 18:00:46 +00:00
|
|
|
result = await helpers.safe_read(self._endpoint.light_color,
|
|
|
|
['current_x', 'current_y'],
|
|
|
|
allow_cache=False,
|
|
|
|
only_cache=(
|
|
|
|
not self._initialized
|
|
|
|
))
|
2017-07-07 05:59:17 +00:00
|
|
|
if 'current_x' in result and 'current_y' in result:
|
2018-05-28 14:32:47 +00:00
|
|
|
xy_color = (round(result['current_x']/65535, 3),
|
|
|
|
round(result['current_y']/65535, 3))
|
2018-03-18 22:00:29 +00:00
|
|
|
self._hs_color = color_util.color_xy_to_hs(*xy_color)
|