Skip to content

Commit 1dcc40e

Browse files
committed
Fix deprecated color_temp
1 parent 0a5df54 commit 1dcc40e

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

README.MD

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,6 @@ For configuring colors, you can choose from the following options, see [Color Co
125125
- 75.0 # Saturation value
126126
```
127127
128-
#### For Color Temp lights
129-
130-
```yaml
131-
- color_type: color_temp
132-
color:
133-
- 450 # Color temperature in mireds
134-
```
135-
136128
#### For Color Temp in Kelvin lights
137129
138130
```yaml
@@ -231,7 +223,7 @@ sequence:
231223
- light.living_room_1
232224
- light.living_room_2
233225
data:
234-
kelvin: 2800
226+
color_temp_kelvin: 2800
235227
brightness_pct: 100
236228
```
237229

custom_components/animated_scenes/animations.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from 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,
@@ -45,6 +44,7 @@
4544
from homeassistant.helpers.event import async_track_state_change_event
4645

4746
from .const import (
47+
ATTR_COLOR_TEMP,
4848
CONF_ANIMATE_BRIGHTNESS,
4949
CONF_ANIMATE_COLOR,
5050
CONF_ANIMATED_SCENE_SWITCH,
@@ -198,6 +198,18 @@
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+
201213
async 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:

custom_components/animated_scenes/const.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
VERSION = "2.0.1"
66
COMPONENT_COLOR_CONFIG_URL = "https://github.com/chazzu/hass-animated-scenes#color-configuration"
77

8+
ATTR_COLOR_TEMP = "color_temp" # Deprecated by HASS, will auto-convert to ATTR_COLOR_TEMP_KELVIN
9+
810
CONF_EXTERNAL_SWITCHES = "external_switches"
911

1012
CONF_ANIMATE_BRIGHTNESS = "animate_brightness"
@@ -14,9 +16,6 @@
1416
CONF_CHANGE_SEQUENCE = "change_sequence"
1517
CONF_COLORS = "colors"
1618
CONF_COLOR = "color"
17-
CONF_COLOR_TEMP = "color_temp"
18-
CONF_COLOR_HS = "hs_color"
19-
CONF_COLOR_XY = "xy_color"
2019
CONF_COLOR_TYPE = "color_type"
2120
CONF_IGNORE_OFF = "ignore_off"
2221
CONF_PLATFORM = "platform"

0 commit comments

Comments
 (0)