2019-02-14 04:35:12 +00:00
|
|
|
"""Support for KNX/IP lights."""
|
2021-03-19 09:21:06 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-04-20 15:40:41 +00:00
|
|
|
from collections.abc import Iterable
|
|
|
|
from typing import Any, Callable
|
2021-03-19 09:21:06 +00:00
|
|
|
|
2019-10-22 05:38:21 +00:00
|
|
|
from xknx.devices import Light as XknxLight
|
2021-05-03 09:12:06 +00:00
|
|
|
from xknx.telegram.address import parse_device_group_address
|
2017-04-30 04:20:46 +00:00
|
|
|
|
2018-01-21 06:35:38 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_COLOR_TEMP,
|
|
|
|
ATTR_HS_COLOR,
|
|
|
|
ATTR_WHITE_VALUE,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_COLOR,
|
|
|
|
SUPPORT_COLOR_TEMP,
|
|
|
|
SUPPORT_WHITE_VALUE,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-05-03 09:12:06 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers import entity_registry as er
|
2021-03-19 09:21:06 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2021-03-27 21:20:11 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2018-03-18 22:00:29 +00:00
|
|
|
import homeassistant.util.color as color_util
|
2017-04-30 04:20:46 +00:00
|
|
|
|
2021-05-03 09:12:06 +00:00
|
|
|
from .const import DOMAIN, KNX_ADDRESS
|
2020-09-21 16:08:35 +00:00
|
|
|
from .knx_entity import KnxEntity
|
2021-03-19 09:21:06 +00:00
|
|
|
from .schema import LightSchema
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_COLOR = (0.0, 0.0)
|
2019-02-08 07:28:52 +00:00
|
|
|
DEFAULT_BRIGHTNESS = 255
|
2019-07-11 20:01:37 +00:00
|
|
|
DEFAULT_WHITE_VALUE = 255
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
|
2021-03-19 09:21:06 +00:00
|
|
|
async def async_setup_platform(
|
2021-03-27 21:20:11 +00:00
|
|
|
hass: HomeAssistant,
|
2021-03-19 09:21:06 +00:00
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: Callable[[Iterable[Entity]], None],
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Set up lights for KNX platform."""
|
2021-05-03 09:12:06 +00:00
|
|
|
_async_migrate_unique_id(hass, discovery_info)
|
2017-09-07 07:11:55 +00:00
|
|
|
entities = []
|
2020-09-21 16:08:35 +00:00
|
|
|
for device in hass.data[DOMAIN].xknx.devices:
|
2020-08-30 18:13:47 +00:00
|
|
|
if isinstance(device, XknxLight):
|
|
|
|
entities.append(KNXLight(device))
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(entities)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
|
2021-05-03 09:12:06 +00:00
|
|
|
@callback
|
|
|
|
def _async_migrate_unique_id(
|
|
|
|
hass: HomeAssistant, discovery_info: DiscoveryInfoType | None
|
|
|
|
) -> None:
|
|
|
|
"""Change unique_ids used in 2021.4 to exchange individual color switch address for brightness address."""
|
|
|
|
entity_registry = er.async_get(hass)
|
|
|
|
if not discovery_info or not discovery_info["platform_config"]:
|
|
|
|
return
|
|
|
|
|
|
|
|
platform_config = discovery_info["platform_config"]
|
|
|
|
for entity_config in platform_config:
|
|
|
|
individual_colors_config = entity_config.get(LightSchema.CONF_INDIVIDUAL_COLORS)
|
|
|
|
if individual_colors_config is None:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
ga_red_switch = individual_colors_config[LightSchema.CONF_RED][KNX_ADDRESS][
|
|
|
|
0
|
|
|
|
]
|
|
|
|
ga_green_switch = individual_colors_config[LightSchema.CONF_GREEN][
|
|
|
|
KNX_ADDRESS
|
|
|
|
][0]
|
|
|
|
ga_blue_switch = individual_colors_config[LightSchema.CONF_BLUE][
|
|
|
|
KNX_ADDRESS
|
|
|
|
][0]
|
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
# normalize group address strings
|
|
|
|
ga_red_switch = parse_device_group_address(ga_red_switch)
|
|
|
|
ga_green_switch = parse_device_group_address(ga_green_switch)
|
|
|
|
ga_blue_switch = parse_device_group_address(ga_blue_switch)
|
|
|
|
# white config is optional so it has to be checked for `None` extra
|
|
|
|
white_config = individual_colors_config.get(LightSchema.CONF_WHITE)
|
|
|
|
white_switch = (
|
|
|
|
white_config.get(KNX_ADDRESS) if white_config is not None else None
|
|
|
|
)
|
|
|
|
ga_white_switch = (
|
|
|
|
parse_device_group_address(white_switch[0])
|
|
|
|
if white_switch is not None
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
|
|
|
|
old_uid = (
|
|
|
|
f"{ga_red_switch}_"
|
|
|
|
f"{ga_green_switch}_"
|
|
|
|
f"{ga_blue_switch}_"
|
|
|
|
f"{ga_white_switch}"
|
|
|
|
)
|
|
|
|
entity_id = entity_registry.async_get_entity_id("light", DOMAIN, old_uid)
|
|
|
|
if entity_id is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
ga_red_brightness = parse_device_group_address(
|
|
|
|
individual_colors_config[LightSchema.CONF_RED][
|
|
|
|
LightSchema.CONF_BRIGHTNESS_ADDRESS
|
|
|
|
][0]
|
|
|
|
)
|
|
|
|
ga_green_brightness = parse_device_group_address(
|
|
|
|
individual_colors_config[LightSchema.CONF_GREEN][
|
|
|
|
LightSchema.CONF_BRIGHTNESS_ADDRESS
|
|
|
|
][0]
|
|
|
|
)
|
|
|
|
ga_blue_brightness = parse_device_group_address(
|
|
|
|
individual_colors_config[LightSchema.CONF_BLUE][
|
|
|
|
LightSchema.CONF_BRIGHTNESS_ADDRESS
|
|
|
|
][0]
|
|
|
|
)
|
|
|
|
|
|
|
|
new_uid = f"{ga_red_brightness}_{ga_green_brightness}_{ga_blue_brightness}"
|
|
|
|
entity_registry.async_update_entity(entity_id, new_unique_id=new_uid)
|
|
|
|
|
|
|
|
|
2020-09-21 16:08:35 +00:00
|
|
|
class KNXLight(KnxEntity, LightEntity):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Representation of a KNX light."""
|
|
|
|
|
2021-03-19 09:21:06 +00:00
|
|
|
def __init__(self, device: XknxLight) -> None:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize of KNX light."""
|
2021-03-19 09:21:06 +00:00
|
|
|
self._device: XknxLight
|
2020-09-21 16:08:35 +00:00
|
|
|
super().__init__(device)
|
2021-04-28 13:50:01 +00:00
|
|
|
self._unique_id = self._device_unique_id()
|
2021-03-19 09:21:06 +00:00
|
|
|
self._min_kelvin = device.min_kelvin or LightSchema.DEFAULT_MIN_KELVIN
|
|
|
|
self._max_kelvin = device.max_kelvin or LightSchema.DEFAULT_MAX_KELVIN
|
2019-07-31 19:25:30 +00:00
|
|
|
self._min_mireds = color_util.color_temperature_kelvin_to_mired(
|
|
|
|
self._max_kelvin
|
|
|
|
)
|
|
|
|
self._max_mireds = color_util.color_temperature_kelvin_to_mired(
|
|
|
|
self._min_kelvin
|
|
|
|
)
|
2019-02-08 07:28:52 +00:00
|
|
|
|
2021-04-28 13:50:01 +00:00
|
|
|
def _device_unique_id(self) -> str:
|
|
|
|
"""Return unique id for this device."""
|
|
|
|
if self._device.switch.group_address is not None:
|
|
|
|
return f"{self._device.switch.group_address}"
|
|
|
|
return (
|
2021-05-03 09:12:06 +00:00
|
|
|
f"{self._device.red.brightness.group_address}_"
|
|
|
|
f"{self._device.green.brightness.group_address}_"
|
|
|
|
f"{self._device.blue.brightness.group_address}"
|
2021-04-28 13:50:01 +00:00
|
|
|
)
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
@property
|
2021-03-19 09:21:06 +00:00
|
|
|
def brightness(self) -> int | None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Return the brightness of this light between 0..255."""
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.supports_brightness:
|
|
|
|
return self._device.current_brightness
|
2020-04-18 00:25:44 +00:00
|
|
|
hsv_color = self._hsv_color
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.supports_color and hsv_color:
|
2020-04-18 00:25:44 +00:00
|
|
|
return round(hsv_color[-1] / 100 * 255)
|
|
|
|
return None
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-19 09:21:06 +00:00
|
|
|
def hs_color(self) -> tuple[float, float] | None:
|
2018-03-18 22:00:29 +00:00
|
|
|
"""Return the HS color value."""
|
2021-03-19 09:21:06 +00:00
|
|
|
rgb: tuple[int, int, int] | None = None
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.supports_rgbw or self._device.supports_color:
|
|
|
|
rgb, _ = self._device.current_color
|
2019-07-11 20:01:37 +00:00
|
|
|
return color_util.color_RGB_to_hs(*rgb) if rgb else None
|
|
|
|
|
2020-04-18 00:25:44 +00:00
|
|
|
@property
|
2021-03-19 09:21:06 +00:00
|
|
|
def _hsv_color(self) -> tuple[float, float, float] | None:
|
2020-04-18 00:25:44 +00:00
|
|
|
"""Return the HSV color value."""
|
2021-03-19 09:21:06 +00:00
|
|
|
rgb: tuple[int, int, int] | None = None
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.supports_rgbw or self._device.supports_color:
|
|
|
|
rgb, _ = self._device.current_color
|
2020-04-18 00:25:44 +00:00
|
|
|
return color_util.color_RGB_to_hsv(*rgb) if rgb else None
|
|
|
|
|
2019-07-11 20:01:37 +00:00
|
|
|
@property
|
2021-03-19 09:21:06 +00:00
|
|
|
def white_value(self) -> int | None:
|
2019-07-11 20:01:37 +00:00
|
|
|
"""Return the white value."""
|
2021-03-19 09:21:06 +00:00
|
|
|
white: int | None = None
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.supports_rgbw:
|
|
|
|
_, white = self._device.current_color
|
2019-07-11 20:01:37 +00:00
|
|
|
return white
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-19 09:21:06 +00:00
|
|
|
def color_temp(self) -> int | None:
|
2019-02-08 07:28:52 +00:00
|
|
|
"""Return the color temperature in mireds."""
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.supports_color_temperature:
|
|
|
|
kelvin = self._device.current_color_temperature
|
2020-12-22 12:28:37 +00:00
|
|
|
# Avoid division by zero if actuator reported 0 Kelvin (e.g., uninitialized DALI-Gateway)
|
|
|
|
if kelvin is not None and kelvin > 0:
|
2019-02-08 07:28:52 +00:00
|
|
|
return color_util.color_temperature_kelvin_to_mired(kelvin)
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.supports_tunable_white:
|
|
|
|
relative_ct = self._device.current_tunable_white
|
2019-02-08 07:28:52 +00:00
|
|
|
if relative_ct is not None:
|
|
|
|
# as KNX devices typically use Kelvin we use it as base for
|
|
|
|
# calculating ct from percent
|
|
|
|
return color_util.color_temperature_kelvin_to_mired(
|
2019-07-31 19:25:30 +00:00
|
|
|
self._min_kelvin
|
|
|
|
+ ((relative_ct / 255) * (self._max_kelvin - self._min_kelvin))
|
|
|
|
)
|
2017-09-07 07:11:55 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2021-03-19 09:21:06 +00:00
|
|
|
def min_mireds(self) -> int:
|
2019-02-08 07:28:52 +00:00
|
|
|
"""Return the coldest color temp this light supports in mireds."""
|
|
|
|
return self._min_mireds
|
|
|
|
|
|
|
|
@property
|
2021-03-19 09:21:06 +00:00
|
|
|
def max_mireds(self) -> int:
|
2019-02-08 07:28:52 +00:00
|
|
|
"""Return the warmest color temp this light supports in mireds."""
|
|
|
|
return self._max_mireds
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-19 09:21:06 +00:00
|
|
|
def effect_list(self) -> list[str] | None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Return the list of supported effects."""
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property
|
2021-03-19 09:21:06 +00:00
|
|
|
def effect(self) -> str | None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Return the current effect."""
|
|
|
|
return None
|
2017-07-07 05:24:25 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-19 09:21:06 +00:00
|
|
|
def is_on(self) -> bool:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Return true if light is on."""
|
2021-03-19 09:21:06 +00:00
|
|
|
return bool(self._device.state)
|
2017-07-07 05:24:25 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-19 09:21:06 +00:00
|
|
|
def supported_features(self) -> int:
|
2017-07-07 05:24:25 +00:00
|
|
|
"""Flag supported features."""
|
2017-09-07 07:11:55 +00:00
|
|
|
flags = 0
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.supports_brightness:
|
2017-09-07 07:11:55 +00:00
|
|
|
flags |= SUPPORT_BRIGHTNESS
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.supports_color:
|
2019-02-08 07:28:52 +00:00
|
|
|
flags |= SUPPORT_COLOR | SUPPORT_BRIGHTNESS
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.supports_rgbw:
|
2019-07-11 20:01:37 +00:00
|
|
|
flags |= SUPPORT_COLOR | SUPPORT_WHITE_VALUE
|
2020-09-21 16:08:35 +00:00
|
|
|
if (
|
|
|
|
self._device.supports_color_temperature
|
|
|
|
or self._device.supports_tunable_white
|
|
|
|
):
|
2019-02-08 07:28:52 +00:00
|
|
|
flags |= SUPPORT_COLOR_TEMP
|
2017-09-07 07:11:55 +00:00
|
|
|
return flags
|
|
|
|
|
2021-03-19 09:21:06 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Turn the light on."""
|
2019-02-08 07:28:52 +00:00
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness)
|
|
|
|
hs_color = kwargs.get(ATTR_HS_COLOR, self.hs_color)
|
2019-07-11 20:01:37 +00:00
|
|
|
white_value = kwargs.get(ATTR_WHITE_VALUE, self.white_value)
|
2019-02-08 07:28:52 +00:00
|
|
|
mireds = kwargs.get(ATTR_COLOR_TEMP, self.color_temp)
|
|
|
|
|
|
|
|
update_brightness = ATTR_BRIGHTNESS in kwargs
|
|
|
|
update_color = ATTR_HS_COLOR in kwargs
|
2019-07-11 20:01:37 +00:00
|
|
|
update_white_value = ATTR_WHITE_VALUE in kwargs
|
2019-02-08 07:28:52 +00:00
|
|
|
update_color_temp = ATTR_COLOR_TEMP in kwargs
|
|
|
|
|
2020-01-29 11:07:25 +00:00
|
|
|
# avoid conflicting changes and weird effects
|
|
|
|
if not (
|
|
|
|
self.is_on
|
|
|
|
or update_brightness
|
|
|
|
or update_color
|
|
|
|
or update_white_value
|
|
|
|
or update_color_temp
|
|
|
|
):
|
2020-09-21 16:08:35 +00:00
|
|
|
await self._device.set_on()
|
2020-01-29 11:07:25 +00:00
|
|
|
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.supports_brightness and (
|
|
|
|
update_brightness and not update_color
|
|
|
|
):
|
2019-02-08 07:28:52 +00:00
|
|
|
# if we don't need to update the color, try updating brightness
|
|
|
|
# directly if supported; don't do it if color also has to be
|
|
|
|
# changed, as RGB color implicitly sets the brightness as well
|
2020-09-21 16:08:35 +00:00
|
|
|
await self._device.set_brightness(brightness)
|
|
|
|
elif (self._device.supports_rgbw or self._device.supports_color) and (
|
2019-07-31 19:25:30 +00:00
|
|
|
update_brightness or update_color or update_white_value
|
|
|
|
):
|
2020-01-29 11:07:25 +00:00
|
|
|
# change RGB color, white value (if supported), and brightness
|
2019-02-08 07:28:52 +00:00
|
|
|
# if brightness or hs_color was not yet set use the default value
|
|
|
|
# to calculate RGB from as a fallback
|
|
|
|
if brightness is None:
|
|
|
|
brightness = DEFAULT_BRIGHTNESS
|
|
|
|
if hs_color is None:
|
|
|
|
hs_color = DEFAULT_COLOR
|
2020-09-21 16:08:35 +00:00
|
|
|
if white_value is None and self._device.supports_rgbw:
|
2019-07-11 20:01:37 +00:00
|
|
|
white_value = DEFAULT_WHITE_VALUE
|
2021-03-19 09:21:06 +00:00
|
|
|
hsv_color = hs_color + (brightness * 100 / 255,)
|
|
|
|
rgb = color_util.color_hsv_to_RGB(*hsv_color)
|
2020-09-21 16:08:35 +00:00
|
|
|
await self._device.set_color(rgb, white_value)
|
2020-01-29 11:07:25 +00:00
|
|
|
|
|
|
|
if update_color_temp:
|
2019-02-08 07:28:52 +00:00
|
|
|
kelvin = int(color_util.color_temperature_mired_to_kelvin(mireds))
|
2020-01-29 11:07:25 +00:00
|
|
|
kelvin = min(self._max_kelvin, max(self._min_kelvin, kelvin))
|
|
|
|
|
2020-09-21 16:08:35 +00:00
|
|
|
if self._device.supports_color_temperature:
|
|
|
|
await self._device.set_color_temperature(kelvin)
|
|
|
|
elif self._device.supports_tunable_white:
|
2020-01-29 11:07:25 +00:00
|
|
|
relative_ct = int(
|
|
|
|
255
|
|
|
|
* (kelvin - self._min_kelvin)
|
|
|
|
/ (self._max_kelvin - self._min_kelvin)
|
|
|
|
)
|
2020-09-21 16:08:35 +00:00
|
|
|
await self._device.set_tunable_white(relative_ct)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-03-19 09:21:06 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Turn the light off."""
|
2020-09-21 16:08:35 +00:00
|
|
|
await self._device.set_off()
|