Use ColorMode enum in tuya (#70545)

This commit is contained in:
epenet 2022-04-23 23:00:34 +02:00 committed by GitHub
parent 9906fd649e
commit 89200b27f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,10 +11,7 @@ from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP, ATTR_COLOR_TEMP,
ATTR_HS_COLOR, ATTR_HS_COLOR,
COLOR_MODE_BRIGHTNESS, ColorMode,
COLOR_MODE_COLOR_TEMP,
COLOR_MODE_HS,
COLOR_MODE_ONOFF,
LightEntity, LightEntity,
LightEntityDescription, LightEntityDescription,
) )
@ -401,7 +398,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
super().__init__(device, device_manager) super().__init__(device, device_manager)
self.entity_description = description self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}" 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 # Determine DPCodes
self._color_mode_dpcode = self.find_dpcode( self._color_mode_dpcode = self.find_dpcode(
@ -412,7 +409,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
description.brightness, dptype=DPType.INTEGER, prefer_function=True description.brightness, dptype=DPType.INTEGER, prefer_function=True
): ):
self._brightness = int_type 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( self._brightness_max = self.find_dpcode(
description.brightness_max, dptype=DPType.INTEGER description.brightness_max, dptype=DPType.INTEGER
) )
@ -424,13 +421,13 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
description.color_temp, dptype=DPType.INTEGER, prefer_function=True description.color_temp, dptype=DPType.INTEGER, prefer_function=True
): ):
self._color_temp = int_type 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 ( if (
dpcode := self.find_dpcode(description.color_data, prefer_function=True) dpcode := self.find_dpcode(description.color_data, prefer_function=True)
) and self.get_dptype(dpcode) == DPType.JSON: ) and self.get_dptype(dpcode) == DPType.JSON:
self._color_data_dpcode = dpcode 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: if dpcode in self.device.function:
values = cast(str, self.device.function[dpcode].values) values = cast(str, self.device.function[dpcode].values)
else: else:
@ -451,6 +448,9 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
): ):
self._color_data_type = DEFAULT_COLOR_TYPE_DATA_V2 self._color_data_type = DEFAULT_COLOR_TYPE_DATA_V2
if not self._attr_supported_color_modes:
self._attr_supported_color_modes = {ColorMode.ONOFF}
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if light is on.""" """Return true if light is on."""
@ -484,7 +484,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
] ]
elif self._color_data_type and ( elif self._color_data_type and (
ATTR_HS_COLOR in kwargs 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: if self._color_mode_dpcode:
commands += [ commands += [
@ -527,7 +527,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
if ( if (
ATTR_BRIGHTNESS in kwargs ATTR_BRIGHTNESS in kwargs
and self.color_mode != COLOR_MODE_HS and self.color_mode != ColorMode.HS
and self._brightness and self._brightness
): ):
brightness = kwargs[ATTR_BRIGHTNESS] brightness = kwargs[ATTR_BRIGHTNESS]
@ -578,7 +578,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
def brightness(self) -> int | None: def brightness(self) -> int | None:
"""Return the brightness of the light.""" """Return the brightness of the light."""
# If the light is currently in color mode, extract the brightness from the color data # 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 return color_data.brightness
if not self._brightness: if not self._brightness:
@ -640,7 +640,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
return color_data.hs_color return color_data.hs_color
@property @property
def color_mode(self) -> str: def color_mode(self) -> ColorMode:
"""Return the color_mode of the light.""" """Return the color_mode of the light."""
# We consider it to be in HS color mode, when work mode is anything # We consider it to be in HS color mode, when work mode is anything
# else than "white". # else than "white".
@ -648,12 +648,12 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
self._color_mode_dpcode self._color_mode_dpcode
and self.device.status.get(self._color_mode_dpcode) != WorkMode.WHITE and self.device.status.get(self._color_mode_dpcode) != WorkMode.WHITE
): ):
return COLOR_MODE_HS return ColorMode.HS
if self._color_temp: if self._color_temp:
return COLOR_MODE_COLOR_TEMP return ColorMode.COLOR_TEMP
if self._brightness: if self._brightness:
return COLOR_MODE_BRIGHTNESS return ColorMode.BRIGHTNESS
return COLOR_MODE_ONOFF return ColorMode.ONOFF
def _get_color_data(self) -> ColorData | None: def _get_color_data(self) -> ColorData | None:
"""Get current color data from device.""" """Get current color data from device."""