Add color temp support for older HomeKit devices (#107206)

This commit is contained in:
J. Nick Koston 2024-01-04 22:50:26 -10:00 committed by GitHub
parent f0ec1235b1
commit 2641e4014a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 151 additions and 14 deletions

View file

@ -17,6 +17,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.util.color as color_util
from . import KNOWN_DEVICES
from .connection import HKDevice
@ -94,12 +95,16 @@ class HomeKitLight(HomeKitEntity, LightEntity):
@cached_property
def min_mireds(self) -> int:
"""Return minimum supported color temperature."""
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
@cached_property
def max_mireds(self) -> int:
"""Return the maximum color temperature."""
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
@ -135,8 +140,9 @@ class HomeKitLight(HomeKitEntity, LightEntity):
CharacteristicsTypes.SATURATION
):
color_modes.add(ColorMode.HS)
color_modes.add(ColorMode.COLOR_TEMP)
if self.service.has(CharacteristicsTypes.COLOR_TEMPERATURE):
elif self.service.has(CharacteristicsTypes.COLOR_TEMPERATURE):
color_modes.add(ColorMode.COLOR_TEMP)
if not color_modes and self.service.has(CharacteristicsTypes.BRIGHTNESS):
@ -153,23 +159,36 @@ class HomeKitLight(HomeKitEntity, LightEntity):
temperature = kwargs.get(ATTR_COLOR_TEMP)
brightness = kwargs.get(ATTR_BRIGHTNESS)
characteristics = {}
if hs_color is not None:
characteristics.update(
{
CharacteristicsTypes.HUE: hs_color[0],
CharacteristicsTypes.SATURATION: hs_color[1],
}
)
characteristics: dict[str, Any] = {}
if brightness is not None:
characteristics[CharacteristicsTypes.BRIGHTNESS] = int(
brightness * 100 / 255
)
# If they send both temperature and hs_color, and the device
# 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:
characteristics[CharacteristicsTypes.COLOR_TEMPERATURE] = int(temperature)
if self.service.has(CharacteristicsTypes.COLOR_TEMPERATURE):
characteristics[CharacteristicsTypes.COLOR_TEMPERATURE] = int(
temperature
)
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)
)
characteristics[CharacteristicsTypes.HUE] = hue_sat[0]
characteristics[CharacteristicsTypes.SATURATION] = hue_sat[1]
if hs_color is not None:
characteristics[CharacteristicsTypes.HUE] = hs_color[0]
characteristics[CharacteristicsTypes.SATURATION] = hs_color[1]
characteristics[CharacteristicsTypes.ON] = True