Skip to content

Commit df3a4c5

Browse files
authored
Migrate tuya event platform to use DPCodeWrapper (home-assistant#156127)
1 parent 2e21ae0 commit df3a4c5

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

homeassistant/components/tuya/event.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
1515

1616
from . import TuyaConfigEntry
17-
from .const import TUYA_DISCOVERY_NEW, DeviceCategory, DPCode, DPType
17+
from .const import TUYA_DISCOVERY_NEW, DeviceCategory, DPCode
1818
from .entity import TuyaEntity
19-
from .models import find_dpcode
19+
from .models import DPCodeEnumWrapper
2020

2121
# All descriptions can be found here. Mostly the Enum data types in the
2222
# default status set of each category (that don't have a set instruction)
@@ -96,10 +96,17 @@ def async_discover_device(device_ids: list[str]) -> None:
9696
for device_id in device_ids:
9797
device = manager.device_map[device_id]
9898
if descriptions := EVENTS.get(device.category):
99-
for description in descriptions:
100-
dpcode = description.key
101-
if dpcode in device.status:
102-
entities.append(TuyaEventEntity(device, manager, description))
99+
entities.extend(
100+
TuyaEventEntity(
101+
device, manager, description, dpcode_wrapper=dpcode_wrapper
102+
)
103+
for description in descriptions
104+
if (
105+
dpcode_wrapper := DPCodeEnumWrapper.find_dpcode(
106+
device, description.key, prefer_function=True
107+
)
108+
)
109+
)
103110

104111
async_add_entities(entities)
105112

@@ -120,14 +127,14 @@ def __init__(
120127
device: CustomerDevice,
121128
device_manager: Manager,
122129
description: EventEntityDescription,
130+
dpcode_wrapper: DPCodeEnumWrapper,
123131
) -> None:
124132
"""Init Tuya event entity."""
125133
super().__init__(device, device_manager)
126134
self.entity_description = description
127135
self._attr_unique_id = f"{super().unique_id}{description.key}"
128-
129-
if dpcode := find_dpcode(self.device, description.key, dptype=DPType.ENUM):
130-
self._attr_event_types: list[str] = dpcode.range
136+
self._dpcode_wrapper = dpcode_wrapper
137+
self._attr_event_types = dpcode_wrapper.enum_type_information.range
131138

132139
async def _handle_state_update(
133140
self,
@@ -136,10 +143,10 @@ async def _handle_state_update(
136143
) -> None:
137144
if (
138145
updated_status_properties is None
139-
or self.entity_description.key not in updated_status_properties
146+
or self._dpcode_wrapper.dpcode not in updated_status_properties
147+
or (value := self._dpcode_wrapper.read_device_status(self.device)) is None
140148
):
141149
return
142150

143-
value = self.device.status.get(self.entity_description.key)
144151
self._trigger_event(value)
145152
self.async_write_ha_state()

0 commit comments

Comments
 (0)