Skip to content

Commit 262f06d

Browse files
epenetCopilot
andauthored
Migrate Tuya light (color_temp) to use wrapper class (home-assistant#156743)
Co-authored-by: Copilot <[email protected]>
1 parent bd87119 commit 262f06d

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

homeassistant/components/tuya/light.py

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
DPCodeEnumWrapper,
3535
DPCodeIntegerWrapper,
3636
IntegerTypeData,
37-
find_dpcode,
3837
)
3938
from .util import get_dpcode, get_dptype, remap_value
4039

@@ -108,6 +107,35 @@ def _convert_value_to_raw_value(self, device: CustomerDevice, value: Any) -> Any
108107
return round(self.type_information.remap_value_from(value))
109108

110109

110+
class _ColorTempWrapper(DPCodeIntegerWrapper):
111+
"""Wrapper for color temperature DP code."""
112+
113+
def read_device_status(self, device: CustomerDevice) -> Any | None:
114+
"""Return the color temperature value in Kelvin."""
115+
if (temperature := self._read_device_status_raw(device)) is None:
116+
return None
117+
118+
return color_util.color_temperature_mired_to_kelvin(
119+
self.type_information.remap_value_to(
120+
temperature,
121+
MIN_MIREDS,
122+
MAX_MIREDS,
123+
reverse=True,
124+
)
125+
)
126+
127+
def _convert_value_to_raw_value(self, device: CustomerDevice, value: Any) -> Any:
128+
"""Convert a Home Assistant value (Kelvin) back to a raw device value."""
129+
return round(
130+
self.type_information.remap_value_from(
131+
color_util.color_temperature_kelvin_to_mired(value),
132+
MIN_MIREDS,
133+
MAX_MIREDS,
134+
reverse=True,
135+
)
136+
)
137+
138+
111139
@dataclass
112140
class ColorTypeData:
113141
"""Color Type Data."""
@@ -529,6 +557,9 @@ def async_discover_device(device_ids: list[str]):
529557
color_mode_wrapper=DPCodeEnumWrapper.find_dpcode(
530558
device, description.color_mode, prefer_function=True
531559
),
560+
color_temp_wrapper=_ColorTempWrapper.find_dpcode(
561+
device, description.color_temp, prefer_function=True
562+
),
532563
switch_wrapper=switch_wrapper,
533564
)
534565
for description in descriptions
@@ -555,7 +586,6 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
555586

556587
_color_data_dpcode: DPCode | None = None
557588
_color_data_type: ColorTypeData | None = None
558-
_color_temp: IntegerTypeData | None = None
559589
_white_color_mode = ColorMode.COLOR_TEMP
560590
_fixed_color_mode: ColorMode | None = None
561591
_attr_min_color_temp_kelvin = 2000 # 500 Mireds
@@ -567,8 +597,9 @@ def __init__(
567597
device_manager: Manager,
568598
description: TuyaLightEntityDescription,
569599
*,
570-
brightness_wrapper: DPCodeIntegerWrapper | None,
600+
brightness_wrapper: _BrightnessWrapper | None,
571601
color_mode_wrapper: DPCodeEnumWrapper | None,
602+
color_temp_wrapper: _ColorTempWrapper | None,
572603
switch_wrapper: DPCodeBooleanWrapper,
573604
) -> None:
574605
"""Init TuyaHaLight."""
@@ -577,6 +608,7 @@ def __init__(
577608
self._attr_unique_id = f"{super().unique_id}{description.key}"
578609
self._brightness_wrapper = brightness_wrapper
579610
self._color_mode_wrapper = color_mode_wrapper
611+
self._color_temp_wrapper = color_temp_wrapper
580612
self._switch_wrapper = switch_wrapper
581613

582614
color_modes: set[ColorMode] = {ColorMode.ONOFF}
@@ -611,13 +643,7 @@ def __init__(
611643
self._color_data_type = DEFAULT_COLOR_TYPE_DATA_V2
612644

613645
# Check if the light has color temperature
614-
if int_type := find_dpcode(
615-
self.device,
616-
description.color_temp,
617-
dptype=DPType.INTEGER,
618-
prefer_function=True,
619-
):
620-
self._color_temp = int_type
646+
if color_temp_wrapper:
621647
color_modes.add(ColorMode.COLOR_TEMP)
622648
# If light has color but does not have color_temp, check if it has
623649
# work_mode "white"
@@ -654,21 +680,11 @@ def turn_on(self, **kwargs: Any) -> None:
654680
),
655681
]
656682

657-
if self._color_temp and ATTR_COLOR_TEMP_KELVIN in kwargs:
683+
if self._color_temp_wrapper and ATTR_COLOR_TEMP_KELVIN in kwargs:
658684
commands += [
659-
{
660-
"code": self._color_temp.dpcode,
661-
"value": round(
662-
self._color_temp.remap_value_from(
663-
color_util.color_temperature_kelvin_to_mired(
664-
kwargs[ATTR_COLOR_TEMP_KELVIN]
665-
),
666-
MIN_MIREDS,
667-
MAX_MIREDS,
668-
reverse=True,
669-
)
670-
),
671-
},
685+
self._color_temp_wrapper.get_update_command(
686+
self.device, kwargs[ATTR_COLOR_TEMP_KELVIN]
687+
)
672688
]
673689

674690
if self._color_data_type and (
@@ -748,18 +764,7 @@ def brightness(self) -> int | None:
748764
@property
749765
def color_temp_kelvin(self) -> int | None:
750766
"""Return the color temperature value in Kelvin."""
751-
if not self._color_temp:
752-
return None
753-
754-
temperature = self.device.status.get(self._color_temp.dpcode)
755-
if temperature is None:
756-
return None
757-
758-
return color_util.color_temperature_mired_to_kelvin(
759-
self._color_temp.remap_value_to(
760-
temperature, MIN_MIREDS, MAX_MIREDS, reverse=True
761-
)
762-
)
767+
return self._read_wrapper(self._color_temp_wrapper)
763768

764769
@property
765770
def hs_color(self) -> tuple[float, float] | None:

0 commit comments

Comments
 (0)