Skip to content

Commit 72d76c3

Browse files
committed
Incorporate latest zigpy.state changes
1 parent 094c0ee commit 72d76c3

File tree

6 files changed

+47
-41
lines changed

6 files changed

+47
-41
lines changed

zigpy_znp/api.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async def detect_zstack_version(self) -> float:
8282
except CommandNotRecognized:
8383
return 3.0
8484

85-
async def load_network_info(self, *, load_keys=False):
85+
async def load_network_info(self, *, load_devices=False):
8686
"""
8787
Loads low-level network information from NVRAM.
8888
Loading key data greatly increases the runtime so it not enabled by default.
@@ -150,6 +150,8 @@ async def load_network_info(self, *, load_keys=False):
150150
partner_ieee=None,
151151
),
152152
tc_link_key=None,
153+
children=[],
154+
nwk_addresses={},
153155
key_table=[],
154156
stack_specific=None,
155157
)
@@ -162,17 +164,29 @@ async def load_network_info(self, *, load_keys=False):
162164
}
163165

164166
# This takes a few seconds
165-
if load_keys:
167+
if load_devices:
166168
for device in await security.read_devices(self):
167-
network_info.key_table.append(
168-
zigpy.state.Key(
169-
key=device.aps_link_key,
170-
seq=0,
171-
tx_counter=device.tx_counter,
172-
rx_counter=device.rx_counter,
173-
partner_ieee=device.ieee,
169+
if device.is_child:
170+
network_info.children.append(
171+
zigpy.state.NodeInfo(
172+
ieee=device.ieee,
173+
nwk=device.nwk,
174+
logical_type=zdo_t.LogicalType.EndDevice,
175+
)
176+
)
177+
else:
178+
network_info.nwk_addresses[device.ieee] = device.nwk
179+
180+
if device.aps_link_key is not None:
181+
network_info.key_table.append(
182+
zigpy.state.Key(
183+
key=device.aps_link_key,
184+
seq=0,
185+
tx_counter=device.tx_counter,
186+
rx_counter=device.rx_counter,
187+
partner_ieee=device.ieee,
188+
)
174189
)
175-
)
176190

177191
ieee = await self.nvram.osal_read(OsalNvIds.EXTADDR, item_type=t.EUI64)
178192
logical_type = await self.nvram.osal_read(
@@ -379,30 +393,21 @@ async def write_network_info(
379393

380394
devices = {}
381395

382-
for neighbor in network_info.neighbor_table or []:
383-
devices[neighbor.ieee] = security.StoredDevice(
384-
nwk=neighbor.nwk,
385-
ieee=neighbor.ieee,
396+
for child in network_info.children or []:
397+
devices[child.ieee] = security.StoredDevice(
398+
nwk=child.nwk if child.nwk is not None else 0xFFFE,
399+
ieee=child.ieee,
386400
is_child=True,
387401
)
388402

389403
for key in network_info.key_table or []:
390404
device = devices.get(key.partner_ieee)
391405

392406
if device is None:
393-
nwk = None
394-
395-
# The key table doesn't save a device's NWK address so we pick one
396-
for test_nwk in range(0x0001, 0xFFFE + 1):
397-
if all(n.nwk != test_nwk for n in network_info.neighbor_table):
398-
nwk = test_nwk
399-
break
400-
401-
assert nwk is not None
402-
403407
device = security.StoredDevice(
404-
nwk=nwk,
408+
nwk=network_info.nwk_addresses.get(key.partner_ieee, 0xFFFE),
405409
ieee=key.partner_ieee,
410+
is_child=False,
406411
)
407412

408413
devices[key.partner_ieee] = device.replace(
@@ -411,7 +416,7 @@ async def write_network_info(
411416
rx_counter=key.rx_counter,
412417
)
413418

414-
LOGGER.debug("Writing neighbors and keys")
419+
LOGGER.debug("Writing children and keys")
415420

416421
await security.write_devices(
417422
znp=self,

zigpy_znp/tools/common.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@
6868
"items": {
6969
"type": "object",
7070
"properties": {
71-
"nwk_address": {"type": "string", "pattern": "[a-fA-F0-9]{4}"},
71+
"nwk_address": {
72+
"type": ["string", "null"],
73+
"pattern": "[a-fA-F0-9]{4}",
74+
},
7275
"ieee_address": {"type": "string", "pattern": "[a-fA-F0-9]{16}"},
7376
"is_child": {"type": "boolean"},
7477
"link_key": {

zigpy_znp/tools/network_backup.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ async def backup_network(znp: ZNP) -> t.JSONType:
2525
devices = []
2626

2727
for device in await read_devices(znp):
28+
nwk = device.nwk if device.nwk != 0xFFFE else None
2829
obj = {
29-
"nwk_address": device.nwk.serialize()[::-1].hex(),
30+
"nwk_address": nwk.serialize()[::-1].hex(),
3031
"ieee_address": device.ieee.serialize()[::-1].hex(),
31-
"is_child": any(
32-
device.ieee == nbr.ieee for nbr in znp.network_info.neighbor_table
33-
),
32+
"is_child": device.is_child,
3433
}
3534

3635
if device.aps_link_key:

zigpy_znp/tools/network_restore.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,20 @@ def json_backup_to_zigpy_state(
4848
network_info.network_key.partner_ieee = None
4949
network_info.network_key.seq = backup["network_key"]["sequence_number"]
5050

51-
network_info.key_table = []
52-
network_info.neighbor_table = []
51+
network_info.children = []
52+
network_info.nwk_addresses = {}
5353

5454
for obj in backup["devices"]:
5555
node = zigpy.state.NodeInfo()
5656
node.nwk, _ = t.NWK.deserialize(bytes.fromhex(obj["nwk_address"])[::-1])
5757
node.ieee, _ = t.EUI64.deserialize(bytes.fromhex(obj["ieee_address"])[::-1])
5858
node.logical_type = None
5959

60-
# The `is_child` key is optional. If it is missing, preserve the old behavior
61-
if obj["is_child"] if "is_child" in obj else ("link_key" not in obj):
62-
network_info.neighbor_table.append(node)
60+
# The `is_child` key is currently optional
61+
if obj.get("is_child", True):
62+
network_info.children.append(node)
63+
else:
64+
network_info.nwk_addresses[node.ieee] = node.nwk
6365

6466
if "link_key" in obj:
6567
key = zigpy.state.Key()

zigpy_znp/zigbee/application.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,9 @@ async def form_network(self):
386386
partner_ieee=None,
387387
),
388388
tc_link_key=None,
389+
children=[],
389390
key_table=[],
390-
neighbor_table=[],
391+
nwk_addresses={},
391392
stack_specific={"zstack": {"tclk_seed": os.urandom(16).hex()}},
392393
)
393394

zigpy_znp/znp/security.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,6 @@ async def read_devices(znp: ZNP) -> typing.Sequence[StoredDevice]:
275275
t.AddrMgrUserType.Assoc | t.AddrMgrUserType.Security,
276276
t.AddrMgrUserType.Security,
277277
):
278-
if not 0x0000 <= entry.nwkAddr <= 0xFFF7:
279-
LOGGER.warning("Ignoring invalid address manager entry: %s", entry)
280-
continue
281-
282278
devices[entry.extAddr] = StoredDevice(
283279
ieee=entry.extAddr,
284280
nwk=entry.nwkAddr,

0 commit comments

Comments
 (0)