1
1
"""Tests for Tuya Sensor quirks."""
2
2
3
+ from unittest import mock
4
+
3
5
import pytest
4
6
from zigpy .zcl import foundation
5
7
from zigpy .zcl .clusters .general import Basic , PowerConfiguration
6
8
from zigpy .zcl .clusters .measurement import RelativeHumidity , TemperatureMeasurement
7
9
10
+ from tests .common import wait_for_zigpy_tasks
8
11
import zhaquirks
9
- from zhaquirks .tuya import TuyaLocalCluster
12
+ from zhaquirks .tuya import TUYA_MCU_VERSION_RSP , TuyaLocalCluster
10
13
from zhaquirks .tuya .mcu import TuyaMCUCluster
14
+ from zhaquirks .tuya .tuya_sensor import RespondingTuyaMCUCluster
15
+
16
+ # Temp DP 1, Humidity DP 2, Battery DP 3
17
+ TUYA_TEMP01_HUM02_BAT03 = b"\x09 \xe0 \x02 \x0b \x33 \x01 \x02 \x00 \x04 \x00 \x00 \x00 \xfd \x02 \x02 \x00 \x04 \x00 \x00 \x00 \x47 \x03 \x02 \x00 \x04 \x00 \x00 \x00 \x01 "
18
+ # Temp DP 1, Humidity DP 2, Battery DP 4
19
+ TUYA_TEMP01_HUM02_BAT04 = b"\x09 \xe0 \x02 \x0b \x33 \x01 \x02 \x00 \x04 \x00 \x00 \x00 \xfd \x02 \x02 \x00 \x04 \x00 \x00 \x00 \x47 \x04 \x02 \x00 \x04 \x00 \x00 \x00 \x01 "
20
+ TUYA_USP = b"\x09 \xe0 \x02 \x0b \x33 \x01 \x02 \x00 \x04 \x00 \x00 \x00 \xfd \x02 \x02 \x00 \x04 \x00 \x00 \x00 \x47 \xff \x02 \x00 \x04 \x00 \x00 \x00 \x64 "
21
+
22
+ ZCL_TUYA_VERSION_RSP = b"\x09 \x06 \x11 \x01 \x6d \x82 "
11
23
12
24
zhaquirks .setup ()
13
25
@@ -73,8 +85,7 @@ async def test_handle_get_data(
73
85
== data .data .datapoints [2 ].data .payload * 2
74
86
)
75
87
76
- message = b"\x09 \xe0 \x02 \x0b \x33 \x01 \x02 \x00 \x04 \x00 \x00 \x00 \xfd \x02 \x02 \x00 \x04 \x00 \x00 \x00 \x47 \xff \x02 \x00 \x04 \x00 \x00 \x00 \x64 "
77
- hdr , data = ep .tuya_manufacturer .deserialize (message )
88
+ hdr , data = ep .tuya_manufacturer .deserialize (TUYA_USP )
78
89
79
90
status = ep .tuya_manufacturer .handle_get_data (data .data )
80
91
assert status == foundation .Status .UNSUPPORTED_ATTRIBUTE
@@ -90,20 +101,20 @@ async def test_handle_get_data(
90
101
91
102
92
103
@pytest .mark .parametrize (
93
- "model,manuf,rh_scale,temp_scale" ,
104
+ "model,manuf,rh_scale,temp_scale,state_rpt " ,
94
105
[
95
- ("_TZE200_yjjdcqsq" , "TS0601" , 100 , 10 ),
96
- ("_TZE200_9yapgbuv" , "TS0601" , 100 , 10 ),
97
- ("_TZE204_yjjdcqsq" , "TS0601" , 100 , 10 ),
98
- ("_TZE200_utkemkbs" , "TS0601" , 100 , 10 ),
99
- ("_TZE204_utkemkbs" , "TS0601" , 100 , 10 ),
100
- ("_TZE204_yjjdcqsq" , "TS0601" , 100 , 10 ),
101
- ("_TZE204_ksz749x8" , "TS0601" , 100 , 10 ),
102
- ("_TZE204_upagmta9" , "TS0601" , 100 , 10 ),
106
+ ("_TZE200_yjjdcqsq" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
107
+ ("_TZE200_9yapgbuv" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
108
+ ("_TZE204_yjjdcqsq" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
109
+ ("_TZE200_utkemkbs" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
110
+ ("_TZE204_utkemkbs" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
111
+ ("_TZE204_yjjdcqsq" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
112
+ ("_TZE204_ksz749x8" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
113
+ ("_TZE204_upagmta9" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT03 ),
103
114
],
104
115
)
105
116
async def test_handle_get_data_enum_batt (
106
- zigpy_device_from_v2_quirk , model , manuf , rh_scale , temp_scale
117
+ zigpy_device_from_v2_quirk , model , manuf , rh_scale , temp_scale , state_rpt
107
118
):
108
119
"""Test handle_get_data for multiple attributes - enum battery."""
109
120
@@ -116,11 +127,7 @@ async def test_handle_get_data_enum_batt(
116
127
assert ep .tuya_manufacturer is not None
117
128
assert isinstance (ep .tuya_manufacturer , TuyaMCUCluster )
118
129
119
- if model == "_TZE204_upagmta9" : # Uses dp 3 for battery
120
- message = b"\x09 \xe0 \x02 \x0b \x33 \x01 \x02 \x00 \x04 \x00 \x00 \x00 \xfd \x02 \x02 \x00 \x04 \x00 \x00 \x00 \x47 \x03 \x02 \x00 \x04 \x00 \x00 \x00 \x01 "
121
- else :
122
- message = b"\x09 \xe0 \x02 \x0b \x33 \x01 \x02 \x00 \x04 \x00 \x00 \x00 \xfd \x02 \x02 \x00 \x04 \x00 \x00 \x00 \x47 \x04 \x02 \x00 \x04 \x00 \x00 \x00 \x01 "
123
- hdr , data = ep .tuya_manufacturer .deserialize (message )
130
+ hdr , data = ep .tuya_manufacturer .deserialize (state_rpt )
124
131
125
132
status = ep .tuya_manufacturer .handle_get_data (data .data )
126
133
@@ -138,8 +145,7 @@ async def test_handle_get_data_enum_batt(
138
145
139
146
assert ep .power .get ("battery_percentage_remaining" ) == 100
140
147
141
- message = b"\x09 \xe0 \x02 \x0b \x33 \x01 \x02 \x00 \x04 \x00 \x00 \x00 \xfd \x02 \x02 \x00 \x04 \x00 \x00 \x00 \x47 \xff \x02 \x00 \x04 \x00 \x00 \x00 \x64 "
142
- hdr , data = ep .tuya_manufacturer .deserialize (message )
148
+ hdr , data = ep .tuya_manufacturer .deserialize (TUYA_USP )
143
149
144
150
status = ep .tuya_manufacturer .handle_get_data (data .data )
145
151
assert status == foundation .Status .UNSUPPORTED_ATTRIBUTE
@@ -168,4 +174,30 @@ def test_valid_attributes(zigpy_device_from_v2_quirk):
168
174
assert {power_attr_id } == power_config_cluster ._VALID_ATTRIBUTES
169
175
170
176
171
- # _TZE204_upagmta9
177
+ async def test_tuya_version (zigpy_device_from_v2_quirk ):
178
+ """Test TUYA_MCU_VERSION_RSP messages, ensure response."""
179
+
180
+ quirked = zigpy_device_from_v2_quirk ("_TZE204_upagmta9" , "TS0601" )
181
+ ep = quirked .endpoints [1 ]
182
+
183
+ tuya_cluster = ep .tuya_manufacturer
184
+
185
+ assert ep .tuya_manufacturer is not None
186
+ assert isinstance (ep .tuya_manufacturer , TuyaMCUCluster )
187
+ assert isinstance (ep .tuya_manufacturer , RespondingTuyaMCUCluster )
188
+
189
+ # simulate a TUYA_MCU_VERSION_RSP message
190
+ hdr , args = tuya_cluster .deserialize (ZCL_TUYA_VERSION_RSP )
191
+ assert hdr .command_id == TUYA_MCU_VERSION_RSP
192
+
193
+ with mock .patch .object (
194
+ ep .tuya_manufacturer ._endpoint ,
195
+ "request" ,
196
+ return_value = foundation .Status .SUCCESS ,
197
+ ) as m1 :
198
+ ep .tuya_manufacturer .handle_message (hdr , args )
199
+ await wait_for_zigpy_tasks ()
200
+
201
+ res_hdr = foundation .ZCLHeader .deserialize (m1 .await_args [1 ]["data" ])
202
+ assert not res_hdr [0 ].manufacturer
203
+ assert not res_hdr [0 ].frame_control .is_manufacturer_specific
0 commit comments