1
1
import asyncio
2
2
import contextlib
3
+ from unittest import mock
3
4
4
5
import pytest
5
6
import zigpy .util
6
7
import zigpy .types
8
+ import zigpy .device
7
9
import zigpy .zdo .types as zdo_t
8
10
9
11
import zigpy_znp .types as t
21
23
22
24
@pytest .mark .parametrize ("device" , FORMED_DEVICES )
23
25
async def test_permit_join (device , mocker , make_application ):
24
- app , znp_server = await make_application (server_cls = device )
26
+ app , znp_server = make_application (server_cls = device )
25
27
26
28
permit_join_coordinator = znp_server .reply_once_to (
27
29
request = c .ZDO .MgmtPermitJoinReq .Req (
@@ -38,7 +40,7 @@ async def test_permit_join(device, mocker, make_application):
38
40
request = zdo_request_matcher (
39
41
dst_addr = t .AddrModeAddress (t .AddrMode .Broadcast , 0xFFFC ),
40
42
command_id = zdo_t .ZDOCmd .Mgmt_Permit_Joining_req ,
41
- TSN = 6 ,
43
+ TSN = 7 ,
42
44
zdo_PermitDuration = 10 ,
43
45
zdo_TC_Significant = 0 ,
44
46
),
@@ -74,7 +76,7 @@ async def test_permit_join(device, mocker, make_application):
74
76
75
77
@pytest .mark .parametrize ("device" , FORMED_DEVICES )
76
78
async def test_join_coordinator (device , make_application ):
77
- app , znp_server = await make_application (server_cls = device )
79
+ app , znp_server = make_application (server_cls = device )
78
80
79
81
# Handle us opening joins on the coordinator
80
82
permit_join_coordinator = znp_server .reply_once_to (
@@ -100,7 +102,7 @@ async def test_join_device(device, make_application):
100
102
ieee = t .EUI64 .convert ("EC:1B:BD:FF:FE:54:4F:40" )
101
103
nwk = 0x1234
102
104
103
- app , znp_server = await make_application (server_cls = device )
105
+ app , znp_server = make_application (server_cls = device )
104
106
device = app .add_initialized_device (ieee = ieee , nwk = nwk )
105
107
106
108
permit_join = znp_server .reply_once_to (
@@ -115,7 +117,7 @@ async def test_join_device(device, make_application):
115
117
IsBroadcast = t .Bool .false ,
116
118
ClusterId = 32822 ,
117
119
SecurityUse = 0 ,
118
- TSN = 6 ,
120
+ TSN = 7 ,
119
121
MacDst = 0x0000 ,
120
122
Data = b"\x00 " ,
121
123
),
@@ -133,7 +135,7 @@ async def test_join_device(device, make_application):
133
135
@pytest .mark .parametrize ("device" , FORMED_ZSTACK3_DEVICES )
134
136
@pytest .mark .parametrize ("permit_result" , [None , asyncio .TimeoutError ()])
135
137
async def test_permit_join_with_key (device , permit_result , make_application , mocker ):
136
- app , znp_server = await make_application (server_cls = device )
138
+ app , znp_server = make_application (server_cls = device )
137
139
138
140
# Consciot bulb
139
141
ieee = t .EUI64 .convert ("EC:1B:BD:FF:FE:54:4F:40" )
@@ -183,7 +185,7 @@ async def test_permit_join_with_key(device, permit_result, make_application, moc
183
185
184
186
@pytest .mark .parametrize ("device" , FORMED_ZSTACK3_DEVICES )
185
187
async def test_permit_join_with_invalid_key (device , make_application ):
186
- app , znp_server = await make_application (server_cls = device )
188
+ app , znp_server = make_application (server_cls = device )
187
189
188
190
# Consciot bulb
189
191
ieee = t .EUI64 .convert ("EC:1B:BD:FF:FE:54:4F:40" )
@@ -197,10 +199,10 @@ async def test_permit_join_with_invalid_key(device, make_application):
197
199
198
200
@pytest .mark .parametrize ("device" , FORMED_DEVICES )
199
201
async def test_on_zdo_device_join (device , make_application , mocker ):
200
- app , znp_server = await make_application (server_cls = device )
202
+ app , znp_server = make_application (server_cls = device )
201
203
await app .startup (auto_form = False )
202
204
203
- mocker .patch .object (app , "handle_join" )
205
+ mocker .patch .object (app , "handle_join" , wraps = app . handle_join )
204
206
mocker .patch ("zigpy_znp.zigbee.application.DEVICE_JOIN_MAX_DELAY" , new = 0 )
205
207
206
208
nwk = 0x1234
@@ -217,10 +219,10 @@ async def test_on_zdo_device_join(device, make_application, mocker):
217
219
218
220
@pytest .mark .parametrize ("device" , FORMED_DEVICES )
219
221
async def test_on_zdo_device_join_and_announce_fast (device , make_application , mocker ):
220
- app , znp_server = await make_application (server_cls = device )
222
+ app , znp_server = make_application (server_cls = device )
221
223
await app .startup (auto_form = False )
222
224
223
- mocker .patch .object (app , "handle_join" )
225
+ mocker .patch .object (app , "handle_join" , wraps = app . handle_join )
224
226
mocker .patch ("zigpy_znp.zigbee.application.DEVICE_JOIN_MAX_DELAY" , new = 0.5 )
225
227
226
228
nwk = 0x1234
@@ -272,18 +274,22 @@ async def test_on_zdo_device_join_and_announce_fast(device, make_application, mo
272
274
await app .shutdown ()
273
275
274
276
277
+ @mock .patch ("zigpy_znp.zigbee.application.DEVICE_JOIN_MAX_DELAY" , new = 0.1 )
278
+ @mock .patch (
279
+ "zigpy.device.Device._initialize" ,
280
+ new = zigpy .device .Device ._initialize .__wrapped__ , # to disable retries
281
+ )
275
282
@pytest .mark .parametrize ("device" , FORMED_DEVICES )
276
283
async def test_on_zdo_device_join_and_announce_slow (device , make_application , mocker ):
277
- app , znp_server = await make_application (server_cls = device )
284
+ app , znp_server = make_application (server_cls = device )
278
285
await app .startup (auto_form = False )
279
286
280
287
znp_server .reply_to (
281
288
c .ZDO .ExtRouteDisc .Req (partial = True ),
282
289
responses = [c .ZDO .ExtRouteDisc .Rsp (Status = t .Status .SUCCESS )],
283
290
)
284
291
285
- mocker .patch .object (app , "handle_join" )
286
- mocker .patch ("zigpy_znp.zigbee.application.DEVICE_JOIN_MAX_DELAY" , new = 0.1 )
292
+ mocker .patch .object (app , "handle_join" , wraps = app .handle_join )
287
293
288
294
nwk = 0x1234
289
295
ieee = t .EUI64 .convert ("11:22:33:44:55:66:77:88" )
@@ -295,11 +301,13 @@ async def test_on_zdo_device_join_and_announce_slow(device, make_application, mo
295
301
# We're waiting for the device to announce itself
296
302
assert app .handle_join .call_count == 0
297
303
298
- await asyncio .sleep (0.3 )
304
+ # Wait for the trust center join timeout to elapse
305
+ while app .handle_join .call_count == 0 :
306
+ await asyncio .sleep (0.1 )
299
307
300
- # Too late, it already happened
301
308
app .handle_join .assert_called_once_with (nwk = nwk , ieee = ieee , parent_nwk = 0x0001 )
302
309
310
+ # Finally, send the device announcement
303
311
znp_server .send (
304
312
c .ZDO .MsgCbIncoming .Callback (
305
313
Src = nwk ,
@@ -327,9 +335,10 @@ async def test_on_zdo_device_join_and_announce_slow(device, make_application, mo
327
335
)
328
336
)
329
337
330
- await asyncio .sleep (0.1 )
338
+ await asyncio .sleep (0.5 )
331
339
332
340
# The announcement will trigger another join indication
333
341
assert app .handle_join .call_count == 2
334
342
343
+ app .get_device (ieee = ieee ).cancel_initialization ()
335
344
await app .shutdown ()
0 commit comments