From 659bafa99e545fedce126565d042a2b1ba11fa1a Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 9 Oct 2026 12:16:13 +0200 Subject: [PATCH] Fix Tradfri color bulbs losing HS color mode (#185197) --- homeassistant/components/tradfri/light.py | 15 ++++++---- .../tradfri/fixtures/bulb_cws_color_temp.json | 29 +++++++++++++++++++ tests/components/tradfri/test_light.py | 24 +++++++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 tests/components/tradfri/fixtures/bulb_cws_color_temp.json diff --git a/homeassistant/components/tradfri/light.py b/homeassistant/components/tradfri/light.py index 0cc9cfafde3b..4911e27e8b56 100644 --- a/homeassistant/components/tradfri/light.py +++ b/homeassistant/components/tradfri/light.py @@ -74,7 +74,7 @@ class TradfriLight(TradfriBaseEntity, LightEntity): # Calculate supported color modes modes: set[ColorMode] = {ColorMode.ONOFF} - if self._device_data.supports_hsb_xy_color: + if self._supports_hs_color: modes.add(ColorMode.HS) if self._device_data.supports_color_temp: modes.add(ColorMode.COLOR_TEMP) @@ -91,6 +91,12 @@ class TradfriLight(TradfriBaseEntity, LightEntity): self._device_control.max_mireds ) + @property + def _supports_hs_color(self) -> bool: + """Return if the light supports setting a hue and saturation.""" + # supports_hsb_xy_color is False for color bulbs that also report mireds + return self._device_data.raw.color_hue is not None + @override def _refresh(self) -> None: """Refresh the device.""" @@ -179,7 +185,7 @@ class TradfriLight(TradfriBaseEntity, LightEntity): dimmer_command = self._device_control.set_state(True) color_command = None - if ATTR_HS_COLOR in kwargs and self._device_data.supports_hsb_xy_color: + if ATTR_HS_COLOR in kwargs and self._supports_hs_color: hue = int(kwargs[ATTR_HS_COLOR][0] * (self._device_control.max_hue / 360)) sat = int( kwargs[ATTR_HS_COLOR][1] * (self._device_control.max_saturation / 100) @@ -191,8 +197,7 @@ class TradfriLight(TradfriBaseEntity, LightEntity): temp_command = None if ATTR_COLOR_TEMP_KELVIN in kwargs and ( - self._device_data.supports_color_temp - or self._device_data.supports_hsb_xy_color + self._device_data.supports_color_temp or self._supports_hs_color ): temp_k = kwargs[ATTR_COLOR_TEMP_KELVIN] # White Spectrum bulb @@ -208,7 +213,7 @@ class TradfriLight(TradfriBaseEntity, LightEntity): transition_time = None # Color bulb (CWS) # color_temp needs to be set with hue/saturation - elif self._device_data.supports_hsb_xy_color: + elif self._supports_hs_color: hs_color = color_util.color_temperature_to_hs(temp_k) hue = int(hs_color[0] * (self._device_control.max_hue / 360)) sat = int(hs_color[1] * (self._device_control.max_saturation / 100)) diff --git a/tests/components/tradfri/fixtures/bulb_cws_color_temp.json b/tests/components/tradfri/fixtures/bulb_cws_color_temp.json new file mode 100644 index 000000000000..95faab65f3df --- /dev/null +++ b/tests/components/tradfri/fixtures/bulb_cws_color_temp.json @@ -0,0 +1,29 @@ +{ + "3": { + "0": "IKEA of Sweden", + "1": "TRADFRI bulb GU10 CWS 345lm", + "2": "", + "3": "1.3.002", + "6": 1 + }, + "3311": [ + { + "5706": "f1e0b5", + "5707": 5427, + "5708": 42596, + "5709": 30015, + "5710": 26870, + "5711": 370, + "5850": 1, + "5851": 250, + "9003": 0 + } + ], + "5750": 2, + "9001": "Test CWS color temp", + "9002": 1509924799, + "9003": 65542, + "9019": 1, + "9020": 1510011206, + "9054": 0 +} diff --git a/tests/components/tradfri/test_light.py b/tests/components/tradfri/test_light.py index 43f3b436f5e1..6e7e81f3d1f2 100644 --- a/tests/components/tradfri/test_light.py +++ b/tests/components/tradfri/test_light.py @@ -50,6 +50,12 @@ def bulb_cws() -> str: return load_fixture("bulb_cws.json", DOMAIN) +@pytest.fixture(scope="module") +def bulb_cws_color_temp() -> str: + """Return a color bulb that also reports a color temperature.""" + return load_fixture("bulb_cws_color_temp.json", DOMAIN) + + @pytest.mark.parametrize( ("device", "entity_id", "state_attributes"), [ @@ -84,6 +90,16 @@ def bulb_cws() -> str: ATTR_COLOR_MODE: ColorMode.HS, }, ), + ( + "bulb_cws_color_temp", + "light.test_cws_color_temp", + { + ATTR_BRIGHTNESS: 250, + ATTR_HS_COLOR: (29.812, 65.252), + ATTR_SUPPORTED_COLOR_MODES: [ColorMode.COLOR_TEMP, ColorMode.HS], + ATTR_COLOR_MODE: ColorMode.HS, + }, + ), ], indirect=["device"], ) @@ -190,6 +206,13 @@ async def test_light_available( {"hs_color": [300, 100]}, {"hs_color": [300, 100]}, ), + # hs_color on a color bulb that also reports a color temperature + ( + "bulb_cws_color_temp", + "light.test_cws_color_temp", + {"hs_color": [300, 100]}, + {"hs_color": [300, 100]}, + ), # ct + brightness ( "bulb_ws", @@ -229,6 +252,7 @@ async def test_light_available( "color_temp_kelvin > 4000", "color_temp_kelvin < 2202", "hs_color", + "hs_color (color bulb with color temp)", "ct + brightness", "ct + brightness (no temp support)", "ct + brightness (no temp or color support)",