2020
2121from homeassistant .components .light import (
2222 ATTR_COLOR_MODE ,
23- ATTR_COLOR_TEMP ,
2423 ATTR_COLOR_TEMP_KELVIN ,
2524 ATTR_HS_COLOR ,
2625 ATTR_RGB_COLOR ,
4544from homeassistant .helpers .event import async_track_state_change_event
4645
4746from .const import (
47+ ATTR_COLOR_TEMP ,
4848 CONF_ANIMATE_BRIGHTNESS ,
4949 CONF_ANIMATE_COLOR ,
5050 CONF_ANIMATED_SCENE_SWITCH ,
198198)
199199
200200
201+ def _convert_mireds_to_kelvin (mireds : int ) -> int :
202+ """Convert mireds to kelvin, ensuring the result is within valid bounds."""
203+ if mireds <= 0 :
204+ raise IntegrationError ("Mireds must be a positive integer" )
205+ kelvin = int (1000000 / mireds )
206+ if kelvin < 1500 :
207+ kelvin = 1500
208+ elif kelvin > 9000 :
209+ kelvin = 9000
210+ return kelvin
211+
212+
201213async def safe_call (hass : HomeAssistant , domain : str , service : str , attr : dict ) -> None :
202214 """Call a Home Assistant service safely, logging exceptions.
203215
@@ -249,7 +261,7 @@ def __init__(self, hass: HomeAssistant, config: dict[str, Any]) -> None:
249261 self ._global_brightness : int | list [int ] = config [CONF_BRIGHTNESS ]
250262 self ._change_amount : int | list [int ] | str = config [CONF_CHANGE_AMOUNT ]
251263 self ._change_frequency : int | list [int ] = config [CONF_CHANGE_FREQUENCY ]
252- self ._colors : dict [int , Any ] = config [CONF_COLORS ]
264+ self ._colors : list [ dict [str , Any ] ] = config [CONF_COLORS ]
253265 self ._current_color_index : int = 0
254266 self ._hass : HomeAssistant = hass
255267 self ._ignore_off : bool = config [CONF_IGNORE_OFF ]
@@ -263,6 +275,8 @@ def __init__(self, hass: HomeAssistant, config: dict[str, Any]) -> None:
263275 self ._transition : int | list [int ] = config [CONF_TRANSITION ]
264276 self ._weights : list = []
265277
278+ self ._change_mired_colors_to_kelvin ()
279+
266280 for color in self ._colors :
267281 if "weight" in color :
268282 self ._weights .append (color ["weight" ])
@@ -294,6 +308,25 @@ def restore_power(self) -> bool:
294308 """Whether this animation should restore power (turn off) states."""
295309 return self ._restore_power
296310
311+ def _change_mired_colors_to_kelvin (self ) -> None :
312+ """Convert any colors in mireds to kelvin in place."""
313+ for color in self ._colors [:]:
314+ if color [CONF_COLOR_TYPE ] == ATTR_COLOR_TEMP :
315+ try :
316+ kelvin = _convert_mireds_to_kelvin (color [CONF_COLOR ])
317+ _LOGGER .debug (
318+ "Converted color temp %d mireds to %d kelvin" , color [CONF_COLOR ], kelvin
319+ )
320+ color [CONF_COLOR ] = kelvin
321+ color [CONF_COLOR_TYPE ] = ATTR_COLOR_TEMP_KELVIN
322+ except IntegrationError as e :
323+ _LOGGER .warning (
324+ "Skipping invalid color temp %d mireds: %s" ,
325+ color [CONF_COLOR ],
326+ e ,
327+ )
328+ self ._colors .remove (color )
329+
297330 def add_light (self , entity_id : str ) -> None :
298331 """Add a single light to this animation's active list.
299332
@@ -386,7 +419,7 @@ def build_light_attributes(self, light: str, initial: bool = False) -> dict[str,
386419 }
387420
388421 if self ._sequence :
389- color = self ._colors [self ._current_color_index ]
422+ color : dict [ str , Any ] = self ._colors [self ._current_color_index ]
390423 else :
391424 color = self .pick_color ()
392425
@@ -409,7 +442,6 @@ def build_light_attributes(self, light: str, initial: bool = False) -> dict[str,
409442 "change_one" : color [CONF_COLOR_ONE_CHANGE_PER_TICK ],
410443 "brightness" : color [CONF_BRIGHTNESS ],
411444 }
412-
413445 return attributes
414446
415447 def find_nearby_color (self , color : dict [str , Any ]) -> list [int ]:
@@ -497,7 +529,7 @@ def get_static_or_random(self, value: int | list, step: int = 1) -> float:
497529 return randrange (value [0 ], value [1 ], step )
498530 return value
499531
500- def pick_color (self ) -> list [ int ]:
532+ def pick_color (self ) -> dict [ str , Any ]:
501533 """Pick a color group according to configured weights."""
502534 color : list = choices (self ._colors , self ._weights , k = 1 )
503535 return color .pop ()
@@ -681,7 +713,6 @@ def build_attributes_from_state(self, state: State) -> dict[str, Any]:
681713 if value :
682714 attributes [attr ] = value
683715 break
684-
685716 return attributes
686717
687718 def external_light_change (self , event : Event [EventStateChangedData ]) -> Any :
0 commit comments