From 40d0a6e99eceedd2ad61f42b7a813745376ca7d8 Mon Sep 17 00:00:00 2001 From: Knodd Date: Mon, 10 Jan 2022 15:21:03 +0100 Subject: [PATCH] Support Tuya strip lights with correct values for saturation and brightness (#63812) Co-authored-by: Franck Nijhof --- homeassistant/components/tuya/light.py | 48 ++++++++++++++------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/tuya/light.py b/homeassistant/components/tuya/light.py index 1459ef94fa9..4198e036d62 100644 --- a/homeassistant/components/tuya/light.py +++ b/homeassistant/components/tuya/light.py @@ -30,6 +30,28 @@ from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode, WorkMode from .util import remap_value +@dataclass +class ColorTypeData: + """Color Type Data.""" + + h_type: IntegerTypeData + s_type: IntegerTypeData + v_type: IntegerTypeData + + +DEFAULT_COLOR_TYPE_DATA = ColorTypeData( + h_type=IntegerTypeData(min=1, scale=0, max=360, step=1), + s_type=IntegerTypeData(min=1, scale=0, max=255, step=1), + v_type=IntegerTypeData(min=1, scale=0, max=255, step=1), +) + +DEFAULT_COLOR_TYPE_DATA_V2 = ColorTypeData( + h_type=IntegerTypeData(min=1, scale=0, max=360, step=1), + s_type=IntegerTypeData(min=1, scale=0, max=1000, step=1), + v_type=IntegerTypeData(min=1, scale=0, max=1000, step=1), +) + + @dataclass class TuyaLightEntityDescription(LightEntityDescription): """Describe an Tuya light entity.""" @@ -40,6 +62,7 @@ class TuyaLightEntityDescription(LightEntityDescription): color_data: DPCode | tuple[DPCode, ...] | None = None color_mode: DPCode | None = None color_temp: DPCode | tuple[DPCode, ...] | None = None + default_color_type: ColorTypeData = DEFAULT_COLOR_TYPE_DATA LIGHTS: dict[str, tuple[TuyaLightEntityDescription, ...]] = { @@ -63,6 +86,7 @@ LIGHTS: dict[str, tuple[TuyaLightEntityDescription, ...]] = { brightness=DPCode.BRIGHT_VALUE, color_temp=DPCode.TEMP_VALUE, color_data=DPCode.COLOUR_DATA, + default_color_type=DEFAULT_COLOR_TYPE_DATA_V2, ), ), # Light @@ -242,28 +266,6 @@ LIGHTS["cz"] = LIGHTS["kg"] LIGHTS["pc"] = LIGHTS["kg"] -@dataclass -class ColorTypeData: - """Color Type Data.""" - - h_type: IntegerTypeData - s_type: IntegerTypeData - v_type: IntegerTypeData - - -DEFAULT_COLOR_TYPE_DATA = ColorTypeData( - h_type=IntegerTypeData(min=1, scale=0, max=360, step=1), - s_type=IntegerTypeData(min=1, scale=0, max=255, step=1), - v_type=IntegerTypeData(min=1, scale=0, max=255, step=1), -) - -DEFAULT_COLOR_TYPE_DATA_V2 = ColorTypeData( - h_type=IntegerTypeData(min=1, scale=0, max=360, step=1), - s_type=IntegerTypeData(min=1, scale=0, max=1000, step=1), - v_type=IntegerTypeData(min=1, scale=0, max=1000, step=1), -) - - @dataclass class ColorData: """Color Data.""" @@ -440,7 +442,7 @@ class TuyaLightEntity(TuyaEntity, LightEntity): ) else: # If no type is found, use a default one - self._color_data_type = DEFAULT_COLOR_TYPE_DATA + self._color_data_type = self.entity_description.default_color_type if self._color_data_dpcode == DPCode.COLOUR_DATA_V2 or ( self._brightness_type and self._brightness_type.max > 255 ):