Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions homeassistant/components/homekit_controller/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
ATTR_COLOR_TEMP_KELVIN,
ATTR_HS_COLOR,
ColorMode,
LightEntity,
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -90,25 +95,35 @@ def hs_color(self) -> tuple[float, float]:
)

@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:
Expand Down Expand Up @@ -153,7 +168,7 @@ def supported_color_modes(self) -> set[ColorMode]:
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] = {}
Expand All @@ -167,19 +182,18 @@ async def async_turn_on(self, **kwargs: Any) -> None:
# 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]

Expand Down