Migrate homematic lights to use Kelvin (#132794)

This commit is contained in:
epenet 2024-12-10 10:17:40 +01:00 committed by GitHub
parent 7b0a309fa7
commit 4880849074
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,7 +6,7 @@ from typing import Any
from homeassistant.components.light import ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP, ATTR_COLOR_TEMP_KELVIN,
ATTR_EFFECT, ATTR_EFFECT,
ATTR_HS_COLOR, ATTR_HS_COLOR,
ATTR_TRANSITION, ATTR_TRANSITION,
@ -17,10 +17,14 @@ from homeassistant.components.light import (
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import color as color_util
from .const import ATTR_DISCOVER_DEVICES from .const import ATTR_DISCOVER_DEVICES
from .entity import HMDevice from .entity import HMDevice
MAX_MIREDS = 500 # 2000 K
MIN_MIREDS = 153 # 6500 K
def setup_platform( def setup_platform(
hass: HomeAssistant, hass: HomeAssistant,
@ -43,6 +47,9 @@ def setup_platform(
class HMLight(HMDevice, LightEntity): class HMLight(HMDevice, LightEntity):
"""Representation of a Homematic light.""" """Representation of a Homematic light."""
_attr_min_color_temp_kelvin = 2000 # 500 Mireds
_attr_max_color_temp_kelvin = 6500 # 153 Mireds
@property @property
def brightness(self): def brightness(self):
"""Return the brightness of this light between 0..255.""" """Return the brightness of this light between 0..255."""
@ -99,12 +106,14 @@ class HMLight(HMDevice, LightEntity):
return hue * 360.0, sat * 100.0 return hue * 360.0, sat * 100.0
@property @property
def color_temp(self): def color_temp_kelvin(self) -> int | None:
"""Return the color temp in mireds [int].""" """Return the color temperature value in Kelvin."""
if ColorMode.COLOR_TEMP not in self.supported_color_modes: if ColorMode.COLOR_TEMP not in self.supported_color_modes:
return None return None
hm_color_temp = self._hmdevice.get_color_temp(self._channel) hm_color_temp = self._hmdevice.get_color_temp(self._channel)
return self.max_mireds - (self.max_mireds - self.min_mireds) * hm_color_temp return color_util.color_temperature_mired_to_kelvin(
MAX_MIREDS - (MAX_MIREDS - MIN_MIREDS) * hm_color_temp
)
@property @property
def effect_list(self): def effect_list(self):
@ -130,7 +139,7 @@ class HMLight(HMDevice, LightEntity):
self._hmdevice.set_level(percent_bright, self._channel) self._hmdevice.set_level(percent_bright, self._channel)
elif ( elif (
ATTR_HS_COLOR not in kwargs ATTR_HS_COLOR not in kwargs
and ATTR_COLOR_TEMP not in kwargs and ATTR_COLOR_TEMP_KELVIN not in kwargs
and ATTR_EFFECT not in kwargs and ATTR_EFFECT not in kwargs
): ):
self._hmdevice.on(self._channel) self._hmdevice.on(self._channel)
@ -141,10 +150,11 @@ class HMLight(HMDevice, LightEntity):
saturation=kwargs[ATTR_HS_COLOR][1] / 100.0, saturation=kwargs[ATTR_HS_COLOR][1] / 100.0,
channel=self._channel, channel=self._channel,
) )
if ATTR_COLOR_TEMP in kwargs: if ATTR_COLOR_TEMP_KELVIN in kwargs:
hm_temp = (self.max_mireds - kwargs[ATTR_COLOR_TEMP]) / ( mireds = color_util.color_temperature_kelvin_to_mired(
self.max_mireds - self.min_mireds kwargs[ATTR_COLOR_TEMP_KELVIN]
) )
hm_temp = (MAX_MIREDS - mireds) / (MAX_MIREDS - MIN_MIREDS)
self._hmdevice.set_color_temp(hm_temp) self._hmdevice.set_color_temp(hm_temp)
if ATTR_EFFECT in kwargs: if ATTR_EFFECT in kwargs:
self._hmdevice.set_effect(kwargs[ATTR_EFFECT]) self._hmdevice.set_effect(kwargs[ATTR_EFFECT])