Migrate homekit_controller lights to use Kelvin (#132792)

This commit is contained in:
epenet 2024-12-10 08:28:38 +01:00 committed by GitHub
parent 53e528e9b6
commit 1ee3b68824
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,7 +10,7 @@ from propcache import cached_property
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,
ColorMode, ColorMode,
LightEntity, LightEntity,
@ -57,7 +57,12 @@ class HomeKitLight(HomeKitEntity, LightEntity):
def _async_reconfigure(self) -> None: def _async_reconfigure(self) -> None:
"""Reconfigure entity.""" """Reconfigure entity."""
self._async_clear_property_cache( self._async_clear_property_cache(
("supported_features", "min_mireds", "max_mireds", "supported_color_modes") (
"supported_features",
"min_color_temp_kelvin",
"max_color_temp_kelvin",
"supported_color_modes",
)
) )
super()._async_reconfigure() super()._async_reconfigure()
@ -90,25 +95,35 @@ class HomeKitLight(HomeKitEntity, LightEntity):
) )
@cached_property @cached_property
def min_mireds(self) -> int: def max_color_temp_kelvin(self) -> int:
"""Return minimum supported color temperature.""" """Return the coldest color_temp_kelvin that this light supports."""
if not self.service.has(CharacteristicsTypes.COLOR_TEMPERATURE): if not self.service.has(CharacteristicsTypes.COLOR_TEMPERATURE):
return super().min_mireds return super().max_color_temp_kelvin
min_value = self.service[CharacteristicsTypes.COLOR_TEMPERATURE].minValue min_value_mireds = self.service[CharacteristicsTypes.COLOR_TEMPERATURE].minValue
return int(min_value) if min_value else super().min_mireds return (
color_util.color_temperature_mired_to_kelvin(min_value_mireds)
if min_value_mireds
else super().max_color_temp_kelvin
)
@cached_property @cached_property
def max_mireds(self) -> int: def min_color_temp_kelvin(self) -> int:
"""Return the maximum color temperature.""" """Return the warmest color_temp_kelvin that this light supports."""
if not self.service.has(CharacteristicsTypes.COLOR_TEMPERATURE): if not self.service.has(CharacteristicsTypes.COLOR_TEMPERATURE):
return super().max_mireds return super().min_color_temp_kelvin
max_value = self.service[CharacteristicsTypes.COLOR_TEMPERATURE].maxValue max_value_mireds = self.service[CharacteristicsTypes.COLOR_TEMPERATURE].maxValue
return int(max_value) if max_value else super().max_mireds return (
color_util.color_temperature_mired_to_kelvin(max_value_mireds)
if max_value_mireds
else super().min_color_temp_kelvin
)
@property @property
def color_temp(self) -> int: def color_temp_kelvin(self) -> int:
"""Return the color temperature.""" """Return the color temperature value in Kelvin."""
return self.service.value(CharacteristicsTypes.COLOR_TEMPERATURE) return color_util.color_temperature_mired_to_kelvin(
self.service.value(CharacteristicsTypes.COLOR_TEMPERATURE)
)
@property @property
def color_mode(self) -> str: def color_mode(self) -> str:
@ -153,7 +168,7 @@ class HomeKitLight(HomeKitEntity, LightEntity):
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the specified light on.""" """Turn the specified light on."""
hs_color = kwargs.get(ATTR_HS_COLOR) hs_color = kwargs.get(ATTR_HS_COLOR)
temperature = kwargs.get(ATTR_COLOR_TEMP) temperature_kelvin = kwargs.get(ATTR_COLOR_TEMP_KELVIN)
brightness = kwargs.get(ATTR_BRIGHTNESS) brightness = kwargs.get(ATTR_BRIGHTNESS)
characteristics: dict[str, Any] = {} characteristics: dict[str, Any] = {}
@ -167,19 +182,18 @@ class HomeKitLight(HomeKitEntity, LightEntity):
# does not support both, temperature will win. This is not # does not support both, temperature will win. This is not
# expected to happen in the UI, but it is possible via a manual # expected to happen in the UI, but it is possible via a manual
# service call. # service call.
if temperature is not None: if temperature_kelvin is not None:
if self.service.has(CharacteristicsTypes.COLOR_TEMPERATURE): if self.service.has(CharacteristicsTypes.COLOR_TEMPERATURE):
characteristics[CharacteristicsTypes.COLOR_TEMPERATURE] = int( characteristics[CharacteristicsTypes.COLOR_TEMPERATURE] = (
temperature color_util.color_temperature_kelvin_to_mired(temperature_kelvin)
) )
elif hs_color is None: elif hs_color is None:
# Some HomeKit devices implement color temperature with HS # Some HomeKit devices implement color temperature with HS
# since the spec "technically" does not permit the COLOR_TEMPERATURE # since the spec "technically" does not permit the COLOR_TEMPERATURE
# characteristic and the HUE and SATURATION characteristics to be # characteristic and the HUE and SATURATION characteristics to be
# present at the same time. # present at the same time.
hue_sat = color_util.color_temperature_to_hs( hue_sat = color_util.color_temperature_to_hs(temperature_kelvin)
color_util.color_temperature_mired_to_kelvin(temperature)
)
characteristics[CharacteristicsTypes.HUE] = hue_sat[0] characteristics[CharacteristicsTypes.HUE] = hue_sat[0]
characteristics[CharacteristicsTypes.SATURATION] = hue_sat[1] characteristics[CharacteristicsTypes.SATURATION] = hue_sat[1]