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
@@ -75,8 +87,7 @@ async def test_handle_get_data(
75
87
== data .data .datapoints [2 ].data .payload * 2
76
88
)
77
89
78
- 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 "
79
- hdr , data = ep .tuya_manufacturer .deserialize (message )
90
+ hdr , data = ep .tuya_manufacturer .deserialize (TUYA_USP )
80
91
81
92
status = ep .tuya_manufacturer .handle_get_data (data .data )
82
93
assert status == foundation .Status .UNSUPPORTED_ATTRIBUTE
@@ -92,20 +103,20 @@ async def test_handle_get_data(
92
103
93
104
94
105
@pytest .mark .parametrize (
95
- "model,manuf,rh_scale,temp_scale" ,
106
+ "model,manuf,rh_scale,temp_scale,state_rpt " ,
96
107
[
97
- ("_TZE200_yjjdcqsq" , "TS0601" , 100 , 10 ),
98
- ("_TZE200_9yapgbuv" , "TS0601" , 100 , 10 ),
99
- ("_TZE204_yjjdcqsq" , "TS0601" , 100 , 10 ),
100
- ("_TZE200_utkemkbs" , "TS0601" , 100 , 10 ),
101
- ("_TZE204_utkemkbs" , "TS0601" , 100 , 10 ),
102
- ("_TZE204_yjjdcqsq" , "TS0601" , 100 , 10 ),
103
- ("_TZE204_ksz749x8" , "TS0601" , 100 , 10 ),
104
- ("_TZE204_upagmta9" , "TS0601" , 100 , 10 ),
108
+ ("_TZE200_yjjdcqsq" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
109
+ ("_TZE200_9yapgbuv" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
110
+ ("_TZE204_yjjdcqsq" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
111
+ ("_TZE200_utkemkbs" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
112
+ ("_TZE204_utkemkbs" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
113
+ ("_TZE204_yjjdcqsq" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
114
+ ("_TZE204_ksz749x8" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT04 ),
115
+ ("_TZE204_upagmta9" , "TS0601" , 100 , 10 , TUYA_TEMP01_HUM02_BAT03 ),
105
116
],
106
117
)
107
118
async def test_handle_get_data_enum_batt (
108
- zigpy_device_from_v2_quirk , model , manuf , rh_scale , temp_scale
119
+ zigpy_device_from_v2_quirk , model , manuf , rh_scale , temp_scale , state_rpt
109
120
):
110
121
"""Test handle_get_data for multiple attributes - enum battery."""
111
122
@@ -118,11 +129,7 @@ async def test_handle_get_data_enum_batt(
118
129
assert ep .tuya_manufacturer is not None
119
130
assert isinstance (ep .tuya_manufacturer , TuyaMCUCluster )
120
131
121
- if model == "_TZE204_upagmta9" : # Uses dp 3 for battery
122
- 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 "
123
- else :
124
- 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 "
125
- hdr , data = ep .tuya_manufacturer .deserialize (message )
132
+ hdr , data = ep .tuya_manufacturer .deserialize (state_rpt )
126
133
127
134
status = ep .tuya_manufacturer .handle_get_data (data .data )
128
135
@@ -140,8 +147,7 @@ async def test_handle_get_data_enum_batt(
140
147
141
148
assert ep .power .get ("battery_percentage_remaining" ) == 100
142
149
143
- 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 "
144
- hdr , data = ep .tuya_manufacturer .deserialize (message )
150
+ hdr , data = ep .tuya_manufacturer .deserialize (TUYA_USP )
145
151
146
152
status = ep .tuya_manufacturer .handle_get_data (data .data )
147
153
assert status == foundation .Status .UNSUPPORTED_ATTRIBUTE
@@ -168,3 +174,32 @@ def test_valid_attributes(zigpy_device_from_v2_quirk):
168
174
assert {temperature_attr_id } == temperature_cluster ._VALID_ATTRIBUTES
169
175
assert {humidity_attr_id } == humidity_cluster ._VALID_ATTRIBUTES
170
176
assert {power_attr_id } == power_config_cluster ._VALID_ATTRIBUTES
177
+
178
+
179
+ async def test_tuya_version (zigpy_device_from_v2_quirk ):
180
+ """Test TUYA_MCU_VERSION_RSP messages, ensure response."""
181
+
182
+ quirked = zigpy_device_from_v2_quirk ("_TZE204_upagmta9" , "TS0601" )
183
+ ep = quirked .endpoints [1 ]
184
+
185
+ tuya_cluster = ep .tuya_manufacturer
186
+
187
+ assert ep .tuya_manufacturer is not None
188
+ assert isinstance (ep .tuya_manufacturer , TuyaMCUCluster )
189
+ assert isinstance (ep .tuya_manufacturer , RespondingTuyaMCUCluster )
190
+
191
+ # simulate a TUYA_MCU_VERSION_RSP message
192
+ hdr , args = tuya_cluster .deserialize (ZCL_TUYA_VERSION_RSP )
193
+ assert hdr .command_id == TUYA_MCU_VERSION_RSP
194
+
195
+ with mock .patch .object (
196
+ ep .tuya_manufacturer ._endpoint ,
197
+ "request" ,
198
+ return_value = foundation .Status .SUCCESS ,
199
+ ) as m1 :
200
+ ep .tuya_manufacturer .handle_message (hdr , args )
201
+ await wait_for_zigpy_tasks ()
202
+
203
+ res_hdr = foundation .ZCLHeader .deserialize (m1 .await_args [1 ]["data" ])
204
+ assert not res_hdr [0 ].manufacturer
205
+ assert not res_hdr [0 ].frame_control .is_manufacturer_specific
0 commit comments