From 89200b27f01c4f1048273b2348875d329a494315 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 23 Apr 2022 23:00:34 +0200 Subject: [PATCH] Use ColorMode enum in tuya (#70545) --- homeassistant/components/tuya/light.py | 32 +++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/tuya/light.py b/homeassistant/components/tuya/light.py index c4f874d0687..9601aa0a910 100644 --- a/homeassistant/components/tuya/light.py +++ b/homeassistant/components/tuya/light.py @@ -11,10 +11,7 @@ from homeassistant.components.light import ( ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_HS_COLOR, - COLOR_MODE_BRIGHTNESS, - COLOR_MODE_COLOR_TEMP, - COLOR_MODE_HS, - COLOR_MODE_ONOFF, + ColorMode, LightEntity, LightEntityDescription, ) @@ -401,7 +398,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 = {COLOR_MODE_ONOFF} + self._attr_supported_color_modes = set() # Determine DPCodes self._color_mode_dpcode = self.find_dpcode( @@ -412,7 +409,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): description.brightness, dptype=DPType.INTEGER, prefer_function=True ): self._brightness = int_type - self._attr_supported_color_modes.add(COLOR_MODE_BRIGHTNESS) + self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS) self._brightness_max = self.find_dpcode( description.brightness_max, dptype=DPType.INTEGER ) @@ -424,13 +421,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(COLOR_MODE_COLOR_TEMP) + self._attr_supported_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(COLOR_MODE_HS) + self._attr_supported_color_modes.add(ColorMode.HS) if dpcode in self.device.function: values = cast(str, self.device.function[dpcode].values) else: @@ -451,6 +448,9 @@ 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} + @property def is_on(self) -> bool: """Return true if light is on.""" @@ -484,7 +484,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): ] elif self._color_data_type and ( ATTR_HS_COLOR in kwargs - or (ATTR_BRIGHTNESS in kwargs and self.color_mode == COLOR_MODE_HS) + or (ATTR_BRIGHTNESS in kwargs and self.color_mode == ColorMode.HS) ): if self._color_mode_dpcode: commands += [ @@ -527,7 +527,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): if ( ATTR_BRIGHTNESS in kwargs - and self.color_mode != COLOR_MODE_HS + and self.color_mode != ColorMode.HS and self._brightness ): brightness = kwargs[ATTR_BRIGHTNESS] @@ -578,7 +578,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): def brightness(self) -> int | None: """Return the brightness of the light.""" # If the light is currently in color mode, extract the brightness from the color data - if self.color_mode == COLOR_MODE_HS and (color_data := self._get_color_data()): + if self.color_mode == ColorMode.HS and (color_data := self._get_color_data()): return color_data.brightness if not self._brightness: @@ -640,7 +640,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): return color_data.hs_color @property - def color_mode(self) -> str: + 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 # else than "white". @@ -648,12 +648,12 @@ class TuyaLightEntity(TuyaEntity, LightEntity): self._color_mode_dpcode and self.device.status.get(self._color_mode_dpcode) != WorkMode.WHITE ): - return COLOR_MODE_HS + return ColorMode.HS if self._color_temp: - return COLOR_MODE_COLOR_TEMP + return ColorMode.COLOR_TEMP if self._brightness: - return COLOR_MODE_BRIGHTNESS - return COLOR_MODE_ONOFF + return ColorMode.BRIGHTNESS + return ColorMode.ONOFF def _get_color_data(self) -> ColorData | None: """Get current color data from device."""