|
1 | 1 | """Tuya TY0201 temperature and humidity sensor."""
|
2 | 2 |
|
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 |
4 | 10 | from zhaquirks.tuya.builder import TuyaQuirkBuilder
|
5 | 11 |
|
| 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 | + |
6 | 42 | (
|
7 | 43 | TuyaQuirkBuilder("_TZ3000_bjawzodf", "TY0201")
|
8 | 44 | .applies_to("_TZ3000_zl1kmjqx", "TY0201")
|
9 | 45 | .applies_to("_TZ3000_zl1kmjqx", "")
|
10 | 46 | .replaces(TuyaPowerConfigurationCluster2AA)
|
11 |
| - .tuya_enchantment() |
| 47 | + .device_class(TY0201Device) |
12 | 48 | .skip_configuration()
|
13 | 49 | .add_to_registry()
|
14 | 50 | )
|
0 commit comments