From aaf1cc818a06140c9b5a42fae528cea6a467f2dd Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 24 Jan 2024 16:55:18 +0100 Subject: [PATCH] Fix light color mode in tradfri (#108761) --- homeassistant/components/tradfri/light.py | 29 +++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/tradfri/light.py b/homeassistant/components/tradfri/light.py index df35301b373..769c8f6f9e1 100644 --- a/homeassistant/components/tradfri/light.py +++ b/homeassistant/components/tradfri/light.py @@ -14,6 +14,7 @@ from homeassistant.components.light import ( ColorMode, LightEntity, LightEntityFeature, + filter_supported_color_modes, ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant @@ -51,6 +52,7 @@ class TradfriLight(TradfriBaseEntity, LightEntity): _attr_name = None _attr_supported_features = LightEntityFeature.TRANSITION + _fixed_color_mode: ColorMode | None = None def __init__( self, @@ -72,18 +74,16 @@ class TradfriLight(TradfriBaseEntity, LightEntity): self._hs_color = None # Calculate supported color modes - self._attr_supported_color_modes: set[ColorMode] = set() + modes: set[ColorMode] = {ColorMode.ONOFF} if self._device.light_control.can_set_color: - self._attr_supported_color_modes.add(ColorMode.HS) + modes.add(ColorMode.HS) if self._device.light_control.can_set_temp: - self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP) - if ( - not self._attr_supported_color_modes - and self._device.light_control.can_set_dimmer - ): - # Must be the only supported mode according to docs for - # ColorMode.BRIGHTNESS - self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS) + modes.add(ColorMode.COLOR_TEMP) + if self._device.light_control.can_set_dimmer: + modes.add(ColorMode.BRIGHTNESS) + self._attr_supported_color_modes = filter_supported_color_modes(modes) + if len(self._attr_supported_color_modes) == 1: + self._fixed_color_mode = next(iter(self._attr_supported_color_modes)) if self._device_control: self._attr_min_mireds = self._device_control.min_mireds @@ -100,6 +100,15 @@ class TradfriLight(TradfriBaseEntity, LightEntity): return False return cast(bool, self._device_data.state) + @property + def color_mode(self) -> ColorMode | None: + """Return the color mode of the light.""" + if self._fixed_color_mode: + return self._fixed_color_mode + if self.hs_color: + return ColorMode.HS + return ColorMode.COLOR_TEMP + @property def brightness(self) -> int | None: """Return the brightness of the light."""