diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index 1038fef7a30e..5bf72b7267bd 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -911,11 +911,20 @@ class LightEntity(ToggleEntity): supported_color_modes = self._light_internal_supported_color_modes if ColorMode.COLOR_TEMP in supported_color_modes: - data[ATTR_MIN_MIREDS] = self.min_mireds - data[ATTR_MAX_MIREDS] = self.max_mireds data[ATTR_MIN_COLOR_TEMP_KELVIN] = self.min_color_temp_kelvin data[ATTR_MAX_COLOR_TEMP_KELVIN] = self.max_color_temp_kelvin - + if not self.max_color_temp_kelvin: + data[ATTR_MIN_MIREDS] = None + else: + data[ATTR_MIN_MIREDS] = color_util.color_temperature_kelvin_to_mired( + self.max_color_temp_kelvin + ) + if not self.min_color_temp_kelvin: + data[ATTR_MAX_MIREDS] = None + else: + data[ATTR_MAX_MIREDS] = color_util.color_temperature_kelvin_to_mired( + self.min_color_temp_kelvin + ) if supported_features & LightEntityFeature.EFFECT: data[ATTR_EFFECT_LIST] = self.effect_list diff --git a/tests/components/group/test_light.py b/tests/components/group/test_light.py index 74dd74759f4c..3ba4aaaad81a 100644 --- a/tests/components/group/test_light.py +++ b/tests/components/group/test_light.py @@ -821,13 +821,13 @@ async def test_min_max_mireds(hass, enable_custom_integrations): entity0.supported_color_modes = {ColorMode.COLOR_TEMP} entity0.color_mode = ColorMode.COLOR_TEMP entity0.color_temp_kelvin = 2 - entity0.min_color_temp_kelvin = 2 - entity0.max_color_temp_kelvin = 5 + entity0._attr_min_color_temp_kelvin = 2 + entity0._attr_max_color_temp_kelvin = 5 entity1 = platform.ENTITIES[1] entity1.supported_features = SUPPORT_COLOR_TEMP - entity1.min_color_temp_kelvin = 1 - entity1.max_color_temp_kelvin = 1234567890 + entity1._attr_min_color_temp_kelvin = 1 + entity1._attr_max_color_temp_kelvin = 1234567890 assert await async_setup_component( hass, diff --git a/tests/components/light/test_init.py b/tests/components/light/test_init.py index 25156eef9ca5..bff46af29e92 100644 --- a/tests/components/light/test_init.py +++ b/tests/components/light/test_init.py @@ -1903,6 +1903,10 @@ async def test_light_service_call_color_temp_conversion( light.ColorMode.COLOR_TEMP, light.ColorMode.RGBWW, ] + assert state.attributes["min_mireds"] == 153 + assert state.attributes["max_mireds"] == 500 + assert state.attributes["min_color_temp_kelvin"] == 2000 + assert state.attributes["max_color_temp_kelvin"] == 6500 state = hass.states.get(entity1.entity_id) assert state.attributes["supported_color_modes"] == [light.ColorMode.RGBWW] @@ -2001,6 +2005,52 @@ async def test_light_service_call_color_temp_conversion( assert data == {"brightness": 255, "rgbww_color": (0, 0, 0, 66, 189)} +async def test_light_mired_color_temp_conversion(hass, enable_custom_integrations): + """Test color temp conversion from K to legacy mired.""" + platform = getattr(hass.components, "test.light") + platform.init(empty=True) + + platform.ENTITIES.append(platform.MockLight("Test_rgbww_ct", STATE_ON)) + platform.ENTITIES.append(platform.MockLight("Test_rgbww", STATE_ON)) + + entity0 = platform.ENTITIES[0] + entity0.supported_color_modes = { + light.ColorMode.COLOR_TEMP, + } + entity0._attr_min_color_temp_kelvin = 1800 + entity0._attr_max_color_temp_kelvin = 6700 + + assert await async_setup_component(hass, "light", {"light": {"platform": "test"}}) + await hass.async_block_till_done() + + state = hass.states.get(entity0.entity_id) + assert state.attributes["supported_color_modes"] == [light.ColorMode.COLOR_TEMP] + assert state.attributes["min_mireds"] == 149 + assert state.attributes["max_mireds"] == 555 + assert state.attributes["min_color_temp_kelvin"] == 1800 + assert state.attributes["max_color_temp_kelvin"] == 6700 + + await hass.services.async_call( + "light", + "turn_on", + { + "entity_id": [ + entity0.entity_id, + ], + "brightness_pct": 100, + "color_temp_kelvin": 3500, + }, + blocking=True, + ) + _, data = entity0.last_call("turn_on") + assert data == {"brightness": 255, "color_temp": 285, "color_temp_kelvin": 3500} + + state = hass.states.get(entity0.entity_id) + assert state.attributes["color_mode"] == light.ColorMode.COLOR_TEMP + assert state.attributes["color_temp"] == 285 + assert state.attributes["color_temp_kelvin"] == 3500 + + async def test_light_service_call_white_mode(hass, enable_custom_integrations): """Test color_mode white in service calls.""" platform = getattr(hass.components, "test.light") diff --git a/tests/testing_config/custom_components/test/light.py b/tests/testing_config/custom_components/test/light.py index c4b72e6405a9..3c78e7acce36 100644 --- a/tests/testing_config/custom_components/test/light.py +++ b/tests/testing_config/custom_components/test/light.py @@ -37,8 +37,8 @@ class MockLight(MockToggleEntity, LightEntity): """Mock light class.""" color_mode = None - max_color_temp_kelvin = 6500 - min_color_temp_kelvin = 2000 + _attr_max_color_temp_kelvin = 6500 + _attr_min_color_temp_kelvin = 2000 supported_color_modes = None supported_features = 0