Migrate KNX to use kelvin for color temperature (#81112)

This commit is contained in:
Matthias Alphart 2022-10-27 22:00:34 +02:00 committed by GitHub
parent 68346599d2
commit 428a33c00f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 39 deletions

View File

@ -9,7 +9,7 @@ from xknx.devices.light import Light as XknxLight, XYYColor
from homeassistant import config_entries
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_COLOR_TEMP_KELVIN,
ATTR_HS_COLOR,
ATTR_RGB_COLOR,
ATTR_RGBW_COLOR,
@ -153,15 +153,8 @@ class KNXLight(KnxEntity, LightEntity):
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
"""Initialize of KNX light."""
super().__init__(_create_light(xknx, config))
self._max_kelvin: int = config[LightSchema.CONF_MAX_KELVIN]
self._min_kelvin: int = config[LightSchema.CONF_MIN_KELVIN]
self._attr_max_mireds = color_util.color_temperature_kelvin_to_mired(
self._min_kelvin
)
self._attr_min_mireds = color_util.color_temperature_kelvin_to_mired(
self._max_kelvin
)
self._attr_max_color_temp_kelvin: int = config[LightSchema.CONF_MAX_KELVIN]
self._attr_min_color_temp_kelvin: int = config[LightSchema.CONF_MIN_KELVIN]
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
self._attr_unique_id = self._device_unique_id()
@ -242,21 +235,23 @@ class KNXLight(KnxEntity, LightEntity):
return None
@property
def color_temp(self) -> int | None:
"""Return the color temperature in mireds."""
def color_temp_kelvin(self) -> int | None:
"""Return the color temperature in Kelvin."""
if self._device.supports_color_temperature:
kelvin = self._device.current_color_temperature
# Avoid division by zero if actuator reported 0 Kelvin (e.g., uninitialized DALI-Gateway)
if kelvin is not None and kelvin > 0:
return color_util.color_temperature_kelvin_to_mired(kelvin)
if kelvin := self._device.current_color_temperature:
return kelvin
if self._device.supports_tunable_white:
relative_ct = self._device.current_tunable_white
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(
self._min_kelvin
+ ((relative_ct / 255) * (self._max_kelvin - self._min_kelvin))
return int(
self._attr_min_color_temp_kelvin
+ (
(relative_ct / 255)
* (
self._attr_max_color_temp_kelvin
- self._attr_min_color_temp_kelvin
)
)
)
return None
@ -288,7 +283,7 @@ class KNXLight(KnxEntity, LightEntity):
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the light on."""
brightness = kwargs.get(ATTR_BRIGHTNESS)
mireds = kwargs.get(ATTR_COLOR_TEMP)
color_temp = kwargs.get(ATTR_COLOR_TEMP_KELVIN)
rgb = kwargs.get(ATTR_RGB_COLOR)
rgbw = kwargs.get(ATTR_RGBW_COLOR)
hs_color = kwargs.get(ATTR_HS_COLOR)
@ -297,7 +292,7 @@ class KNXLight(KnxEntity, LightEntity):
if (
not self.is_on
and brightness is None
and mireds is None
and color_temp is None
and rgb is None
and rgbw is None
and hs_color is None
@ -335,17 +330,21 @@ class KNXLight(KnxEntity, LightEntity):
await set_color(rgb, None, brightness)
return
if mireds is not None:
kelvin = int(color_util.color_temperature_mired_to_kelvin(mireds))
kelvin = min(self._max_kelvin, max(self._min_kelvin, kelvin))
if color_temp is not None:
color_temp = min(
self._attr_max_color_temp_kelvin,
max(self._attr_min_color_temp_kelvin, color_temp),
)
if self._device.supports_color_temperature:
await self._device.set_color_temperature(kelvin)
await self._device.set_color_temperature(color_temp)
elif self._device.supports_tunable_white:
relative_ct = int(
relative_ct = round(
255
* (kelvin - self._min_kelvin)
/ (self._max_kelvin - self._min_kelvin)
* (color_temp - self._attr_min_color_temp_kelvin)
/ (
self._attr_max_color_temp_kelvin
- self._attr_min_color_temp_kelvin
)
)
await self._device.set_tunable_white(relative_ct)

View File

@ -11,7 +11,7 @@ from homeassistant.components.knx.schema import LightSchema
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_NAME,
ATTR_COLOR_TEMP,
ATTR_COLOR_TEMP_KELVIN,
ATTR_HS_COLOR,
ATTR_RGBW_COLOR,
ColorMode,
@ -166,19 +166,25 @@ async def test_light_color_temp_absolute(hass: HomeAssistant, knx: KNXTestKit):
brightness=255,
color_mode=ColorMode.COLOR_TEMP,
color_temp=370,
color_temp_kelvin=2700,
)
# change color temperature from HA
await hass.services.async_call(
"light",
"turn_on",
{"entity_id": "light.test", ATTR_COLOR_TEMP: 250}, # 4000 Kelvin - 0x0FA0
{"entity_id": "light.test", ATTR_COLOR_TEMP_KELVIN: 4000}, # 4000 - 0x0FA0
blocking=True,
)
await knx.assert_write(test_ct, (0x0F, 0xA0))
knx.assert_state("light.test", STATE_ON, color_temp=250)
# change color temperature from KNX
await knx.receive_write(test_ct_state, (0x17, 0x70)) # 6000 Kelvin - 166 Mired
knx.assert_state("light.test", STATE_ON, color_temp=166)
knx.assert_state(
"light.test",
STATE_ON,
color_temp=166,
color_temp_kelvin=6000,
)
async def test_light_color_temp_relative(hass: HomeAssistant, knx: KNXTestKit):
@ -222,19 +228,33 @@ async def test_light_color_temp_relative(hass: HomeAssistant, knx: KNXTestKit):
brightness=255,
color_mode=ColorMode.COLOR_TEMP,
color_temp=250,
color_temp_kelvin=4000,
)
# change color temperature from HA
await hass.services.async_call(
"light",
"turn_on",
{"entity_id": "light.test", ATTR_COLOR_TEMP: 300}, # 3333 Kelvin - 33 % - 0x54
{
"entity_id": "light.test",
ATTR_COLOR_TEMP_KELVIN: 3333, # 3333 Kelvin - 33.3 % - 0x55
},
blocking=True,
)
await knx.assert_write(test_ct, (0x54,))
knx.assert_state("light.test", STATE_ON, color_temp=300)
await knx.assert_write(test_ct, (0x55,))
knx.assert_state(
"light.test",
STATE_ON,
color_temp=300,
color_temp_kelvin=3333,
)
# change color temperature from KNX
await knx.receive_write(test_ct_state, (0xE6,)) # 3900 Kelvin - 90 % - 256 Mired
knx.assert_state("light.test", STATE_ON, color_temp=256)
await knx.receive_write(test_ct_state, (0xE6,)) # 3901 Kelvin - 90.1 % - 256 Mired
knx.assert_state(
"light.test",
STATE_ON,
color_temp=256,
color_temp_kelvin=3901,
)
async def test_light_hs_color(hass: HomeAssistant, knx: KNXTestKit):