1 """Support for IKEA Tradfri lights."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from typing
import Any, cast
8 from pytradfri.command
import Command
18 filter_supported_color_modes,
25 from .const
import CONF_GATEWAY_ID, COORDINATOR, COORDINATOR_LIST, DOMAIN, KEY_API
26 from .coordinator
import TradfriDeviceDataUpdateCoordinator
27 from .entity
import TradfriBaseEntity
32 config_entry: ConfigEntry,
33 async_add_entities: AddEntitiesCallback,
35 """Load Tradfri lights based on a config entry."""
36 gateway_id = config_entry.data[CONF_GATEWAY_ID]
37 coordinator_data = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
38 api = coordinator_data[KEY_API]
46 for device_coordinator
in coordinator_data[COORDINATOR_LIST]
47 if device_coordinator.device.has_light_control
52 """The platform class required by Home Assistant."""
55 _attr_supported_features = LightEntityFeature.TRANSITION
56 _fixed_color_mode: ColorMode |
None =
None
60 device_coordinator: TradfriDeviceDataUpdateCoordinator,
61 api: Callable[[Command | list[Command]], Any],
64 """Initialize a Light."""
66 device_coordinator=device_coordinator,
68 gateway_id=gateway_id,
78 modes: set[ColorMode] = {ColorMode.ONOFF}
79 if self._device.light_control.can_set_color:
80 modes.add(ColorMode.HS)
81 if self._device.light_control.can_set_temp:
82 modes.add(ColorMode.COLOR_TEMP)
83 if self._device.light_control.can_set_dimmer:
84 modes.add(ColorMode.BRIGHTNESS)
94 """Refresh the device."""
95 self.
_device_data_device_data = self.coordinator.data.light_control.lights[0]
99 """Return true if light is on."""
106 """Return the color mode of the light."""
111 return ColorMode.COLOR_TEMP
115 """Return the brightness of the light."""
122 """Return the color temp value in mireds."""
125 return cast(int, self.
_device_data_device_data.color_temp)
129 """HS color of the light."""
135 sat = hsbxy[1] / (self.
_device_control_device_control.max_saturation / 100)
136 if hue
is not None and sat
is not None:
141 """Instruct the light to turn off."""
146 transition_time =
None
147 if ATTR_TRANSITION
in kwargs:
148 transition_time =
int(kwargs[ATTR_TRANSITION]) * 10
152 dimmer=0, transition_time=transition_time
159 """Instruct the light to turn on."""
162 transition_time =
None
163 if ATTR_TRANSITION
in kwargs:
164 transition_time =
int(kwargs[ATTR_TRANSITION]) * 10
166 dimmer_command =
None
167 if ATTR_BRIGHTNESS
in kwargs:
168 brightness = kwargs[ATTR_BRIGHTNESS]
169 brightness =
min(brightness, 254)
171 "dimmer": brightness,
172 "transition_time": transition_time,
174 dimmer_command = self.
_device_control_device_control.set_dimmer(**dimmer_data)
175 transition_time =
None
180 if ATTR_HS_COLOR
in kwargs
and self.
_device_control_device_control.can_set_color:
181 hue =
int(kwargs[ATTR_HS_COLOR][0] * (self.
_device_control_device_control.max_hue / 360))
183 kwargs[ATTR_HS_COLOR][1] * (self.
_device_control_device_control.max_saturation / 100)
188 "transition_time": transition_time,
190 color_command = self.
_device_control_device_control.set_hsb(**color_data)
191 transition_time =
None
194 if ATTR_COLOR_TEMP
in kwargs
and (
197 temp = kwargs[ATTR_COLOR_TEMP]
205 ATTR_COLOR_TEMP: temp,
206 "transition_time": transition_time,
208 temp_command = self.
_device_control_device_control.set_color_temp(**temp_data)
209 transition_time =
None
213 temp_k = color_util.color_temperature_mired_to_kelvin(temp)
214 hs_color = color_util.color_temperature_to_hs(temp_k)
220 "transition_time": transition_time,
222 color_command = self.
_device_control_device_control.set_hsb(**color_data)
223 transition_time =
None
226 if (command := dimmer_command)
is not None:
227 command += color_command
229 command = color_command
232 await self.
_api_api(command + temp_command)
234 if temp_command
is not None:
235 await self.
_api_api(temp_command)
236 if command
is not None:
237 await self.
_api_api(command)
tuple[float, float]|None hs_color(self)
int|None brightness(self)
ColorMode|None color_mode(self)
None __init__(self, TradfriDeviceDataUpdateCoordinator device_coordinator, Callable[[Command|list[Command]], Any] api, str gateway_id)
_attr_supported_color_modes
None async_turn_on(self, **Any kwargs)
int|None color_temp(self)
tuple[float, float]|None hs_color(self)
None async_turn_off(self, **Any kwargs)
set[ColorMode] filter_supported_color_modes(Iterable[ColorMode] color_modes)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)