1 """Support for Homekit lights."""
3 from __future__
import annotations
7 from aiohomekit.model.characteristics
import CharacteristicsTypes
8 from aiohomekit.model.services
import Service, ServicesTypes
9 from propcache
import cached_property
24 from .
import KNOWN_DEVICES
25 from .connection
import HKDevice
26 from .entity
import HomeKitEntity
31 config_entry: ConfigEntry,
32 async_add_entities: AddEntitiesCallback,
34 """Set up Homekit lightbulb."""
35 hkid: str = config_entry.data[
"AccessoryPairingID"]
36 conn: HKDevice = hass.data[KNOWN_DEVICES][hkid]
39 def async_add_service(service: Service) -> bool:
40 if service.type != ServicesTypes.LIGHTBULB:
42 info = {
"aid": service.accessory.aid,
"iid": service.iid}
44 conn.async_migrate_unique_id(
45 entity.old_unique_id, entity.unique_id, Platform.LIGHT
50 conn.add_listener(async_add_service)
54 """Representation of a Homekit light."""
58 """Reconfigure entity."""
60 (
"supported_features",
"min_mireds",
"max_mireds",
"supported_color_modes")
65 """Define the homekit characteristics the entity cares about."""
67 CharacteristicsTypes.ON,
68 CharacteristicsTypes.BRIGHTNESS,
69 CharacteristicsTypes.COLOR_TEMPERATURE,
70 CharacteristicsTypes.HUE,
71 CharacteristicsTypes.SATURATION,
76 """Return true if device is on."""
77 return self.
serviceservice.value(CharacteristicsTypes.ON)
81 """Return the brightness of this light between 0..255."""
82 return self.
serviceservice.value(CharacteristicsTypes.BRIGHTNESS) * 255 / 100
86 """Return the color property."""
88 self.
serviceservice.value(CharacteristicsTypes.HUE),
89 self.
serviceservice.value(CharacteristicsTypes.SATURATION),
94 """Return minimum supported color temperature."""
95 if not self.
serviceservice.has(CharacteristicsTypes.COLOR_TEMPERATURE):
96 return super().min_mireds
97 min_value = self.
serviceservice[CharacteristicsTypes.COLOR_TEMPERATURE].minValue
98 return int(min_value)
if min_value
else super().min_mireds
102 """Return the maximum color temperature."""
103 if not self.
serviceservice.has(CharacteristicsTypes.COLOR_TEMPERATURE):
104 return super().max_mireds
105 max_value = self.
serviceservice[CharacteristicsTypes.COLOR_TEMPERATURE].maxValue
106 return int(max_value)
if max_value
else super().max_mireds
110 """Return the color temperature."""
111 return self.
serviceservice.value(CharacteristicsTypes.COLOR_TEMPERATURE)
115 """Return the color mode of the light."""
118 if self.
serviceservice.has(CharacteristicsTypes.HUE)
or self.
serviceservice.has(
119 CharacteristicsTypes.SATURATION
123 if self.
serviceservice.has(CharacteristicsTypes.COLOR_TEMPERATURE):
124 return ColorMode.COLOR_TEMP
126 if self.
serviceservice.has(CharacteristicsTypes.BRIGHTNESS):
127 return ColorMode.BRIGHTNESS
129 return ColorMode.ONOFF
133 """Flag supported color modes."""
134 color_modes: set[ColorMode] = set()
136 if self.
serviceservice.has(CharacteristicsTypes.HUE)
or self.
serviceservice.has(
137 CharacteristicsTypes.SATURATION
139 color_modes.add(ColorMode.HS)
140 color_modes.add(ColorMode.COLOR_TEMP)
142 elif self.
serviceservice.has(CharacteristicsTypes.COLOR_TEMPERATURE):
143 color_modes.add(ColorMode.COLOR_TEMP)
145 if not color_modes
and self.
serviceservice.has(CharacteristicsTypes.BRIGHTNESS):
146 color_modes.add(ColorMode.BRIGHTNESS)
149 color_modes.add(ColorMode.ONOFF)
154 """Turn the specified light on."""
155 hs_color = kwargs.get(ATTR_HS_COLOR)
156 temperature = kwargs.get(ATTR_COLOR_TEMP)
157 brightness = kwargs.get(ATTR_BRIGHTNESS)
159 characteristics: dict[str, Any] = {}
161 if brightness
is not None:
162 characteristics[CharacteristicsTypes.BRIGHTNESS] =
int(
163 brightness * 100 / 255
170 if temperature
is not None:
171 if self.
serviceservice.has(CharacteristicsTypes.COLOR_TEMPERATURE):
172 characteristics[CharacteristicsTypes.COLOR_TEMPERATURE] =
int(
175 elif hs_color
is None:
180 hue_sat = color_util.color_temperature_to_hs(
181 color_util.color_temperature_mired_to_kelvin(temperature)
183 characteristics[CharacteristicsTypes.HUE] = hue_sat[0]
184 characteristics[CharacteristicsTypes.SATURATION] = hue_sat[1]
186 if hs_color
is not None:
187 characteristics[CharacteristicsTypes.HUE] = hs_color[0]
188 characteristics[CharacteristicsTypes.SATURATION] = hs_color[1]
190 characteristics[CharacteristicsTypes.ON] =
True
195 """Turn the specified light off."""
None async_put_characteristics(self, dict[str, Any] characteristics)
None _async_clear_property_cache(self, tuple[str,...] properties)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
None _async_reconfigure(self)
set[ColorMode] supported_color_modes(self)
tuple[float, float] hs_color(self)
list[str] get_characteristic_types(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)