mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Migrate KNX to use kelvin for color temperature (#81112)
This commit is contained in:
parent
68346599d2
commit
428a33c00f
@ -9,7 +9,7 @@ from xknx.devices.light import Light as XknxLight, XYYColor
|
|||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS,
|
ATTR_BRIGHTNESS,
|
||||||
ATTR_COLOR_TEMP,
|
ATTR_COLOR_TEMP_KELVIN,
|
||||||
ATTR_HS_COLOR,
|
ATTR_HS_COLOR,
|
||||||
ATTR_RGB_COLOR,
|
ATTR_RGB_COLOR,
|
||||||
ATTR_RGBW_COLOR,
|
ATTR_RGBW_COLOR,
|
||||||
@ -153,15 +153,8 @@ class KNXLight(KnxEntity, LightEntity):
|
|||||||
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
|
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
|
||||||
"""Initialize of KNX light."""
|
"""Initialize of KNX light."""
|
||||||
super().__init__(_create_light(xknx, config))
|
super().__init__(_create_light(xknx, config))
|
||||||
self._max_kelvin: int = config[LightSchema.CONF_MAX_KELVIN]
|
self._attr_max_color_temp_kelvin: int = config[LightSchema.CONF_MAX_KELVIN]
|
||||||
self._min_kelvin: int = config[LightSchema.CONF_MIN_KELVIN]
|
self._attr_min_color_temp_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_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
||||||
self._attr_unique_id = self._device_unique_id()
|
self._attr_unique_id = self._device_unique_id()
|
||||||
|
|
||||||
@ -242,21 +235,23 @@ class KNXLight(KnxEntity, LightEntity):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def color_temp(self) -> int | None:
|
def color_temp_kelvin(self) -> int | None:
|
||||||
"""Return the color temperature in mireds."""
|
"""Return the color temperature in Kelvin."""
|
||||||
if self._device.supports_color_temperature:
|
if self._device.supports_color_temperature:
|
||||||
kelvin = self._device.current_color_temperature
|
if kelvin := self._device.current_color_temperature:
|
||||||
# Avoid division by zero if actuator reported 0 Kelvin (e.g., uninitialized DALI-Gateway)
|
return kelvin
|
||||||
if kelvin is not None and kelvin > 0:
|
|
||||||
return color_util.color_temperature_kelvin_to_mired(kelvin)
|
|
||||||
if self._device.supports_tunable_white:
|
if self._device.supports_tunable_white:
|
||||||
relative_ct = self._device.current_tunable_white
|
relative_ct = self._device.current_tunable_white
|
||||||
if relative_ct is not None:
|
if relative_ct is not None:
|
||||||
# as KNX devices typically use Kelvin we use it as base for
|
return int(
|
||||||
# calculating ct from percent
|
self._attr_min_color_temp_kelvin
|
||||||
return color_util.color_temperature_kelvin_to_mired(
|
+ (
|
||||||
self._min_kelvin
|
(relative_ct / 255)
|
||||||
+ ((relative_ct / 255) * (self._max_kelvin - self._min_kelvin))
|
* (
|
||||||
|
self._attr_max_color_temp_kelvin
|
||||||
|
- self._attr_min_color_temp_kelvin
|
||||||
|
)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -288,7 +283,7 @@ class KNXLight(KnxEntity, LightEntity):
|
|||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the light on."""
|
"""Turn the light on."""
|
||||||
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
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)
|
rgb = kwargs.get(ATTR_RGB_COLOR)
|
||||||
rgbw = kwargs.get(ATTR_RGBW_COLOR)
|
rgbw = kwargs.get(ATTR_RGBW_COLOR)
|
||||||
hs_color = kwargs.get(ATTR_HS_COLOR)
|
hs_color = kwargs.get(ATTR_HS_COLOR)
|
||||||
@ -297,7 +292,7 @@ class KNXLight(KnxEntity, LightEntity):
|
|||||||
if (
|
if (
|
||||||
not self.is_on
|
not self.is_on
|
||||||
and brightness is None
|
and brightness is None
|
||||||
and mireds is None
|
and color_temp is None
|
||||||
and rgb is None
|
and rgb is None
|
||||||
and rgbw is None
|
and rgbw is None
|
||||||
and hs_color is None
|
and hs_color is None
|
||||||
@ -335,17 +330,21 @@ class KNXLight(KnxEntity, LightEntity):
|
|||||||
await set_color(rgb, None, brightness)
|
await set_color(rgb, None, brightness)
|
||||||
return
|
return
|
||||||
|
|
||||||
if mireds is not None:
|
if color_temp is not None:
|
||||||
kelvin = int(color_util.color_temperature_mired_to_kelvin(mireds))
|
color_temp = min(
|
||||||
kelvin = min(self._max_kelvin, max(self._min_kelvin, kelvin))
|
self._attr_max_color_temp_kelvin,
|
||||||
|
max(self._attr_min_color_temp_kelvin, color_temp),
|
||||||
|
)
|
||||||
if self._device.supports_color_temperature:
|
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:
|
elif self._device.supports_tunable_white:
|
||||||
relative_ct = int(
|
relative_ct = round(
|
||||||
255
|
255
|
||||||
* (kelvin - self._min_kelvin)
|
* (color_temp - self._attr_min_color_temp_kelvin)
|
||||||
/ (self._max_kelvin - self._min_kelvin)
|
/ (
|
||||||
|
self._attr_max_color_temp_kelvin
|
||||||
|
- self._attr_min_color_temp_kelvin
|
||||||
|
)
|
||||||
)
|
)
|
||||||
await self._device.set_tunable_white(relative_ct)
|
await self._device.set_tunable_white(relative_ct)
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ from homeassistant.components.knx.schema import LightSchema
|
|||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS,
|
ATTR_BRIGHTNESS,
|
||||||
ATTR_COLOR_NAME,
|
ATTR_COLOR_NAME,
|
||||||
ATTR_COLOR_TEMP,
|
ATTR_COLOR_TEMP_KELVIN,
|
||||||
ATTR_HS_COLOR,
|
ATTR_HS_COLOR,
|
||||||
ATTR_RGBW_COLOR,
|
ATTR_RGBW_COLOR,
|
||||||
ColorMode,
|
ColorMode,
|
||||||
@ -166,19 +166,25 @@ async def test_light_color_temp_absolute(hass: HomeAssistant, knx: KNXTestKit):
|
|||||||
brightness=255,
|
brightness=255,
|
||||||
color_mode=ColorMode.COLOR_TEMP,
|
color_mode=ColorMode.COLOR_TEMP,
|
||||||
color_temp=370,
|
color_temp=370,
|
||||||
|
color_temp_kelvin=2700,
|
||||||
)
|
)
|
||||||
# change color temperature from HA
|
# change color temperature from HA
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"light",
|
"light",
|
||||||
"turn_on",
|
"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,
|
blocking=True,
|
||||||
)
|
)
|
||||||
await knx.assert_write(test_ct, (0x0F, 0xA0))
|
await knx.assert_write(test_ct, (0x0F, 0xA0))
|
||||||
knx.assert_state("light.test", STATE_ON, color_temp=250)
|
knx.assert_state("light.test", STATE_ON, color_temp=250)
|
||||||
# change color temperature from KNX
|
# change color temperature from KNX
|
||||||
await knx.receive_write(test_ct_state, (0x17, 0x70)) # 6000 Kelvin - 166 Mired
|
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):
|
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,
|
brightness=255,
|
||||||
color_mode=ColorMode.COLOR_TEMP,
|
color_mode=ColorMode.COLOR_TEMP,
|
||||||
color_temp=250,
|
color_temp=250,
|
||||||
|
color_temp_kelvin=4000,
|
||||||
)
|
)
|
||||||
# change color temperature from HA
|
# change color temperature from HA
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"light",
|
"light",
|
||||||
"turn_on",
|
"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,
|
blocking=True,
|
||||||
)
|
)
|
||||||
await knx.assert_write(test_ct, (0x54,))
|
await knx.assert_write(test_ct, (0x55,))
|
||||||
knx.assert_state("light.test", STATE_ON, color_temp=300)
|
knx.assert_state(
|
||||||
|
"light.test",
|
||||||
|
STATE_ON,
|
||||||
|
color_temp=300,
|
||||||
|
color_temp_kelvin=3333,
|
||||||
|
)
|
||||||
# change color temperature from KNX
|
# change color temperature from KNX
|
||||||
await knx.receive_write(test_ct_state, (0xE6,)) # 3900 Kelvin - 90 % - 256 Mired
|
await knx.receive_write(test_ct_state, (0xE6,)) # 3901 Kelvin - 90.1 % - 256 Mired
|
||||||
knx.assert_state("light.test", STATE_ON, color_temp=256)
|
knx.assert_state(
|
||||||
|
"light.test",
|
||||||
|
STATE_ON,
|
||||||
|
color_temp=256,
|
||||||
|
color_temp_kelvin=3901,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_light_hs_color(hass: HomeAssistant, knx: KNXTestKit):
|
async def test_light_hs_color(hass: HomeAssistant, knx: KNXTestKit):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user