diff --git a/majordom_zigbee/controller.py b/majordom_zigbee/controller.py index 79fdbae..dca3997 100644 --- a/majordom_zigbee/controller.py +++ b/majordom_zigbee/controller.py @@ -355,6 +355,20 @@ async def stop(self): await asyncio.gather(reconnecting, return_exceptions=True) await self._close() + @override + async def settings_did_change(self, before: Settings) -> bool: + """A new transmit power is told to the running radio; clearing it needs a restart for the radio's default. + + The network's own fields are read-only: the runtime refuses a change to them. + """ + tx_power = self.settings.tx_power + if tx_power == before.tx_power: + return True + if tx_power is None or self._application is None: + return False + await self._application.set_tx_power(tx_power) + return True + async def _open(self): """Starts zigpy on the bound radio and takes over the devices on its network.""" link = self.peripherals.endpoint("zigbee") diff --git a/tests/conftest.py b/tests/conftest.py index 12aa21a..2581c35 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -80,6 +80,14 @@ class _MockZigpyApp(zigpy.application.ControllerApplication): _zb_controller = None # set to the controller after start() given_config: dict[str, Any] | None = None # what the controller started it with + def __init__(self, *args: Any, **kwargs: Any): + super().__init__(*args, **kwargs) + self.tx_powers: list[float] = [] # what the radio was told to transmit with, in order + + async def _set_tx_power(self, tx_power: float): + self.tx_powers.append(tx_power) + return tx_power + async def send_packet(self, *_): pass diff --git a/tests/test_settings.py b/tests/test_settings.py index e39599b..c1e6e55 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -1,5 +1,7 @@ """ZigBeeController.Settings: the user sets only what zigpy accepts; the network's own values are shown, never set.""" +from dataclasses import replace + import pytest from conftest import EXTENDED_PAN_ID, RADIO, wait_for from majordom_integration_sdk import Peripherals @@ -53,7 +55,33 @@ async def test_a_change_to_the_network_is_refused(zigpy_apps, tmp_path): await runtime.update_settings(controller, {"channel": 25}) shown = controller.settings.model_dump(mode="json") await runtime.update_settings(controller, shown | {"tx_power": 4}) # the form sent back with a new power - await wait_for(lambda: len(zigpy_apps) == 2 and runtime.status(controller).state == PeripheralState.RUNNING) assert controller.settings.channel == 15 and controller.settings.tx_power == 4 + assert zigpy_apps[0].tx_powers == [4] # told to the radio that runs, which keeps running + assert len(zigpy_apps) == 1 and runtime.status(controller).state == PeripheralState.RUNNING + finally: + await runtime.stop() + + +async def test_clearing_the_power_restarts_the_radio_for_its_default(zigpy_apps, tmp_path): + controller = ZigBeeController(build_test_platform(), settings=ZigBeeController.Settings(tx_power=8)) + runtime = Runtime(tmp_path, [Pinned(RADIO), controller], platform=controller.platform) + await runtime.start() + try: + await wait_for(lambda: runtime.status(controller).state == PeripheralState.RUNNING) + await runtime.update_settings(controller, {"tx_power": None}) # a radio can't be told "the default" + await wait_for(lambda: len(zigpy_apps) == 2 and runtime.status(controller).state == PeripheralState.RUNNING) + assert zigpy_apps[1].given_config and zigpy_apps[1].given_config[CONF_NWK] == {} + finally: + await runtime.stop() + + +async def test_the_hosts_facts_need_no_restart(zigpy_apps, tmp_path): + controller = ZigBeeController(build_test_platform()) + runtime = Runtime(tmp_path, [Pinned(RADIO), controller], platform=controller.platform) + await runtime.start() + try: + await wait_for(lambda: runtime.status(controller).state == PeripheralState.RUNNING) + await runtime.update_platform(replace(controller.platform, region="Europe", thread_dataset="0e08")) + assert len(zigpy_apps) == 1 and runtime.status(controller).state == PeripheralState.RUNNING finally: await runtime.stop()