Skip to content

Commit d4bb473

Browse files
committed
Actually implement auto forming (untested, for now)
1 parent b609de9 commit d4bb473

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

tests/test_api.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,28 @@ async def test_znp_nvram_writes(znp, event_loop):
618618
await znp.nvram_write(nvids.NwkNvIds.STARTUP_OPTION, t.uint8_t(0xAB))
619619

620620

621+
@pytest_mark_asyncio_timeout()
622+
async def test_znp_nvram_read_success(znp, event_loop):
623+
event_loop.call_soon(
624+
znp.frame_received,
625+
c.Sys.OSALNVRead.Rsp(Status=t.Status.Success, Value=b"test",).to_frame(),
626+
)
627+
result = await znp.nvram_read(nvids.NwkNvIds.STARTUP_OPTION)
628+
629+
assert result == b"test"
630+
631+
632+
@pytest_mark_asyncio_timeout()
633+
async def test_znp_nvram_read_failure(znp, event_loop):
634+
event_loop.call_soon(
635+
znp.frame_received,
636+
c.Sys.OSALNVRead.Rsp(Status=t.Status.Failure, Value=b"test",).to_frame(),
637+
)
638+
639+
with pytest.raises(InvalidCommandResponse):
640+
await znp.nvram_read(nvids.NwkNvIds.STARTUP_OPTION)
641+
642+
621643
@pytest_mark_asyncio_timeout()
622644
async def test_listeners_resolve(event_loop):
623645
callback = Mock()

zigpy_znp/api.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,12 @@ async def nvram_write(
350350
c.Sys.OSALNVWrite.Req(Id=nv_id, Offset=offset, Value=t.ShortBytes(value)),
351351
RspStatus=t.Status.Success,
352352
)
353+
354+
async def nvram_read(
355+
self, nv_id: nvids.BaseNvIds, *, offset: t.uint8_t = 0
356+
) -> bytes:
357+
response = await self.request(
358+
c.Sys.OSALNVRead.Req(Id=nv_id, Offset=offset), RspStatus=t.Status.Success,
359+
)
360+
361+
return response.Value

zigpy_znp/zigbee/application.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,15 @@ async def startup(self, auto_form=False):
255255

256256
await self._reset()
257257

258-
if auto_form and False:
259-
# XXX: actually form a network
260-
await self.form_network()
258+
if auto_form:
259+
is_configured = await self._znp.nvram_read(NwkNvIds.HAS_CONFIGURED_ZSTACK3)
260+
261+
# This is some NVID that stores 0x55 (0b01010101) if the stack is configured
262+
# It is not documented or present in the TI codebase, as far as I can tell.
263+
if is_configured != b"\x55":
264+
await self.form_network()
265+
else:
266+
LOGGER.info("ZNP is already configured, no need to form network.")
261267

262268
if self.config[conf.CONF_ZNP_CONFIG][conf.CONF_TX_POWER] is not None:
263269
dbm = self.config[conf.CONF_ZNP_CONFIG][conf.CONF_TX_POWER]

0 commit comments

Comments
 (0)