Fix light color mode in tradfri (#108761)

This commit is contained in:
Erik Montnemery 2024-01-24 16:55:18 +01:00 committed by GitHub
parent 4b2b4ae36b
commit aaf1cc818a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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."""