From 3f6ba1b1b6aa57ab6b41413de5a9551fefd72e25 Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Wed, 29 Mar 2023 19:36:35 -0400 Subject: [PATCH] Allow forming a network quickly, without writing keys and tables (#208) * Allow forming a network quickly, without writing keys and tables * Add a unit test --- tests/api/test_network_state.py | 22 ++++++++++++++++++++++ zigpy_znp/api.py | 11 ++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/api/test_network_state.py b/tests/api/test_network_state.py index f89c2e01..5242a2cc 100644 --- a/tests/api/test_network_state.py +++ b/tests/api/test_network_state.py @@ -1,4 +1,5 @@ import logging +from unittest import mock import pytest @@ -10,6 +11,7 @@ FORMED_DEVICES, BaseZStack1CC2531, FormedZStack3CC2531, + FormedLaunchpadCC26X2R1, ) @@ -92,3 +94,23 @@ async def test_state_write_tclk_zstack3(device, make_connected_znp, caplog): # TCLK was not changed assert formed_znp.network_info == empty_znp.network_info + + +@pytest.mark.parametrize("device", ALL_DEVICES) +async def test_write_settings_fast(device, make_connected_znp): + formed_znp, _ = await make_connected_znp(server_cls=FormedLaunchpadCC26X2R1) + await formed_znp.load_network_info() + formed_znp.close() + + znp, _ = await make_connected_znp(server_cls=device) + + formed_znp.network_info.stack_specific["form_quickly"] = True + + with mock.patch("zigpy_znp.znp.security.write_devices") as mock_write_devices: + await znp.write_network_info( + network_info=formed_znp.network_info, + node_info=formed_znp.node_info, + ) + + # We don't waste time writing device info + assert len(mock_write_devices.mock_awaits) == 0 diff --git a/zigpy_znp/api.py b/zigpy_znp/api.py index 73f97ed5..f6c2157f 100644 --- a/zigpy_znp/api.py +++ b/zigpy_znp/api.py @@ -344,7 +344,8 @@ async def write_network_info( """ from zigpy_znp.znp import security - await self.reset_network_info() + if not network_info.stack_specific.get("form_quickly", False): + await self.reset_network_info() # Form a network with completely random settings to get NVRAM to a known state for item, value in { @@ -377,6 +378,14 @@ async def write_network_info( await self.start_network() await self.reset() + if network_info.stack_specific.get("form_quickly", False): + await self.nvram.osal_write( + OsalNvIds.ZIGPY_ZNP_MIGRATION_ID, + t.uint8_t(NVRAM_MIGRATION_ID), + create=True, + ) + return + LOGGER.debug("Writing actual network settings") # Now that we have a formed network, update its state