Skip to content

Commit 0c03e8d

Browse files
authored
Migrate Tuya light (color_mode) to use wrapper class (home-assistant#156582)
1 parent 47cf4e3 commit 0c03e8d

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

homeassistant/components/tuya/light.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@
2929
from . import TuyaConfigEntry
3030
from .const import TUYA_DISCOVERY_NEW, DeviceCategory, DPCode, DPType, WorkMode
3131
from .entity import TuyaEntity
32-
from .models import DPCodeBooleanWrapper, IntegerTypeData, find_dpcode
32+
from .models import (
33+
DPCodeBooleanWrapper,
34+
DPCodeEnumWrapper,
35+
IntegerTypeData,
36+
find_dpcode,
37+
)
3338
from .util import get_dpcode, get_dptype, remap_value
3439

3540

@@ -429,7 +434,13 @@ def async_discover_device(device_ids: list[str]):
429434
if descriptions := LIGHTS.get(device.category):
430435
entities.extend(
431436
TuyaLightEntity(
432-
device, manager, description, switch_wrapper=switch_wrapper
437+
device,
438+
manager,
439+
description,
440+
color_mode_wrapper=DPCodeEnumWrapper.find_dpcode(
441+
device, description.color_mode, prefer_function=True
442+
),
443+
switch_wrapper=switch_wrapper,
433444
)
434445
for description in descriptions
435446
if (
@@ -458,7 +469,6 @@ class TuyaLightEntity(TuyaEntity, LightEntity):
458469
_brightness: IntegerTypeData | None = None
459470
_color_data_dpcode: DPCode | None = None
460471
_color_data_type: ColorTypeData | None = None
461-
_color_mode: DPCode | None = None
462472
_color_temp: IntegerTypeData | None = None
463473
_white_color_mode = ColorMode.COLOR_TEMP
464474
_fixed_color_mode: ColorMode | None = None
@@ -471,19 +481,18 @@ def __init__(
471481
device_manager: Manager,
472482
description: TuyaLightEntityDescription,
473483
*,
484+
color_mode_wrapper: DPCodeEnumWrapper | None,
474485
switch_wrapper: DPCodeBooleanWrapper,
475486
) -> None:
476487
"""Init TuyaHaLight."""
477488
super().__init__(device, device_manager)
478489
self.entity_description = description
479490
self._attr_unique_id = f"{super().unique_id}{description.key}"
491+
self._color_mode_wrapper = color_mode_wrapper
480492
self._switch_wrapper = switch_wrapper
481493

482494
color_modes: set[ColorMode] = {ColorMode.ONOFF}
483495

484-
# Determine DPCodes
485-
self._color_mode_dpcode = get_dpcode(self.device, description.color_mode)
486-
487496
if int_type := find_dpcode(
488497
self.device,
489498
description.brightness,
@@ -537,15 +546,8 @@ def __init__(
537546
# work_mode "white"
538547
elif (
539548
color_supported(color_modes)
540-
and (
541-
color_mode_enum := find_dpcode(
542-
self.device,
543-
description.color_mode,
544-
dptype=DPType.ENUM,
545-
prefer_function=True,
546-
)
547-
)
548-
and WorkMode.WHITE.value in color_mode_enum.range
549+
and color_mode_wrapper is not None
550+
and WorkMode.WHITE in color_mode_wrapper.type_information.range
549551
):
550552
color_modes.add(ColorMode.WHITE)
551553
self._white_color_mode = ColorMode.WHITE
@@ -566,14 +568,13 @@ def turn_on(self, **kwargs: Any) -> None:
566568
self._switch_wrapper.get_update_command(self.device, True),
567569
]
568570

569-
if self._color_mode_dpcode and (
571+
if self._color_mode_wrapper and (
570572
ATTR_WHITE in kwargs or ATTR_COLOR_TEMP_KELVIN in kwargs
571573
):
572574
commands += [
573-
{
574-
"code": self._color_mode_dpcode,
575-
"value": WorkMode.WHITE,
576-
},
575+
self._color_mode_wrapper.get_update_command(
576+
self.device, WorkMode.WHITE
577+
),
577578
]
578579

579580
if self._color_temp and ATTR_COLOR_TEMP_KELVIN in kwargs:
@@ -602,12 +603,11 @@ def turn_on(self, **kwargs: Any) -> None:
602603
and ATTR_COLOR_TEMP_KELVIN not in kwargs
603604
)
604605
):
605-
if self._color_mode_dpcode:
606+
if self._color_mode_wrapper:
606607
commands += [
607-
{
608-
"code": self._color_mode_dpcode,
609-
"value": WorkMode.COLOUR,
610-
},
608+
self._color_mode_wrapper.get_update_command(
609+
self.device, WorkMode.COLOUR
610+
),
611611
]
612612

613613
if not (brightness := kwargs.get(ATTR_BRIGHTNESS)):
@@ -765,8 +765,8 @@ def color_mode(self) -> ColorMode:
765765
# and HS, determine which mode the light is in. We consider it to be in HS color
766766
# mode, when work mode is anything else than "white".
767767
if (
768-
self._color_mode_dpcode
769-
and self.device.status.get(self._color_mode_dpcode) != WorkMode.WHITE
768+
self._color_mode_wrapper
769+
and self._read_wrapper(self._color_mode_wrapper) != WorkMode.WHITE
770770
):
771771
return ColorMode.HS
772772
return self._white_color_mode

0 commit comments

Comments
 (0)