Skip to content

Commit 615da87

Browse files
committed
Treat any failure to load network information as an un-formed network
1 parent 49362aa commit 615da87

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

zigpy_znp/api.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,35 +93,28 @@ async def detect_zstack_version(self) -> float:
9393
except CommandNotRecognized:
9494
return 3.0
9595

96-
async def load_network_info(self, *, load_devices=False):
96+
async def _load_network_info(self, *, load_devices=False):
9797
"""
9898
Loads low-level network information from NVRAM.
9999
Loading key data greatly increases the runtime so it not enabled by default.
100100
"""
101101

102102
from zigpy_znp.znp import security
103103

104-
is_on_network = None
105-
nib = None
106-
107-
try:
108-
nib = await self.nvram.osal_read(OsalNvIds.NIB, item_type=t.NIB)
109-
except KeyError:
110-
is_on_network = False
111-
else:
112-
is_on_network = nib.nwkLogicalChannel != 0 and nib.nwkKeyLoaded
104+
nib = await self.nvram.osal_read(OsalNvIds.NIB, item_type=t.NIB)
113105

114-
if is_on_network and self.version >= 3.0:
115-
# This NVRAM item is the very first thing initialized in `zgInit`
116-
is_on_network = (
117-
await self.nvram.osal_read(
118-
OsalNvIds.BDBNODEISONANETWORK, item_type=t.uint8_t
119-
)
120-
== 1
121-
)
106+
if nib.nwkLogicalChannel == 0 or not nib.nwkKeyLoaded:
107+
raise NetworkNotFormed()
122108

123-
if not is_on_network:
124-
raise NetworkNotFormed("Device is not a part of a network")
109+
# This NVRAM item is the very first thing initialized in `zgInit`
110+
if (
111+
self.version >= 3.0
112+
and await self.nvram.osal_read(
113+
OsalNvIds.BDBNODEISONANETWORK, item_type=t.uint8_t
114+
)
115+
!= 1
116+
):
117+
raise NetworkNotFormed()
125118

126119
ieee = await self.nvram.osal_read(OsalNvIds.EXTADDR, item_type=t.EUI64)
127120
logical_type = await self.nvram.osal_read(
@@ -225,6 +218,17 @@ async def load_network_info(self, *, load_devices=False):
225218
self.network_info = network_info
226219
self.node_info = node_info
227220

221+
async def load_network_info(self, *, load_devices=False):
222+
"""
223+
Loads low-level network information from NVRAM.
224+
Loading key data greatly increases the runtime so it not enabled by default.
225+
"""
226+
227+
try:
228+
await self._load_network_info()
229+
except KeyError as e:
230+
raise NetworkNotFormed() from e
231+
228232
async def start_network(self):
229233
# Both startup sequences end with the same callback
230234
started_as_coordinator = self.wait_for_response(

0 commit comments

Comments
 (0)