Simplify Hue v2 color mode calculation (#109857)

This commit is contained in:
Erik Montnemery 2024-02-07 08:56:35 +01:00 committed by GitHub
parent 2fc56ff4e4
commit 586b4ab93d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -21,6 +21,7 @@ from homeassistant.components.light import (
LightEntity, LightEntity,
LightEntityDescription, LightEntityDescription,
LightEntityFeature, LightEntityFeature,
filter_supported_color_modes,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -70,6 +71,7 @@ async def async_setup_entry(
class HueLight(HueBaseEntity, LightEntity): class HueLight(HueBaseEntity, LightEntity):
"""Representation of a Hue light.""" """Representation of a Hue light."""
_fixed_color_mode: ColorMode | None = None
entity_description = LightEntityDescription( entity_description = LightEntityDescription(
key="hue_light", has_entity_name=True, name=None key="hue_light", has_entity_name=True, name=None
) )
@ -83,20 +85,20 @@ class HueLight(HueBaseEntity, LightEntity):
self._attr_supported_features |= LightEntityFeature.FLASH self._attr_supported_features |= LightEntityFeature.FLASH
self.resource = resource self.resource = resource
self.controller = controller self.controller = controller
self._supported_color_modes: set[ColorMode | str] = set() supported_color_modes = {ColorMode.ONOFF}
if self.resource.supports_color: if self.resource.supports_color:
self._supported_color_modes.add(ColorMode.XY) supported_color_modes.add(ColorMode.XY)
if self.resource.supports_color_temperature: if self.resource.supports_color_temperature:
self._supported_color_modes.add(ColorMode.COLOR_TEMP) supported_color_modes.add(ColorMode.COLOR_TEMP)
if self.resource.supports_dimming: if self.resource.supports_dimming:
if len(self._supported_color_modes) == 0: supported_color_modes.add(ColorMode.BRIGHTNESS)
# only add color mode brightness if no color variants
self._supported_color_modes.add(ColorMode.BRIGHTNESS)
# support transition if brightness control # support transition if brightness control
self._attr_supported_features |= LightEntityFeature.TRANSITION self._attr_supported_features |= LightEntityFeature.TRANSITION
if len(self._supported_color_modes) == 0: supported_color_modes = filter_supported_color_modes(supported_color_modes)
# only add onoff colormode as fallback self._attr_supported_color_modes = supported_color_modes
self._supported_color_modes.add(ColorMode.ONOFF) if len(self._attr_supported_color_modes) == 1:
# If the light supports only a single color mode, set it now
self._fixed_color_mode = next(iter(self._attr_supported_color_modes))
self._last_brightness: float | None = None self._last_brightness: float | None = None
self._color_temp_active: bool = False self._color_temp_active: bool = False
# get list of supported effects (combine effects and timed_effects) # get list of supported effects (combine effects and timed_effects)
@ -131,14 +133,15 @@ class HueLight(HueBaseEntity, LightEntity):
@property @property
def color_mode(self) -> ColorMode: def color_mode(self) -> ColorMode:
"""Return the color mode of the light.""" """Return the color mode of the light."""
if self._fixed_color_mode:
# The light supports only a single color mode, return it
return self._fixed_color_mode
# The light supports both color temperature and XY, determine which
# mode the light is in
if self.color_temp_active: if self.color_temp_active:
return ColorMode.COLOR_TEMP return ColorMode.COLOR_TEMP
if self.resource.supports_color: return ColorMode.XY
return ColorMode.XY
if self.resource.supports_dimming:
return ColorMode.BRIGHTNESS
# fallback to on_off
return ColorMode.ONOFF
@property @property
def color_temp_active(self) -> bool: def color_temp_active(self) -> bool:
@ -183,11 +186,6 @@ class HueLight(HueBaseEntity, LightEntity):
# return a fallback value to prevent issues with mired->kelvin conversions # return a fallback value to prevent issues with mired->kelvin conversions
return FALLBACK_MAX_MIREDS return FALLBACK_MAX_MIREDS
@property
def supported_color_modes(self) -> set | None:
"""Flag supported features."""
return self._supported_color_modes
@property @property
def extra_state_attributes(self) -> dict[str, str] | None: def extra_state_attributes(self) -> dict[str, str] | None:
"""Return the optional state attributes.""" """Return the optional state attributes."""