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
This commit is contained in:
Erik Montnemery 2024-03-06 18:28:12 +01:00 committed by GitHub
parent c8f39911cc
commit 770e48d512
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 20 additions and 15 deletions

View file

@ -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"}

View file

@ -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
def _get_color_data(self) -> ColorData | None:
"""Get current color data from device."""

View file

@ -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,
),
],