2019-02-13 21:21:14 +01:00
|
|
|
"""Support for deCONZ lights."""
|
2021-01-22 23:39:34 +01:00
|
|
|
|
2021-06-10 08:51:58 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-29 21:19:21 +02:00
|
|
|
from pydeconz.light import Light
|
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_EFFECT,
|
|
|
|
ATTR_FLASH,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
ATTR_TRANSITION,
|
2021-06-10 08:51:58 +02:00
|
|
|
ATTR_XY_COLOR,
|
|
|
|
COLOR_MODE_BRIGHTNESS,
|
|
|
|
COLOR_MODE_COLOR_TEMP,
|
|
|
|
COLOR_MODE_HS,
|
|
|
|
COLOR_MODE_ONOFF,
|
|
|
|
COLOR_MODE_XY,
|
2020-09-25 22:49:28 +02:00
|
|
|
DOMAIN,
|
2019-07-31 12:25:30 -07:00
|
|
|
EFFECT_COLORLOOP,
|
|
|
|
FLASH_LONG,
|
|
|
|
FLASH_SHORT,
|
|
|
|
SUPPORT_EFFECT,
|
|
|
|
SUPPORT_FLASH,
|
|
|
|
SUPPORT_TRANSITION,
|
2020-04-26 18:49:41 +02:00
|
|
|
LightEntity,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2018-01-01 17:08:13 +01:00
|
|
|
from homeassistant.core import callback
|
2018-05-05 16:11:00 +02:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-07-12 08:17:50 +02:00
|
|
|
from homeassistant.util.color import color_hs_to_xy
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2021-09-29 21:19:21 +02:00
|
|
|
from .const import DOMAIN as DECONZ_DOMAIN, NEW_GROUP, NEW_LIGHT, POWER_PLUGS
|
2019-01-16 08:33:04 +01:00
|
|
|
from .deconz_device import DeconzDevice
|
2020-02-05 01:37:01 +01:00
|
|
|
from .gateway import get_gateway_from_config_entry
|
2019-01-15 19:29:56 +01:00
|
|
|
|
2021-06-22 20:34:25 +02:00
|
|
|
DECONZ_GROUP = "is_deconz_group"
|
2020-11-23 11:37:11 +01:00
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2018-05-05 16:11:00 +02:00
|
|
|
"""Set up the deCONZ lights and groups from a config entry."""
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2020-09-25 22:49:28 +02:00
|
|
|
gateway.entities[DOMAIN] = set()
|
2018-11-05 16:21:44 +01:00
|
|
|
|
2018-05-05 16:11:00 +02:00
|
|
|
@callback
|
2020-12-02 16:21:27 +01:00
|
|
|
def async_add_light(lights=gateway.api.lights.values()):
|
2018-05-05 16:11:00 +02:00
|
|
|
"""Add light from deCONZ."""
|
|
|
|
entities = []
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2018-05-05 16:11:00 +02:00
|
|
|
for light in lights:
|
2020-09-25 22:49:28 +02:00
|
|
|
if (
|
2021-09-29 21:19:21 +02:00
|
|
|
isinstance(light, Light)
|
|
|
|
and light.type not in POWER_PLUGS
|
2021-09-18 09:05:08 +02:00
|
|
|
and light.unique_id not in gateway.entities[DOMAIN]
|
2020-09-25 22:49:28 +02:00
|
|
|
):
|
2018-11-05 16:21:44 +01:00
|
|
|
entities.append(DeconzLight(light, gateway))
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
if entities:
|
2020-10-16 17:14:26 +02:00
|
|
|
async_add_entities(entities)
|
2018-06-15 20:31:22 +02:00
|
|
|
|
2021-04-20 20:20:57 +02:00
|
|
|
config_entry.async_on_unload(
|
2019-07-31 12:25:30 -07:00
|
|
|
async_dispatcher_connect(
|
2019-09-06 01:38:00 +02:00
|
|
|
hass, gateway.async_signal_new_device(NEW_LIGHT), async_add_light
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
|
|
|
)
|
2018-05-05 16:11:00 +02:00
|
|
|
|
|
|
|
@callback
|
2020-12-02 16:21:27 +01:00
|
|
|
def async_add_group(groups=gateway.api.groups.values()):
|
2018-05-05 16:11:00 +02:00
|
|
|
"""Add group from deCONZ."""
|
2020-02-05 01:37:01 +01:00
|
|
|
if not gateway.option_allow_deconz_groups:
|
|
|
|
return
|
|
|
|
|
2018-05-05 16:11:00 +02:00
|
|
|
entities = []
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2018-05-05 16:11:00 +02:00
|
|
|
for group in groups:
|
2020-09-25 22:49:28 +02:00
|
|
|
if not group.lights:
|
|
|
|
continue
|
|
|
|
|
2020-09-27 11:02:45 +02:00
|
|
|
known_groups = set(gateway.entities[DOMAIN])
|
2020-09-25 22:49:28 +02:00
|
|
|
new_group = DeconzGroup(group, gateway)
|
|
|
|
if new_group.unique_id not in known_groups:
|
|
|
|
entities.append(new_group)
|
2019-04-05 02:48:24 +02:00
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
if entities:
|
2020-10-16 17:14:26 +02:00
|
|
|
async_add_entities(entities)
|
2018-06-15 20:31:22 +02:00
|
|
|
|
2021-04-20 20:20:57 +02:00
|
|
|
config_entry.async_on_unload(
|
2019-07-31 12:25:30 -07:00
|
|
|
async_dispatcher_connect(
|
2019-09-06 01:38:00 +02:00
|
|
|
hass, gateway.async_signal_new_device(NEW_GROUP), async_add_group
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
|
|
|
)
|
2018-05-05 16:11:00 +02:00
|
|
|
|
2020-12-02 16:21:27 +01:00
|
|
|
async_add_light()
|
|
|
|
async_add_group()
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
|
2020-06-04 10:15:30 +02:00
|
|
|
class DeconzBaseLight(DeconzDevice, LightEntity):
|
2018-01-01 17:08:13 +01:00
|
|
|
"""Representation of a deCONZ light."""
|
|
|
|
|
2020-09-25 22:49:28 +02:00
|
|
|
TYPE = DOMAIN
|
|
|
|
|
2019-01-16 08:33:04 +01:00
|
|
|
def __init__(self, device, gateway):
|
2019-07-25 18:39:38 +02:00
|
|
|
"""Set up light."""
|
2019-01-16 08:33:04 +01:00
|
|
|
super().__init__(device, gateway)
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2021-06-10 08:51:58 +02:00
|
|
|
self._attr_supported_color_modes = set()
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2021-09-18 09:05:08 +02:00
|
|
|
if device.color_temp is not None:
|
2021-06-22 19:29:58 +02:00
|
|
|
self._attr_supported_color_modes.add(COLOR_MODE_COLOR_TEMP)
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2021-09-18 09:05:08 +02:00
|
|
|
if device.hue is not None and device.saturation is not None:
|
2021-06-22 19:29:58 +02:00
|
|
|
self._attr_supported_color_modes.add(COLOR_MODE_HS)
|
2021-06-10 08:51:58 +02:00
|
|
|
|
|
|
|
if device.xy is not None:
|
2021-06-22 19:29:58 +02:00
|
|
|
self._attr_supported_color_modes.add(COLOR_MODE_XY)
|
2021-06-10 08:51:58 +02:00
|
|
|
|
2021-06-22 19:29:58 +02:00
|
|
|
if not self._attr_supported_color_modes and device.brightness is not None:
|
|
|
|
self._attr_supported_color_modes.add(COLOR_MODE_BRIGHTNESS)
|
2021-06-10 08:51:58 +02:00
|
|
|
|
2021-06-22 19:29:58 +02:00
|
|
|
if not self._attr_supported_color_modes:
|
|
|
|
self._attr_supported_color_modes.add(COLOR_MODE_ONOFF)
|
2021-06-10 08:51:58 +02:00
|
|
|
|
|
|
|
if device.brightness is not None:
|
|
|
|
self._attr_supported_features |= SUPPORT_FLASH
|
|
|
|
self._attr_supported_features |= SUPPORT_TRANSITION
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2021-01-22 23:39:34 +01:00
|
|
|
if device.effect is not None:
|
2021-06-07 12:50:08 +02:00
|
|
|
self._attr_supported_features |= SUPPORT_EFFECT
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2021-06-10 08:51:58 +02:00
|
|
|
@property
|
|
|
|
def color_mode(self) -> str:
|
|
|
|
"""Return the color mode of the light."""
|
2021-09-18 09:05:08 +02:00
|
|
|
if self._device.color_mode == "ct":
|
2021-06-10 08:51:58 +02:00
|
|
|
color_mode = COLOR_MODE_COLOR_TEMP
|
2021-09-18 09:05:08 +02:00
|
|
|
elif self._device.color_mode == "hs":
|
2021-06-10 08:51:58 +02:00
|
|
|
color_mode = COLOR_MODE_HS
|
2021-09-18 09:05:08 +02:00
|
|
|
elif self._device.color_mode == "xy":
|
2021-06-10 08:51:58 +02:00
|
|
|
color_mode = COLOR_MODE_XY
|
|
|
|
elif self._device.brightness is not None:
|
|
|
|
color_mode = COLOR_MODE_BRIGHTNESS
|
|
|
|
else:
|
|
|
|
color_mode = COLOR_MODE_ONOFF
|
|
|
|
return color_mode
|
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..255."""
|
2019-01-16 08:33:04 +01:00
|
|
|
return self._device.brightness
|
2018-01-01 17:08:13 +01: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."""
|
2021-09-18 09:05:08 +02:00
|
|
|
return self._device.color_temp
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
@property
|
2021-06-10 08:51:58 +02:00
|
|
|
def hs_color(self) -> tuple:
|
2018-06-24 23:48:59 +02:00
|
|
|
"""Return the hs color value."""
|
2021-09-18 09:05:08 +02:00
|
|
|
return (self._device.hue / 65535 * 360, self._device.saturation / 255 * 100)
|
2021-06-10 08:51:58 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def xy_color(self) -> tuple | None:
|
|
|
|
"""Return the XY color value."""
|
|
|
|
return self._device.xy
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if light is on."""
|
2019-01-16 08:33:04 +01:00
|
|
|
return self._device.state
|
2018-01-30 23:42:24 +01:00
|
|
|
|
2018-03-13 08:47:45 +01:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2018-01-01 17:08:13 +01:00
|
|
|
"""Turn on light."""
|
2019-07-31 12:25:30 -07:00
|
|
|
data = {"on": True}
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2021-06-10 08:51:58 +02:00
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
2021-09-18 09:05:08 +02:00
|
|
|
data["brightness"] = kwargs[ATTR_BRIGHTNESS]
|
2021-06-10 08:51:58 +02:00
|
|
|
|
2018-01-01 17:08:13 +01:00
|
|
|
if ATTR_COLOR_TEMP in kwargs:
|
2021-09-18 09:05:08 +02:00
|
|
|
data["color_temperature"] = kwargs[ATTR_COLOR_TEMP]
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2018-03-18 18:00:29 -04:00
|
|
|
if ATTR_HS_COLOR in kwargs:
|
2021-07-12 08:17:50 +02:00
|
|
|
if COLOR_MODE_XY in self._attr_supported_color_modes:
|
|
|
|
data["xy"] = color_hs_to_xy(*kwargs[ATTR_HS_COLOR])
|
|
|
|
else:
|
|
|
|
data["hue"] = int(kwargs[ATTR_HS_COLOR][0] / 360 * 65535)
|
2021-09-18 09:05:08 +02:00
|
|
|
data["saturation"] = int(kwargs[ATTR_HS_COLOR][1] / 100 * 255)
|
2018-02-03 17:08:00 +01:00
|
|
|
|
2021-06-10 08:51:58 +02:00
|
|
|
if ATTR_XY_COLOR in kwargs:
|
|
|
|
data["xy"] = kwargs[ATTR_XY_COLOR]
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
2021-09-18 09:05:08 +02:00
|
|
|
data["transition_time"] = int(kwargs[ATTR_TRANSITION] * 10)
|
2020-02-02 19:07:20 +01:00
|
|
|
elif "IKEA" in self._device.manufacturer:
|
2021-09-18 09:05:08 +02:00
|
|
|
data["transition_time"] = 0
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
if ATTR_FLASH in kwargs:
|
|
|
|
if kwargs[ATTR_FLASH] == FLASH_SHORT:
|
2019-07-31 12:25:30 -07:00
|
|
|
data["alert"] = "select"
|
|
|
|
del data["on"]
|
2018-01-01 17:08:13 +01:00
|
|
|
elif kwargs[ATTR_FLASH] == FLASH_LONG:
|
2019-07-31 12:25:30 -07:00
|
|
|
data["alert"] = "lselect"
|
|
|
|
del data["on"]
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
if ATTR_EFFECT in kwargs:
|
|
|
|
if kwargs[ATTR_EFFECT] == EFFECT_COLORLOOP:
|
2019-07-31 12:25:30 -07:00
|
|
|
data["effect"] = "colorloop"
|
2018-01-01 17:08:13 +01:00
|
|
|
else:
|
2019-07-31 12:25:30 -07:00
|
|
|
data["effect"] = "none"
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2021-09-18 09:05:08 +02:00
|
|
|
await self._device.set_state(**data)
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2018-03-13 08:47:45 +01:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2018-01-01 17:08:13 +01:00
|
|
|
"""Turn off light."""
|
2020-06-02 00:20:52 +02:00
|
|
|
if not self._device.state:
|
|
|
|
return
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
data = {"on": False}
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
2021-09-18 09:05:08 +02:00
|
|
|
data["brightness"] = 0
|
|
|
|
data["transition_time"] = int(kwargs[ATTR_TRANSITION] * 10)
|
2018-01-01 17:08:13 +01:00
|
|
|
|
|
|
|
if ATTR_FLASH in kwargs:
|
|
|
|
if kwargs[ATTR_FLASH] == FLASH_SHORT:
|
2019-07-31 12:25:30 -07:00
|
|
|
data["alert"] = "select"
|
|
|
|
del data["on"]
|
2018-01-01 17:08:13 +01:00
|
|
|
elif kwargs[ATTR_FLASH] == FLASH_LONG:
|
2019-07-31 12:25:30 -07:00
|
|
|
data["alert"] = "lselect"
|
|
|
|
del data["on"]
|
2018-01-01 17:08:13 +01:00
|
|
|
|
2021-09-18 09:05:08 +02:00
|
|
|
await self._device.set_state(**data)
|
2018-08-01 11:03:08 +02:00
|
|
|
|
|
|
|
@property
|
2021-03-11 16:51:03 +01:00
|
|
|
def extra_state_attributes(self):
|
2018-08-01 11:03:08 +02:00
|
|
|
"""Return the device state attributes."""
|
2021-06-22 20:34:25 +02:00
|
|
|
return {DECONZ_GROUP: self._device.type == "LightGroup"}
|
2019-07-25 18:39:38 +02:00
|
|
|
|
|
|
|
|
2020-06-04 10:15:30 +02:00
|
|
|
class DeconzLight(DeconzBaseLight):
|
|
|
|
"""Representation of a deCONZ light."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def max_mireds(self):
|
|
|
|
"""Return the warmest color_temp that this light supports."""
|
2021-09-18 09:05:08 +02:00
|
|
|
return self._device.max_color_temp or super().max_mireds
|
2020-06-04 10:15:30 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def min_mireds(self):
|
|
|
|
"""Return the coldest color_temp that this light supports."""
|
2021-09-18 09:05:08 +02:00
|
|
|
return self._device.min_color_temp or super().min_mireds
|
2020-06-04 10:15:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DeconzGroup(DeconzBaseLight):
|
2019-07-25 18:39:38 +02:00
|
|
|
"""Representation of a deCONZ group."""
|
|
|
|
|
|
|
|
def __init__(self, device, gateway):
|
|
|
|
"""Set up group and create an unique id."""
|
2021-02-06 14:32:17 +01:00
|
|
|
self._unique_id = f"{gateway.bridgeid}-{device.deconz_id}"
|
2020-09-25 22:49:28 +02:00
|
|
|
super().__init__(device, gateway)
|
2019-07-25 18:39:38 +02: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."""
|
|
|
|
return {
|
2019-07-31 12:25:30 -07:00
|
|
|
"identifiers": {(DECONZ_DOMAIN, self.unique_id)},
|
|
|
|
"manufacturer": "Dresden Elektronik",
|
|
|
|
"model": "deCONZ group",
|
|
|
|
"name": self._device.name,
|
2021-09-18 09:05:08 +02:00
|
|
|
"via_device": (DECONZ_DOMAIN, self.gateway.api.config.bridge_id),
|
2019-07-25 18:39:38 +02:00
|
|
|
}
|
2019-09-18 12:47:26 +02:00
|
|
|
|
|
|
|
@property
|
2021-03-11 16:51:03 +01:00
|
|
|
def extra_state_attributes(self):
|
2019-09-18 12:47:26 +02:00
|
|
|
"""Return the device state attributes."""
|
2021-03-11 16:51:03 +01:00
|
|
|
attributes = dict(super().extra_state_attributes)
|
2019-09-18 12:47:26 +02:00
|
|
|
attributes["all_on"] = self._device.all_on
|
|
|
|
|
|
|
|
return attributes
|