Simplify Hue v2 color mode calculation (#109857)

pull/109878/head
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
1 changed files with 18 additions and 20 deletions

View File

@ -21,6 +21,7 @@ from homeassistant.components.light import (
LightEntity,
LightEntityDescription,
LightEntityFeature,
filter_supported_color_modes,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
@ -70,6 +71,7 @@ async def async_setup_entry(
class HueLight(HueBaseEntity, LightEntity):
"""Representation of a Hue light."""
_fixed_color_mode: ColorMode | None = None
entity_description = LightEntityDescription(
key="hue_light", has_entity_name=True, name=None
)
@ -83,20 +85,20 @@ class HueLight(HueBaseEntity, LightEntity):
self._attr_supported_features |= LightEntityFeature.FLASH
self.resource = resource
self.controller = controller
self._supported_color_modes: set[ColorMode | str] = set()
supported_color_modes = {ColorMode.ONOFF}
if self.resource.supports_color:
self._supported_color_modes.add(ColorMode.XY)
supported_color_modes.add(ColorMode.XY)
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 len(self._supported_color_modes) == 0:
# only add color mode brightness if no color variants
self._supported_color_modes.add(ColorMode.BRIGHTNESS)
supported_color_modes.add(ColorMode.BRIGHTNESS)
# support transition if brightness control
self._attr_supported_features |= LightEntityFeature.TRANSITION
if len(self._supported_color_modes) == 0:
# only add onoff colormode as fallback
self._supported_color_modes.add(ColorMode.ONOFF)
supported_color_modes = filter_supported_color_modes(supported_color_modes)
self._attr_supported_color_modes = supported_color_modes
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._color_temp_active: bool = False
# get list of supported effects (combine effects and timed_effects)
@ -131,14 +133,15 @@ class HueLight(HueBaseEntity, LightEntity):
@property
def color_mode(self) -> ColorMode:
"""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:
return ColorMode.COLOR_TEMP
if self.resource.supports_color:
return ColorMode.XY
if self.resource.supports_dimming:
return ColorMode.BRIGHTNESS
# fallback to on_off
return ColorMode.ONOFF
return ColorMode.XY
@property
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 FALLBACK_MAX_MIREDS
@property
def supported_color_modes(self) -> set | None:
"""Flag supported features."""
return self._supported_color_modes
@property
def extra_state_attributes(self) -> dict[str, str] | None:
"""Return the optional state attributes."""