2019-02-13 20:21:14 +00:00
|
|
|
"""Support for deCONZ lights."""
|
2018-01-01 16:08:13 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_EFFECT,
|
|
|
|
ATTR_FLASH,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
ATTR_TRANSITION,
|
2020-09-25 20:49:28 +00:00
|
|
|
DOMAIN,
|
2019-07-31 19:25:30 +00:00
|
|
|
EFFECT_COLORLOOP,
|
|
|
|
FLASH_LONG,
|
|
|
|
FLASH_SHORT,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
|
|
|
SUPPORT_COLOR_TEMP,
|
|
|
|
SUPPORT_EFFECT,
|
|
|
|
SUPPORT_FLASH,
|
|
|
|
SUPPORT_TRANSITION,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-01-01 16:08:13 +00:00
|
|
|
from homeassistant.core import callback
|
2018-05-05 14:11:00 +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
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-07-25 16:39:38 +00:00
|
|
|
from .const import (
|
2020-01-17 23:28:34 +00:00
|
|
|
CONF_GROUP_ID_BASE,
|
2019-07-31 19:25:30 +00:00
|
|
|
COVER_TYPES,
|
|
|
|
DOMAIN as DECONZ_DOMAIN,
|
2020-10-01 07:50:06 +00:00
|
|
|
LOCK_TYPES,
|
2019-07-31 19:25:30 +00:00
|
|
|
NEW_GROUP,
|
|
|
|
NEW_LIGHT,
|
|
|
|
SWITCH_TYPES,
|
|
|
|
)
|
2019-01-16 07:33:04 +00:00
|
|
|
from .deconz_device import DeconzDevice
|
2020-02-05 00:37:01 +00:00
|
|
|
from .gateway import get_gateway_from_config_entry
|
2019-01-15 18:29:56 +00:00
|
|
|
|
2020-11-23 10:37:11 +00:00
|
|
|
CONTROLLER = ["Configuration tool"]
|
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2018-05-05 14:11:00 +00:00
|
|
|
"""Set up the deCONZ lights and groups from a config entry."""
|
2019-04-05 00:48:24 +00:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2020-09-25 20:49:28 +00:00
|
|
|
gateway.entities[DOMAIN] = set()
|
2018-11-05 15:21:44 +00:00
|
|
|
|
2020-11-23 10:37:11 +00:00
|
|
|
other_light_resource_types = CONTROLLER + COVER_TYPES + LOCK_TYPES + SWITCH_TYPES
|
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
@callback
|
|
|
|
def async_add_light(lights):
|
|
|
|
"""Add light from deCONZ."""
|
|
|
|
entities = []
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
for light in lights:
|
2020-09-25 20:49:28 +00:00
|
|
|
if (
|
2020-11-23 10:37:11 +00:00
|
|
|
light.type not in other_light_resource_types
|
2020-09-25 20:49:28 +00:00
|
|
|
and light.uniqueid not in gateway.entities[DOMAIN]
|
|
|
|
):
|
2018-11-05 15:21:44 +00:00
|
|
|
entities.append(DeconzLight(light, gateway))
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-10-02 09:20:33 +00:00
|
|
|
if entities:
|
2020-10-16 15:14:26 +00:00
|
|
|
async_add_entities(entities)
|
2018-06-15 18:31:22 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
gateway.listeners.append(
|
|
|
|
async_dispatcher_connect(
|
2019-09-05 23:38:00 +00:00
|
|
|
hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_light
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2018-05-05 14:11:00 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_group(groups):
|
|
|
|
"""Add group from deCONZ."""
|
2020-02-05 00:37:01 +00:00
|
|
|
if not gateway.option_allow_deconz_groups:
|
|
|
|
return
|
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
entities = []
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2018-05-05 14:11:00 +00:00
|
|
|
for group in groups:
|
2020-09-25 20:49:28 +00:00
|
|
|
if not group.lights:
|
|
|
|
continue
|
|
|
|
|
2020-09-27 09:02:45 +00:00
|
|
|
known_groups = set(gateway.entities[DOMAIN])
|
2020-09-25 20:49:28 +00:00
|
|
|
new_group = DeconzGroup(group, gateway)
|
|
|
|
if new_group.unique_id not in known_groups:
|
|
|
|
entities.append(new_group)
|
2019-04-05 00:48:24 +00:00
|
|
|
|
2020-10-02 09:20:33 +00:00
|
|
|
if entities:
|
2020-10-16 15:14:26 +00:00
|
|
|
async_add_entities(entities)
|
2018-06-15 18:31:22 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
gateway.listeners.append(
|
|
|
|
async_dispatcher_connect(
|
2019-09-05 23:38:00 +00:00
|
|
|
hass, gateway.async_signal_new_device(NEW_GROUP), async_add_group
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2018-05-05 14:11:00 +00:00
|
|
|
|
2018-11-05 15:21:44 +00:00
|
|
|
async_add_light(gateway.api.lights.values())
|
|
|
|
async_add_group(gateway.api.groups.values())
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
|
2020-06-04 08:15:30 +00:00
|
|
|
class DeconzBaseLight(DeconzDevice, LightEntity):
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Representation of a deCONZ light."""
|
|
|
|
|
2020-09-25 20:49:28 +00:00
|
|
|
TYPE = DOMAIN
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
def __init__(self, device, gateway):
|
2019-07-25 16:39:38 +00:00
|
|
|
"""Set up light."""
|
2019-01-16 07:33:04 +00:00
|
|
|
super().__init__(device, gateway)
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2020-02-01 16:08:49 +00:00
|
|
|
self._features = 0
|
|
|
|
|
|
|
|
if self._device.brightness is not None:
|
|
|
|
self._features |= SUPPORT_BRIGHTNESS
|
|
|
|
self._features |= SUPPORT_FLASH
|
|
|
|
self._features |= SUPPORT_TRANSITION
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
if self._device.ct is not None:
|
2018-01-01 16:08:13 +00:00
|
|
|
self._features |= SUPPORT_COLOR_TEMP
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
if self._device.xy is not None:
|
2018-03-18 22:00:29 +00:00
|
|
|
self._features |= SUPPORT_COLOR
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
if self._device.effect is not None:
|
2018-01-01 16:08:13 +00:00
|
|
|
self._features |= SUPPORT_EFFECT
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..255."""
|
2019-01-16 07:33:04 +00:00
|
|
|
return self._device.brightness
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def effect_list(self):
|
|
|
|
"""Return the list of supported effects."""
|
|
|
|
return [EFFECT_COLORLOOP]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def color_temp(self):
|
|
|
|
"""Return the CT color value."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._device.colormode != "ct":
|
2018-08-03 11:56:54 +00:00
|
|
|
return None
|
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
return self._device.ct
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
2018-06-24 21:48:59 +00:00
|
|
|
def hs_color(self):
|
|
|
|
"""Return the hs color value."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._device.colormode in ("xy", "hs") and self._device.xy:
|
2019-01-16 07:33:04 +00:00
|
|
|
return color_util.color_xy_to_hs(*self._device.xy)
|
2018-06-24 21:48:59 +00:00
|
|
|
return None
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if light is on."""
|
2019-01-16 07:33:04 +00:00
|
|
|
return self._device.state
|
2018-01-30 22:42:24 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return self._features
|
|
|
|
|
2018-03-13 07:47:45 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Turn on light."""
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"on": True}
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["ct"] = kwargs[ATTR_COLOR_TEMP]
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2018-03-18 22:00:29 +00:00
|
|
|
if ATTR_HS_COLOR in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["xy"] = color_util.color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
|
2018-02-03 16:08:00 +00:00
|
|
|
|
2018-01-01 16:08:13 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["bri"] = kwargs[ATTR_BRIGHTNESS]
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["transitiontime"] = int(kwargs[ATTR_TRANSITION] * 10)
|
2020-02-02 18:07:20 +00:00
|
|
|
elif "IKEA" in self._device.manufacturer:
|
2019-11-22 14:32:05 +00:00
|
|
|
data["transitiontime"] = 0
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if ATTR_FLASH in kwargs:
|
|
|
|
if kwargs[ATTR_FLASH] == FLASH_SHORT:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["alert"] = "select"
|
|
|
|
del data["on"]
|
2018-01-01 16:08:13 +00:00
|
|
|
elif kwargs[ATTR_FLASH] == FLASH_LONG:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["alert"] = "lselect"
|
|
|
|
del data["on"]
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if ATTR_EFFECT in kwargs:
|
|
|
|
if kwargs[ATTR_EFFECT] == EFFECT_COLORLOOP:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["effect"] = "colorloop"
|
2018-01-01 16:08:13 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["effect"] = "none"
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
await self._device.async_set_state(data)
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2018-03-13 07:47:45 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2018-01-01 16:08:13 +00:00
|
|
|
"""Turn off light."""
|
2020-06-01 22:20:52 +00:00
|
|
|
if not self._device.state:
|
|
|
|
return
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {"on": False}
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["bri"] = 0
|
|
|
|
data["transitiontime"] = int(kwargs[ATTR_TRANSITION] * 10)
|
2018-01-01 16:08:13 +00:00
|
|
|
|
|
|
|
if ATTR_FLASH in kwargs:
|
|
|
|
if kwargs[ATTR_FLASH] == FLASH_SHORT:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["alert"] = "select"
|
|
|
|
del data["on"]
|
2018-01-01 16:08:13 +00:00
|
|
|
elif kwargs[ATTR_FLASH] == FLASH_LONG:
|
2019-07-31 19:25:30 +00:00
|
|
|
data["alert"] = "lselect"
|
|
|
|
del data["on"]
|
2018-01-01 16:08:13 +00:00
|
|
|
|
2019-01-16 07:33:04 +00:00
|
|
|
await self._device.async_set_state(data)
|
2018-08-01 09:03:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device state attributes."""
|
2020-10-06 14:55:16 +00:00
|
|
|
return {"is_deconz_group": self._device.type == "LightGroup"}
|
2019-07-25 16:39:38 +00:00
|
|
|
|
|
|
|
|
2020-06-04 08:15:30 +00:00
|
|
|
class DeconzLight(DeconzBaseLight):
|
|
|
|
"""Representation of a deCONZ light."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_mireds(self):
|
|
|
|
"""Return the warmest color_temp that this light supports."""
|
|
|
|
return self._device.ctmax or super().max_mireds
|
|
|
|
|
|
|
|
@property
|
|
|
|
def min_mireds(self):
|
|
|
|
"""Return the coldest color_temp that this light supports."""
|
|
|
|
return self._device.ctmin or super().min_mireds
|
|
|
|
|
|
|
|
|
|
|
|
class DeconzGroup(DeconzBaseLight):
|
2019-07-25 16:39:38 +00:00
|
|
|
"""Representation of a deCONZ group."""
|
|
|
|
|
|
|
|
def __init__(self, device, gateway):
|
|
|
|
"""Set up group and create an unique id."""
|
2020-09-25 20:49:28 +00:00
|
|
|
group_id_base = gateway.config_entry.unique_id
|
|
|
|
if CONF_GROUP_ID_BASE in gateway.config_entry.data:
|
|
|
|
group_id_base = gateway.config_entry.data[CONF_GROUP_ID_BASE]
|
|
|
|
self._unique_id = f"{group_id_base}-{device.deconz_id}"
|
2019-07-25 16:39:38 +00:00
|
|
|
|
2020-09-25 20:49:28 +00:00
|
|
|
super().__init__(device, gateway)
|
2019-07-25 16:39:38 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique identifier for this device."""
|
|
|
|
return self._unique_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return a device description for device registry."""
|
|
|
|
bridgeid = self.gateway.api.config.bridgeid
|
|
|
|
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"identifiers": {(DECONZ_DOMAIN, self.unique_id)},
|
|
|
|
"manufacturer": "Dresden Elektronik",
|
|
|
|
"model": "deCONZ group",
|
|
|
|
"name": self._device.name,
|
|
|
|
"via_device": (DECONZ_DOMAIN, bridgeid),
|
2019-07-25 16:39:38 +00:00
|
|
|
}
|
2019-09-18 10:47:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device state attributes."""
|
|
|
|
attributes = dict(super().device_state_attributes)
|
|
|
|
attributes["all_on"] = self._device.all_on
|
|
|
|
|
|
|
|
return attributes
|