Skip to content

Commit 094c0ee

Browse files
committed
Use an explicit is_child key instead of an internal subkey
1 parent e1e2b1e commit 094c0ee

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ target/
6363

6464
# virtualenv
6565
.venv/
66+
.venv*/
6667
venv/
6768
ENV/
6869

tests/tools/test_network_backup_restore.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def backup_json():
5454
"internal": {
5555
"creation_time": "2021-02-16T22:29:28+00:00",
5656
"zstack": {"version": 3.3},
57-
"children": ["000b57fffe38b212"],
5857
},
5958
"source": "[email protected]",
6059
"version": 1,
@@ -73,7 +72,12 @@ def backup_json():
7372
"sequence_number": 1,
7473
},
7574
"devices": [
76-
{"ieee_address": "000b57fffe36b9a0", "nwk_address": "f319"}, # No key
75+
{
76+
# No key
77+
"ieee_address": "000b57fffe36b9a0",
78+
"nwk_address": "f319",
79+
"is_child": True,
80+
},
7781
{
7882
"ieee_address": "000b57fffe38b212",
7983
"link_key": {
@@ -82,6 +86,7 @@ def backup_json():
8286
"tx_counter": 456,
8387
},
8488
"nwk_address": "9672",
89+
"is_child": True,
8590
},
8691
{
8792
"ieee_address": "aabbccddeeff0011",
@@ -91,6 +96,7 @@ def backup_json():
9196
"tx_counter": 445566,
9297
},
9398
"nwk_address": "abcd",
99+
"is_child": False,
94100
},
95101
],
96102
}

zigpy_znp/api.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,25 @@ async def write_network_info(
387387
)
388388

389389
for key in network_info.key_table or []:
390-
devices[key.partner_ieee] = devices[key.partner_ieee].replace(
390+
device = devices.get(key.partner_ieee)
391+
392+
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+
403+
device = security.StoredDevice(
404+
nwk=nwk,
405+
ieee=key.partner_ieee,
406+
)
407+
408+
devices[key.partner_ieee] = device.replace(
391409
aps_link_key=key.key,
392410
tx_counter=key.tx_counter,
393411
rx_counter=key.rx_counter,

zigpy_znp/tools/common.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@
3636
"type": "object",
3737
"properties": {
3838
"tclk_seed": {"type": "string", "pattern": "[a-fA-F0-9]{32}"},
39-
"children": {
40-
"type": "array",
41-
"items": {"type": "string", "pattern": "[a-fA-F0-9]{16}"},
42-
},
4339
},
4440
}
4541
},
@@ -74,6 +70,7 @@
7470
"properties": {
7571
"nwk_address": {"type": "string", "pattern": "[a-fA-F0-9]{4}"},
7672
"ieee_address": {"type": "string", "pattern": "[a-fA-F0-9]{16}"},
73+
"is_child": {"type": "boolean"},
7774
"link_key": {
7875
"type": "object",
7976
"properties": {

zigpy_znp/tools/network_backup.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ async def backup_network(znp: ZNP) -> t.JSONType:
2828
obj = {
2929
"nwk_address": device.nwk.serialize()[::-1].hex(),
3030
"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+
),
3134
}
3235

3336
if device.aps_link_key:
@@ -53,10 +56,6 @@ async def backup_network(znp: ZNP) -> t.JSONType:
5356
"zstack": {
5457
"version": znp.version,
5558
},
56-
"children": [
57-
neighbor.ieee.serialize()[::-1].hex()
58-
for neighbor in znp.network_info.neighbor_table
59-
],
6059
},
6160
},
6261
"coordinator_ieee": znp.node_info.ieee.serialize()[::-1].hex(),

zigpy_znp/tools/network_restore.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,14 @@ def json_backup_to_zigpy_state(
5151
network_info.key_table = []
5252
network_info.neighbor_table = []
5353

54-
children = backup["stack_specific"].get("zstack", {}).get("children")
55-
5654
for obj in backup["devices"]:
5755
node = zigpy.state.NodeInfo()
5856
node.nwk, _ = t.NWK.deserialize(bytes.fromhex(obj["nwk_address"])[::-1])
5957
node.ieee, _ = t.EUI64.deserialize(bytes.fromhex(obj["ieee_address"])[::-1])
6058
node.logical_type = None
6159

62-
if children is None or obj["ieee_address"] in children:
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):
6362
network_info.neighbor_table.append(node)
6463

6564
if "link_key" in obj:

0 commit comments

Comments
 (0)