Skip to content

Commit cf3e00e

Browse files
committed
Temporarily delete the NIB during network scanning on Z-Stack 1
1 parent 7f0eb09 commit cf3e00e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

zigpy_znp/tools/network_scan.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,17 @@ async def scan_once(znp: ZNP, channels: t.Channels, duration_exp: int):
4242
async def network_scan(
4343
znp: ZNP, channels: t.Channels, num_scans: int, duration_exp: int, duplicates: bool
4444
) -> None:
45-
previous_channels = await znp.nvram_read(NwkNvIds.CHANLIST)
45+
# Network scanning only works if our device is not joined to a network.
46+
# If we don't start Z-Stack 3 it will always work but Z-Stack 1 keeps the device
47+
# state in the NIB, which we have to temporarily delete in order for the scan to be
48+
# possible.
49+
if znp._version.MinorRel == 6:
50+
previous_nib = await znp.nvram_read(NwkNvIds.NIB)
51+
await znp.nvram_delete(NwkNvIds.NIB)
52+
else:
53+
previous_nib = None
4654

55+
previous_channels = await znp.nvram_read(NwkNvIds.CHANLIST)
4756
await znp.nvram_write(NwkNvIds.CHANLIST, t.Channels.ALL_CHANNELS)
4857

4958
try:
@@ -81,6 +90,9 @@ async def network_scan(
8190
f" LQI={beacon.LQI:>3}"
8291
)
8392
finally:
93+
if previous_nib is not None:
94+
await znp.nvram_write(NwkNvIds.NIB, previous_nib, create=True)
95+
8496
await znp.nvram_write(NwkNvIds.CHANLIST, previous_channels)
8597
znp.close()
8698

0 commit comments

Comments
 (0)