Use ColorMode enum in hue (#70578)

This commit is contained in:
epenet 2022-04-24 21:29:51 +02:00 committed by GitHub
parent 8330d7906a
commit 965665213f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 26 deletions

View File

@ -14,13 +14,10 @@ from homeassistant.components.light import (
ATTR_FLASH, ATTR_FLASH,
ATTR_TRANSITION, ATTR_TRANSITION,
ATTR_XY_COLOR, ATTR_XY_COLOR,
COLOR_MODE_BRIGHTNESS,
COLOR_MODE_COLOR_TEMP,
COLOR_MODE_ONOFF,
COLOR_MODE_XY,
FLASH_SHORT, FLASH_SHORT,
SUPPORT_FLASH, SUPPORT_FLASH,
SUPPORT_TRANSITION, SUPPORT_TRANSITION,
ColorMode,
LightEntity, LightEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -204,7 +201,7 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
@callback @callback
def _update_values(self) -> None: def _update_values(self) -> None:
"""Set base values from underlying lights of a group.""" """Set base values from underlying lights of a group."""
supported_color_modes = set() supported_color_modes: set[ColorMode | str] = set()
lights_with_color_support = 0 lights_with_color_support = 0
lights_with_color_temp_support = 0 lights_with_color_temp_support = 0
lights_with_dimming_support = 0 lights_with_dimming_support = 0
@ -241,18 +238,18 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
# this means that the state is derived from only some of the lights # this means that the state is derived from only some of the lights
# and will never be 100% accurate but it will be close # and will never be 100% accurate but it will be close
if lights_with_color_support > 0: if lights_with_color_support > 0:
supported_color_modes.add(COLOR_MODE_XY) supported_color_modes.add(ColorMode.XY)
if lights_with_color_temp_support > 0: if lights_with_color_temp_support > 0:
supported_color_modes.add(COLOR_MODE_COLOR_TEMP) supported_color_modes.add(ColorMode.COLOR_TEMP)
if lights_with_dimming_support > 0: if lights_with_dimming_support > 0:
if len(supported_color_modes) == 0: if len(supported_color_modes) == 0:
# only add color mode brightness if no color variants # only add color mode brightness if no color variants
supported_color_modes.add(COLOR_MODE_BRIGHTNESS) supported_color_modes.add(ColorMode.BRIGHTNESS)
self._attr_brightness = round( self._attr_brightness = round(
((total_brightness / lights_with_dimming_support) / 100) * 255 ((total_brightness / lights_with_dimming_support) / 100) * 255
) )
else: else:
supported_color_modes.add(COLOR_MODE_ONOFF) supported_color_modes.add(ColorMode.ONOFF)
self._dynamic_mode_active = lights_in_dynamic_mode > 0 self._dynamic_mode_active = lights_in_dynamic_mode > 0
self._attr_supported_color_modes = supported_color_modes self._attr_supported_color_modes = supported_color_modes
# pick a winner for the current colormode # pick a winner for the current colormode
@ -260,10 +257,10 @@ class GroupedHueLight(HueBaseEntity, LightEntity):
lights_with_color_temp_support > 0 lights_with_color_temp_support > 0
and lights_in_colortemp_mode == lights_with_color_temp_support and lights_in_colortemp_mode == lights_with_color_temp_support
): ):
self._attr_color_mode = COLOR_MODE_COLOR_TEMP self._attr_color_mode = ColorMode.COLOR_TEMP
elif lights_with_color_support > 0: elif lights_with_color_support > 0:
self._attr_color_mode = COLOR_MODE_XY self._attr_color_mode = ColorMode.XY
elif lights_with_dimming_support > 0: elif lights_with_dimming_support > 0:
self._attr_color_mode = COLOR_MODE_BRIGHTNESS self._attr_color_mode = ColorMode.BRIGHTNESS
else: else:
self._attr_color_mode = COLOR_MODE_ONOFF self._attr_color_mode = ColorMode.ONOFF

View File

@ -16,14 +16,11 @@ from homeassistant.components.light import (
ATTR_FLASH, ATTR_FLASH,
ATTR_TRANSITION, ATTR_TRANSITION,
ATTR_XY_COLOR, ATTR_XY_COLOR,
COLOR_MODE_BRIGHTNESS,
COLOR_MODE_COLOR_TEMP,
COLOR_MODE_ONOFF,
COLOR_MODE_XY,
FLASH_SHORT, FLASH_SHORT,
SUPPORT_EFFECT, SUPPORT_EFFECT,
SUPPORT_FLASH, SUPPORT_FLASH,
SUPPORT_TRANSITION, SUPPORT_TRANSITION,
ColorMode,
LightEntity, LightEntity,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
@ -80,15 +77,15 @@ class HueLight(HueBaseEntity, LightEntity):
self._attr_supported_features |= SUPPORT_FLASH self._attr_supported_features |= SUPPORT_FLASH
self.resource = resource self.resource = resource
self.controller = controller self.controller = controller
self._supported_color_modes = set() self._supported_color_modes: set[ColorMode | str] = set()
if self.resource.supports_color: if self.resource.supports_color:
self._supported_color_modes.add(COLOR_MODE_XY) self._supported_color_modes.add(ColorMode.XY)
if self.resource.supports_color_temperature: if self.resource.supports_color_temperature:
self._supported_color_modes.add(COLOR_MODE_COLOR_TEMP) self._supported_color_modes.add(ColorMode.COLOR_TEMP)
if self.resource.supports_dimming: if self.resource.supports_dimming:
if len(self._supported_color_modes) == 0: if len(self._supported_color_modes) == 0:
# only add color mode brightness if no color variants # only add color mode brightness if no color variants
self._supported_color_modes.add(COLOR_MODE_BRIGHTNESS) self._supported_color_modes.add(ColorMode.BRIGHTNESS)
# support transition if brightness control # support transition if brightness control
self._attr_supported_features |= SUPPORT_TRANSITION self._attr_supported_features |= SUPPORT_TRANSITION
# get list of supported effects (combine effects and timed_effects) # get list of supported effects (combine effects and timed_effects)
@ -121,18 +118,18 @@ class HueLight(HueBaseEntity, LightEntity):
return self.resource.on.on return self.resource.on.on
@property @property
def color_mode(self) -> str | None: def color_mode(self) -> ColorMode:
"""Return the color mode of the light.""" """Return the color mode of the light."""
if color_temp := self.resource.color_temperature: if color_temp := self.resource.color_temperature:
# Hue lights return `mired_valid` to indicate CT is active # Hue lights return `mired_valid` to indicate CT is active
if color_temp.mirek_valid and color_temp.mirek is not None: if color_temp.mirek_valid and color_temp.mirek is not None:
return COLOR_MODE_COLOR_TEMP return ColorMode.COLOR_TEMP
if self.resource.supports_color: if self.resource.supports_color:
return COLOR_MODE_XY return ColorMode.XY
if self.resource.supports_dimming: if self.resource.supports_dimming:
return COLOR_MODE_BRIGHTNESS return ColorMode.BRIGHTNESS
# fallback to on_off # fallback to on_off
return COLOR_MODE_ONOFF return ColorMode.ONOFF
@property @property
def xy_color(self) -> tuple[float, float] | None: def xy_color(self) -> tuple[float, float] | None: