Migrate homekit_controller lights to use Kelvin (#132792)

pull/132783/head
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
1 changed files with 36 additions and 22 deletions

View File

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