Skip to content

Commit be4e97f

Browse files
committed
Add custom device class for Tuya TY0201 sensor
Introduces TY0201Device with direction fix logic to handle incorrect ZCL direction in Tuya TY0201 temperature and humidity sensors. Updates the quirk to use the new device class and removes the tuya_enchantment call.
1 parent 2e2bbd9 commit be4e97f

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

zhaquirks/tuya/ty0201.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,50 @@
11
"""Tuya TY0201 temperature and humidity sensor."""
22

3-
from zhaquirks.tuya import TuyaPowerConfigurationCluster2AA
3+
import logging
4+
5+
from zigpy.quirks.v2 import CustomDeviceV2
6+
import zigpy.types as t
7+
from zigpy.zcl import Cluster, foundation
8+
9+
from zhaquirks.tuya import BaseEnchantedDevice, TuyaPowerConfigurationCluster2AA
410
from zhaquirks.tuya.builder import TuyaQuirkBuilder
511

12+
_LOGGER = logging.getLogger(__name__)
13+
14+
15+
class TY0201Device(CustomDeviceV2, BaseEnchantedDevice):
16+
"""TY0201 device with direction fix and enchantment."""
17+
18+
def _find_zcl_cluster(
19+
self, hdr: foundation.ZCLHeader, packet: t.ZigbeePacket
20+
) -> Cluster:
21+
"""Find a cluster for the packet."""
22+
23+
# TY0201 devices seem to be very lax with their ZCL header's `direction` field,
24+
# we should try "flipping" it if matching doesn't work normally.
25+
try:
26+
return super()._find_zcl_cluster_strict(hdr, packet)
27+
except KeyError:
28+
_LOGGER.debug(
29+
"Packet is coming in the wrong direction, swapping direction and trying again",
30+
)
31+
32+
return super()._find_zcl_cluster_strict(
33+
hdr.replace(
34+
frame_control=hdr.frame_control.replace(
35+
direction=hdr.frame_control.direction.flip()
36+
)
37+
),
38+
packet,
39+
)
40+
41+
642
(
743
TuyaQuirkBuilder("_TZ3000_bjawzodf", "TY0201")
844
.applies_to("_TZ3000_zl1kmjqx", "TY0201")
945
.applies_to("_TZ3000_zl1kmjqx", "")
1046
.replaces(TuyaPowerConfigurationCluster2AA)
11-
.tuya_enchantment()
47+
.device_class(TY0201Device)
1248
.skip_configuration()
1349
.add_to_registry()
1450
)

0 commit comments

Comments
 (0)