Fix light color mode in govee_light_local (#108762)

This commit is contained in:
Erik Montnemery 2024-01-29 21:40:21 +01:00 committed by GitHub
parent f1392f8519
commit 7ef3ed6107
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 16 deletions

View file

@ -13,6 +13,7 @@ from homeassistant.components.light import (
ATTR_RGB_COLOR,
ColorMode,
LightEntity,
filter_supported_color_modes,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
@ -52,6 +53,8 @@ class GoveeLight(CoordinatorEntity[GoveeLocalApiCoordinator], LightEntity):
_attr_has_entity_name = True
_attr_name = None
_attr_supported_color_modes: set[ColorMode]
_fixed_color_mode: ColorMode | None = None
def __init__(
self,
@ -67,7 +70,7 @@ class GoveeLight(CoordinatorEntity[GoveeLocalApiCoordinator], LightEntity):
self._attr_unique_id = device.fingerprint
capabilities = device.capabilities
color_modes = set()
color_modes = {ColorMode.ONOFF}
if capabilities:
if GoveeLightCapability.COLOR_RGB in capabilities:
color_modes.add(ColorMode.RGB)
@ -77,10 +80,11 @@ class GoveeLight(CoordinatorEntity[GoveeLocalApiCoordinator], LightEntity):
self._attr_min_color_temp_kelvin = 2000
if GoveeLightCapability.BRIGHTNESS in capabilities:
color_modes.add(ColorMode.BRIGHTNESS)
else:
color_modes.add(ColorMode.ONOFF)
self._attr_supported_color_modes = color_modes
self._attr_supported_color_modes = filter_supported_color_modes(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._attr_device_info = DeviceInfo(
identifiers={
@ -116,20 +120,18 @@ class GoveeLight(CoordinatorEntity[GoveeLocalApiCoordinator], LightEntity):
@property
def color_mode(self) -> ColorMode | str | None:
"""Return the color mode."""
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 RGB, determine which
# mode the light is in
if (
self._device.temperature_color is not None
and self._device.temperature_color > 0
):
return ColorMode.COLOR_TEMP
if self._device.rgb_color is not None and any(self._device.rgb_color):
return ColorMode.RGB
if (
self._attr_supported_color_modes
and ColorMode.BRIGHTNESS in self._attr_supported_color_modes
):
return ColorMode.BRIGHTNESS
return ColorMode.ONOFF
return ColorMode.RGB
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the device on."""

View file

@ -46,9 +46,7 @@ async def test_light_known_device(
assert light is not None
color_modes = light.attributes[ATTR_SUPPORTED_COLOR_MODES]
assert ColorMode.RGB in color_modes
assert ColorMode.BRIGHTNESS in color_modes
assert ColorMode.COLOR_TEMP in color_modes
assert set(color_modes) == {ColorMode.COLOR_TEMP, ColorMode.RGB}
# Remove
assert await hass.config_entries.async_remove(entry.entry_id)