Skip to content

Commit 3c10e9f

Browse files
authored
Fix inverted kelvin issue (home-assistant#158054)
1 parent 2dec3be commit 3c10e9f

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

homeassistant/components/template/light.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -635,14 +635,14 @@ def _update_temperature(self, render):
635635
# Support legacy mireds in template light.
636636
temperature = int(render)
637637
if (min_kelvin := self._attr_min_color_temp_kelvin) is not None:
638-
min_mireds = color_util.color_temperature_kelvin_to_mired(min_kelvin)
638+
max_mireds = color_util.color_temperature_kelvin_to_mired(min_kelvin)
639639
else:
640-
min_mireds = DEFAULT_MIN_MIREDS
640+
max_mireds = DEFAULT_MAX_MIREDS
641641

642642
if (max_kelvin := self._attr_max_color_temp_kelvin) is not None:
643-
max_mireds = color_util.color_temperature_kelvin_to_mired(max_kelvin)
643+
min_mireds = color_util.color_temperature_kelvin_to_mired(max_kelvin)
644644
else:
645-
max_mireds = DEFAULT_MAX_MIREDS
645+
min_mireds = DEFAULT_MIN_MIREDS
646646
if min_mireds <= temperature <= max_mireds:
647647
self._attr_color_temp_kelvin = (
648648
color_util.color_temperature_mired_to_kelvin(temperature)
@@ -856,42 +856,36 @@ def _update_max_mireds(self, render):
856856

857857
try:
858858
if render in (None, "None", ""):
859-
self._attr_max_mireds = DEFAULT_MAX_MIREDS
860-
self._attr_max_color_temp_kelvin = None
859+
self._attr_min_color_temp_kelvin = None
861860
return
862861

863-
self._attr_max_mireds = max_mireds = int(render)
864-
self._attr_max_color_temp_kelvin = (
865-
color_util.color_temperature_mired_to_kelvin(max_mireds)
862+
self._attr_min_color_temp_kelvin = (
863+
color_util.color_temperature_mired_to_kelvin(int(render))
866864
)
867865
except ValueError:
868866
_LOGGER.exception(
869867
"Template must supply an integer temperature within the range for"
870868
" this light, or 'None'"
871869
)
872-
self._attr_max_mireds = DEFAULT_MAX_MIREDS
873-
self._attr_max_color_temp_kelvin = None
870+
self._attr_min_color_temp_kelvin = None
874871

875872
@callback
876873
def _update_min_mireds(self, render):
877874
"""Update the min mireds from the template."""
878875
try:
879876
if render in (None, "None", ""):
880-
self._attr_min_mireds = DEFAULT_MIN_MIREDS
881-
self._attr_min_color_temp_kelvin = None
877+
self._attr_max_color_temp_kelvin = None
882878
return
883879

884-
self._attr_min_mireds = min_mireds = int(render)
885-
self._attr_min_color_temp_kelvin = (
886-
color_util.color_temperature_mired_to_kelvin(min_mireds)
880+
self._attr_max_color_temp_kelvin = (
881+
color_util.color_temperature_mired_to_kelvin(int(render))
887882
)
888883
except ValueError:
889884
_LOGGER.exception(
890885
"Template must supply an integer temperature within the range for"
891886
" this light, or 'None'"
892887
)
893-
self._attr_min_mireds = DEFAULT_MIN_MIREDS
894-
self._attr_min_color_temp_kelvin = None
888+
self._attr_max_color_temp_kelvin = None
895889

896890
@callback
897891
def _update_supports_transition(self, render):

tests/components/template/test_light.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2345,19 +2345,20 @@ async def test_effect_template(
23452345
],
23462346
)
23472347
@pytest.mark.parametrize(
2348-
("expected_min_mireds", "attribute_template"),
2348+
("expected_min_mireds", "expected_max_kelvin", "attribute_template"),
23492349
[
2350-
(118, "{{118}}"),
2351-
(153, "{{x - 12}}"),
2352-
(153, "None"),
2353-
(153, "{{ none }}"),
2354-
(153, ""),
2355-
(153, "{{ 'a' }}"),
2350+
(118, 8474, "{{118}}"),
2351+
(153, 6535, "{{x - 12}}"),
2352+
(153, 6535, "None"),
2353+
(153, 6535, "{{ none }}"),
2354+
(153, 6535, ""),
2355+
(153, 6535, "{{ 'a' }}"),
23562356
],
23572357
)
23582358
async def test_min_mireds_template(
23592359
hass: HomeAssistant,
2360-
expected_min_mireds,
2360+
expected_min_mireds: int,
2361+
expected_max_kelvin: int,
23612362
style: ConfigurationStyle,
23622363
setup_light_with_mireds,
23632364
) -> None:
@@ -2369,6 +2370,7 @@ async def test_min_mireds_template(
23692370
state = hass.states.get("light.test_template_light")
23702371
assert state is not None
23712372
assert state.attributes.get("min_mireds") == expected_min_mireds
2373+
assert state.attributes.get("max_color_temp_kelvin") == expected_max_kelvin
23722374

23732375

23742376
@pytest.mark.parametrize("count", [1])
@@ -2381,19 +2383,20 @@ async def test_min_mireds_template(
23812383
],
23822384
)
23832385
@pytest.mark.parametrize(
2384-
("expected_max_mireds", "attribute_template"),
2386+
("expected_max_mireds", "expected_min_kelvin", "attribute_template"),
23852387
[
2386-
(488, "{{488}}"),
2387-
(500, "{{x - 12}}"),
2388-
(500, "None"),
2389-
(500, "{{ none }}"),
2390-
(500, ""),
2391-
(500, "{{ 'a' }}"),
2388+
(488, 2049, "{{488}}"),
2389+
(500, 2000, "{{x - 12}}"),
2390+
(500, 2000, "None"),
2391+
(500, 2000, "{{ none }}"),
2392+
(500, 2000, ""),
2393+
(500, 2000, "{{ 'a' }}"),
23922394
],
23932395
)
23942396
async def test_max_mireds_template(
23952397
hass: HomeAssistant,
2396-
expected_max_mireds,
2398+
expected_max_mireds: int,
2399+
expected_min_kelvin: int,
23972400
style: ConfigurationStyle,
23982401
setup_light_with_mireds,
23992402
) -> None:
@@ -2405,6 +2408,7 @@ async def test_max_mireds_template(
24052408
state = hass.states.get("light.test_template_light")
24062409
assert state is not None
24072410
assert state.attributes.get("max_mireds") == expected_max_mireds
2411+
assert state.attributes.get("min_color_temp_kelvin") == expected_min_kelvin
24082412

24092413

24102414
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)