Skip to content

Commit 09a105d

Browse files
authored
Migrate Tuya light (switch) to use wrapper class (home-assistant#156580)
1 parent 6bd1787 commit 09a105d

File tree

3 files changed

+92
-9
lines changed

3 files changed

+92
-9
lines changed

homeassistant/components/tuya/light.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from . import TuyaConfigEntry
3030
from .const import TUYA_DISCOVERY_NEW, DeviceCategory, DPCode, DPType, WorkMode
3131
from .entity import TuyaEntity
32-
from .models import IntegerTypeData, find_dpcode
32+
from .models import DPCodeBooleanWrapper, IntegerTypeData, find_dpcode
3333
from .util import get_dpcode, get_dptype, remap_value
3434

3535

@@ -428,9 +428,15 @@ def async_discover_device(device_ids: list[str]):
428428
device = manager.device_map[device_id]
429429
if descriptions := LIGHTS.get(device.category):
430430
entities.extend(
431-
TuyaLightEntity(device, manager, description)
431+
TuyaLightEntity(
432+
device, manager, description, switch_wrapper=switch_wrapper
433+
)
432434
for description in descriptions
433-
if description.key in device.status
435+
if (
436+
switch_wrapper := DPCodeBooleanWrapper.find_dpcode(
437+
device, description.key, prefer_function=True
438+
)
439+
)
434440
)
435441

436442
async_add_entities(entities)
@@ -464,11 +470,15 @@ def __init__(
464470
device: CustomerDevice,
465471
device_manager: Manager,
466472
description: TuyaLightEntityDescription,
473+
*,
474+
switch_wrapper: DPCodeBooleanWrapper,
467475
) -> None:
468476
"""Init TuyaHaLight."""
469477
super().__init__(device, device_manager)
470478
self.entity_description = description
471479
self._attr_unique_id = f"{super().unique_id}{description.key}"
480+
self._switch_wrapper = switch_wrapper
481+
472482
color_modes: set[ColorMode] = {ColorMode.ONOFF}
473483

474484
# Determine DPCodes
@@ -546,13 +556,15 @@ def __init__(
546556
self._fixed_color_mode = next(iter(self._attr_supported_color_modes))
547557

548558
@property
549-
def is_on(self) -> bool:
559+
def is_on(self) -> bool | None:
550560
"""Return true if light is on."""
551-
return self.device.status.get(self.entity_description.key, False)
561+
return self._read_wrapper(self._switch_wrapper)
552562

553563
def turn_on(self, **kwargs: Any) -> None:
554564
"""Turn on or control the light."""
555-
commands = [{"code": self.entity_description.key, "value": True}]
565+
commands = [
566+
self._switch_wrapper.get_update_command(self.device, True),
567+
]
556568

557569
if self._color_mode_dpcode and (
558570
ATTR_WHITE in kwargs or ATTR_COLOR_TEMP_KELVIN in kwargs
@@ -673,9 +685,9 @@ def turn_on(self, **kwargs: Any) -> None:
673685

674686
self._send_command(commands)
675687

676-
def turn_off(self, **kwargs: Any) -> None:
688+
async def async_turn_off(self, **kwargs: Any) -> None:
677689
"""Instruct the light to turn off."""
678-
self._send_command([{"code": self.entity_description.key, "value": False}])
690+
await self._async_send_dpcode_update(self._switch_wrapper, False)
679691

680692
@property
681693
def brightness(self) -> int | None:

tests/components/tuya/snapshots/test_init.ambr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6468,7 +6468,7 @@
64686468
'labels': set({
64696469
}),
64706470
'manufacturer': 'Tuya',
6471-
'model': 'Remote controller (unsupported)',
6471+
'model': 'Remote controller',
64726472
'model_id': 'bngwdjsr',
64736473
'name': 'Télécommande lumières ZigBee',
64746474
'name_by_user': None,

tests/components/tuya/snapshots/test_light.ambr

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3582,6 +3582,77 @@
35823582
'state': 'on',
35833583
})
35843584
# ---
3585+
# name: test_platform_setup_and_discovery[light.telecommande_lumieres_zigbee-entry]
3586+
EntityRegistryEntrySnapshot({
3587+
'aliases': set({
3588+
}),
3589+
'area_id': None,
3590+
'capabilities': dict({
3591+
'max_color_temp_kelvin': 6500,
3592+
'max_mireds': 500,
3593+
'min_color_temp_kelvin': 2000,
3594+
'min_mireds': 153,
3595+
'supported_color_modes': list([
3596+
<ColorMode.COLOR_TEMP: 'color_temp'>,
3597+
]),
3598+
}),
3599+
'config_entry_id': <ANY>,
3600+
'config_subentry_id': <ANY>,
3601+
'device_class': None,
3602+
'device_id': <ANY>,
3603+
'disabled_by': None,
3604+
'domain': 'light',
3605+
'entity_category': None,
3606+
'entity_id': 'light.telecommande_lumieres_zigbee',
3607+
'has_entity_name': True,
3608+
'hidden_by': None,
3609+
'icon': None,
3610+
'id': <ANY>,
3611+
'labels': set({
3612+
}),
3613+
'name': None,
3614+
'options': dict({
3615+
}),
3616+
'original_device_class': None,
3617+
'original_icon': None,
3618+
'original_name': None,
3619+
'platform': 'tuya',
3620+
'previous_unique_id': None,
3621+
'suggested_object_id': None,
3622+
'supported_features': 0,
3623+
'translation_key': None,
3624+
'unique_id': 'tuya.rsjdwgnbqkyswitch_controller',
3625+
'unit_of_measurement': None,
3626+
})
3627+
# ---
3628+
# name: test_platform_setup_and_discovery[light.telecommande_lumieres_zigbee-state]
3629+
StateSnapshot({
3630+
'attributes': ReadOnlyDict({
3631+
'brightness': None,
3632+
'color_mode': None,
3633+
'color_temp': None,
3634+
'color_temp_kelvin': None,
3635+
'friendly_name': 'Télécommande lumières ZigBee',
3636+
'hs_color': None,
3637+
'max_color_temp_kelvin': 6500,
3638+
'max_mireds': 500,
3639+
'min_color_temp_kelvin': 2000,
3640+
'min_mireds': 153,
3641+
'rgb_color': None,
3642+
'supported_color_modes': list([
3643+
<ColorMode.COLOR_TEMP: 'color_temp'>,
3644+
]),
3645+
'supported_features': <LightEntityFeature: 0>,
3646+
'xy_color': None,
3647+
}),
3648+
'context': <ANY>,
3649+
'entity_id': 'light.telecommande_lumieres_zigbee',
3650+
'last_changed': <ANY>,
3651+
'last_reported': <ANY>,
3652+
'last_updated': <ANY>,
3653+
'state': 'unknown',
3654+
})
3655+
# ---
35853656
# name: test_platform_setup_and_discovery[light.tower_fan_ca_407g_smart_backlight-entry]
35863657
EntityRegistryEntrySnapshot({
35873658
'aliases': set({

0 commit comments

Comments
 (0)