From 770e48d51265d9c613cadd066d7f53d9b0e2498d Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 6 Mar 2024 18:28:12 +0100 Subject: [PATCH] Simplify color mode logic in Tuya light (#110327) * Simplify color mode logic in Tuya light * Remove exclusion from LightEntity.__should_report_light_issue * Fix test --- homeassistant/components/light/__init__.py | 4 +-- homeassistant/components/tuya/light.py | 29 +++++++++++++--------- tests/components/light/test_init.py | 2 +- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index 795975b5c3e..93a3b23f504 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -1336,5 +1336,5 @@ class LightEntity(ToggleEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_): """Return if light color mode issues should be reported.""" if not self.platform: return True - # philips_js and tuya have known issues, we don't need users to open issues - return self.platform.platform_name not in {"philips_js", "tuya"} + # philips_js has known issues, we don't need users to open issues + return self.platform.platform_name not in {"philips_js"} diff --git a/homeassistant/components/tuya/light.py b/homeassistant/components/tuya/light.py index 98d704326ae..55833d50bdd 100644 --- a/homeassistant/components/tuya/light.py +++ b/homeassistant/components/tuya/light.py @@ -14,6 +14,7 @@ from homeassistant.components.light import ( ColorMode, LightEntity, LightEntityDescription, + filter_supported_color_modes, ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory @@ -442,6 +443,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): _color_data_type: ColorTypeData | None = None _color_mode: DPCode | None = None _color_temp: IntegerTypeData | None = None + _fixed_color_mode: ColorMode | None = None def __init__( self, @@ -453,7 +455,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): super().__init__(device, device_manager) self.entity_description = description self._attr_unique_id = f"{super().unique_id}{description.key}" - self._attr_supported_color_modes: set[ColorMode] = set() + color_modes: set[ColorMode] = {ColorMode.ONOFF} # Determine DPCodes self._color_mode_dpcode = self.find_dpcode( @@ -464,7 +466,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): description.brightness, dptype=DPType.INTEGER, prefer_function=True ): self._brightness = int_type - self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS) + color_modes.add(ColorMode.BRIGHTNESS) self._brightness_max = self.find_dpcode( description.brightness_max, dptype=DPType.INTEGER ) @@ -476,13 +478,13 @@ class TuyaLightEntity(TuyaEntity, LightEntity): description.color_temp, dptype=DPType.INTEGER, prefer_function=True ): self._color_temp = int_type - self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP) + color_modes.add(ColorMode.COLOR_TEMP) if ( dpcode := self.find_dpcode(description.color_data, prefer_function=True) ) and self.get_dptype(dpcode) == DPType.JSON: self._color_data_dpcode = dpcode - self._attr_supported_color_modes.add(ColorMode.HS) + color_modes.add(ColorMode.HS) if dpcode in self.device.function: values = cast(str, self.device.function[dpcode].values) else: @@ -503,8 +505,10 @@ class TuyaLightEntity(TuyaEntity, LightEntity): ): self._color_data_type = DEFAULT_COLOR_TYPE_DATA_V2 - if not self._attr_supported_color_modes: - self._attr_supported_color_modes = {ColorMode.ONOFF} + 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)) @property def is_on(self) -> bool: @@ -698,18 +702,19 @@ class TuyaLightEntity(TuyaEntity, LightEntity): @property def color_mode(self) -> ColorMode: """Return the color_mode of the light.""" - # We consider it to be in HS color mode, when work mode is anything + 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 HS, determine which mode the + # light is in. We consider it to be in HS color mode, when work mode is anything # else than "white". if ( self._color_mode_dpcode and self.device.status.get(self._color_mode_dpcode) != WorkMode.WHITE ): return ColorMode.HS - if self._color_temp: - return ColorMode.COLOR_TEMP - if self._brightness: - return ColorMode.BRIGHTNESS - return ColorMode.ONOFF + return ColorMode.COLOR_TEMP def _get_color_data(self) -> ColorData | None: """Get current color data from device.""" diff --git a/tests/components/light/test_init.py b/tests/components/light/test_init.py index ca25611f890..b2292f59bb1 100644 --- a/tests/components/light/test_init.py +++ b/tests/components/light/test_init.py @@ -2791,7 +2791,7 @@ def test_report_invalid_color_mode( ( light.ColorMode.ONOFF, {light.ColorMode.ONOFF, light.ColorMode.BRIGHTNESS}, - "tuya", # We don't log issues for tuya + "philips_js", # We don't log issues for philips_js False, ), ],