Skip to content

Commit 059bffa

Browse files
committed
Implement provisional zigpy network management API
1 parent 251fc77 commit 059bffa

15 files changed

+245
-488
lines changed

tests/application/test_connect.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ async def test_probe_multiple(device, make_znp_server):
101101

102102
@pytest.mark.parametrize("device", FORMED_DEVICES)
103103
async def test_reconnect(device, event_loop, make_application):
104-
app, znp_server = make_application(
104+
app, znp_server = await make_application(
105105
server_cls=device,
106106
client_config={
107107
# Make auto-reconnection happen really fast
@@ -143,7 +143,7 @@ async def test_reconnect(device, event_loop, make_application):
143143

144144
@pytest.mark.parametrize("device", FORMED_DEVICES)
145145
async def test_shutdown_from_app(device, mocker, make_application):
146-
app, znp_server = make_application(server_cls=device)
146+
app, znp_server = await make_application(server_cls=device)
147147

148148
await app.startup(auto_form=False)
149149

@@ -159,7 +159,7 @@ async def test_shutdown_from_app(device, mocker, make_application):
159159

160160

161161
async def test_clean_shutdown(make_application):
162-
app, znp_server = make_application(server_cls=FormedLaunchpadCC26X2R1)
162+
app, znp_server = await make_application(server_cls=FormedLaunchpadCC26X2R1)
163163
await app.startup(auto_form=False)
164164

165165
# This should not throw
@@ -170,7 +170,7 @@ async def test_clean_shutdown(make_application):
170170

171171

172172
async def test_multiple_shutdown(make_application):
173-
app, znp_server = make_application(server_cls=FormedLaunchpadCC26X2R1)
173+
app, znp_server = await make_application(server_cls=FormedLaunchpadCC26X2R1)
174174
await app.startup(auto_form=False)
175175

176176
await app.shutdown()
@@ -182,7 +182,7 @@ async def test_multiple_shutdown(make_application):
182182
async def test_reconnect_lockup(device, event_loop, make_application, mocker):
183183
mocker.patch("zigpy_znp.zigbee.application.WATCHDOG_PERIOD", 0.1)
184184

185-
app, znp_server = make_application(
185+
app, znp_server = await make_application(
186186
server_cls=device,
187187
client_config={
188188
# Make auto-reconnection happen really fast
@@ -223,7 +223,7 @@ async def test_reconnect_lockup(device, event_loop, make_application, mocker):
223223
async def test_reconnect_lockup_pyserial(device, event_loop, make_application, mocker):
224224
mocker.patch("zigpy_znp.zigbee.application.WATCHDOG_PERIOD", 0.1)
225225

226-
app, znp_server = make_application(
226+
app, znp_server = await make_application(
227227
server_cls=device,
228228
client_config={
229229
conf.CONF_ZNP_CONFIG: {
@@ -244,9 +244,9 @@ async def test_reconnect_lockup_pyserial(device, event_loop, make_application, m
244244

245245
did_load_info = asyncio.get_running_loop().create_future()
246246

247-
async def patched_load_network_info(*, old_load=app.load_network_info):
247+
async def patched_load_network_info(old_load=app.load_network_info, **kwargs):
248248
try:
249-
return await old_load()
249+
return await old_load(**kwargs)
250250
finally:
251251
did_load_info.set_result(True)
252252

tests/application/test_joining.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def test_permit_join(device, fixed_joining_bug, mocker, make_application):
2525
if fixed_joining_bug:
2626
mocker.patch.object(device, "code_revision", 20210708)
2727

28-
app, znp_server = make_application(server_cls=device)
28+
app, znp_server = await make_application(server_cls=device)
2929

3030
# Handle us opening joins on the coordinator
3131
permit_join_coordinator = znp_server.reply_once_to(
@@ -66,7 +66,7 @@ async def test_permit_join(device, fixed_joining_bug, mocker, make_application):
6666

6767
@pytest.mark.parametrize("device", FORMED_DEVICES)
6868
async def test_join_coordinator(device, make_application):
69-
app, znp_server = make_application(server_cls=device)
69+
app, znp_server = await make_application(server_cls=device)
7070

7171
# Handle us opening joins on the coordinator
7272
permit_join_coordinator = znp_server.reply_once_to(
@@ -80,7 +80,7 @@ async def test_join_coordinator(device, make_application):
8080
)
8181

8282
await app.startup(auto_form=False)
83-
await app.permit(node=app.ieee)
83+
await app.permit(node=app.state.node_info.ieee)
8484

8585
await permit_join_coordinator
8686

@@ -90,7 +90,7 @@ async def test_join_coordinator(device, make_application):
9090
@pytest.mark.parametrize("device", FORMED_ZSTACK3_DEVICES)
9191
@pytest.mark.parametrize("permit_result", [None, asyncio.TimeoutError()])
9292
async def test_permit_join_with_key(device, permit_result, make_application, mocker):
93-
app, znp_server = make_application(server_cls=device)
93+
app, znp_server = await make_application(server_cls=device)
9494

9595
# Consciot bulb
9696
ieee = t.EUI64.convert("EC:1B:BD:FF:FE:54:4F:40")
@@ -112,10 +112,6 @@ async def test_permit_join_with_key(device, permit_result, make_application, moc
112112
],
113113
)
114114

115-
mocker.patch.object(
116-
app, "permit", new=CoroutineMock(side_effect=[None, permit_result])
117-
)
118-
119115
join_disable_install_code = znp_server.reply_once_to(
120116
c.AppConfig.BDBSetJoinUsesInstallCodeKey.Req(BdbJoinUsesInstallCodeKey=False),
121117
responses=[
@@ -125,14 +121,16 @@ async def test_permit_join_with_key(device, permit_result, make_application, moc
125121

126122
await app.startup(auto_form=False)
127123

124+
mocker.patch.object(app, "permit", new=CoroutineMock(side_effect=permit_result))
125+
128126
with contextlib.nullcontext() if permit_result is None else pytest.raises(
129127
asyncio.TimeoutError
130128
):
131129
await app.permit_with_key(node=ieee, code=code, time_s=1)
132130

133131
await bdb_add_install_code
134132
await join_enable_install_code
135-
assert app.permit.call_count == 2
133+
assert app.permit.call_count == 1
136134

137135
# The install code policy is reset right after
138136
await join_disable_install_code
@@ -142,7 +140,7 @@ async def test_permit_join_with_key(device, permit_result, make_application, moc
142140

143141
@pytest.mark.parametrize("device", FORMED_ZSTACK3_DEVICES)
144142
async def test_permit_join_with_invalid_key(device, make_application):
145-
app, znp_server = make_application(server_cls=device)
143+
app, znp_server = await make_application(server_cls=device)
146144

147145
# Consciot bulb
148146
ieee = t.EUI64.convert("EC:1B:BD:FF:FE:54:4F:40")
@@ -156,7 +154,7 @@ async def test_permit_join_with_invalid_key(device, make_application):
156154

157155
@pytest.mark.parametrize("device", FORMED_DEVICES)
158156
async def test_on_zdo_device_join(device, make_application, mocker):
159-
app, znp_server = make_application(server_cls=device)
157+
app, znp_server = await make_application(server_cls=device)
160158
await app.startup(auto_form=False)
161159

162160
mocker.patch.object(app, "handle_join")
@@ -176,7 +174,7 @@ async def test_on_zdo_device_join(device, make_application, mocker):
176174

177175
@pytest.mark.parametrize("device", FORMED_DEVICES)
178176
async def test_on_zdo_device_join_and_announce_fast(device, make_application, mocker):
179-
app, znp_server = make_application(server_cls=device)
177+
app, znp_server = await make_application(server_cls=device)
180178
await app.startup(auto_form=False)
181179

182180
mocker.patch.object(app, "handle_join")
@@ -213,7 +211,7 @@ async def test_on_zdo_device_join_and_announce_fast(device, make_application, mo
213211

214212
@pytest.mark.parametrize("device", FORMED_DEVICES)
215213
async def test_on_zdo_device_join_and_announce_slow(device, make_application, mocker):
216-
app, znp_server = make_application(server_cls=device)
214+
app, znp_server = await make_application(server_cls=device)
217215
await app.startup(auto_form=False)
218216

219217
mocker.patch.object(app, "handle_join")
@@ -251,7 +249,7 @@ async def test_on_zdo_device_join_and_announce_slow(device, make_application, mo
251249

252250
@pytest.mark.parametrize("device", FORMED_DEVICES)
253251
async def test_new_device_join_and_bind_complex(device, make_application, mocker):
254-
app, znp_server = make_application(server_cls=device)
252+
app, znp_server = await make_application(server_cls=device)
255253
await app.startup(auto_form=False)
256254

257255
nwk = 0x6A7C
@@ -502,7 +500,7 @@ def bind_req_callback(request):
502500
ep = device.endpoints[request.SrcEndpoint]
503501
assert cluster in ep.in_clusters or cluster in ep.out_clusters
504502

505-
assert request.Address.ieee == app.ieee
503+
assert request.Address.ieee == app.state.node_info.ieee
506504
assert request.Address.addrmode == 0x03
507505

508506
# Make sure the endpoint profiles match up
@@ -528,7 +526,7 @@ def bind_req_callback(request):
528526

529527
@pytest.mark.parametrize("device", FORMED_DEVICES)
530528
async def test_unknown_device_discovery(device, make_application, mocker):
531-
app, znp_server = make_application(server_cls=device)
529+
app, znp_server = await make_application(server_cls=device)
532530
await app.startup(auto_form=False)
533531

534532
mocker.spy(app, "handle_join")
@@ -602,4 +600,4 @@ async def test_unknown_device_discovery(device, make_application, mocker):
602600
assert new_dev.nwk == new_nwk
603601
assert new_dev.ieee == new_ieee
604602

605-
await app.pre_shutdown()
603+
await app.shutdown()

tests/application/test_nvram_migration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async def test_addrmgr_rewrite_fix(device, make_application, mocker):
6060
extAddr=t.EUI64.convert("FF:FF:FF:FF:FF:FF:FF:FF"),
6161
)
6262

63-
app, znp_server = make_application(server_cls=device)
63+
app, znp_server = await make_application(server_cls=device)
6464
znp_server.callback_for_response(
6565
c.SYS.OSALNVReadExt.Req(Id=OsalNvIds.ADDRMGR, Offset=0), addrmgr_reads.append
6666
)
@@ -94,6 +94,7 @@ async def test_addrmgr_rewrite_fix(device, make_application, mocker):
9494

9595
# Will not be read again
9696
assert len(addrmgr_reads) == 2
97+
await app.connect()
9798
await app.startup()
9899
await app.shutdown()
99100
assert len(addrmgr_reads) == 2
@@ -104,6 +105,7 @@ async def test_addrmgr_rewrite_fix(device, make_application, mocker):
104105
old_addrmgr2 = nvram[OsalNvIds.ADDRMGR]
105106

106107
assert len(addrmgr_reads) == 2
108+
await app.connect()
107109
await app.startup()
108110
await app.shutdown()
109111
assert len(addrmgr_reads) == 3

0 commit comments

Comments
 (0)