From 466324cdfb603e306bc445e7d303f2ac290e302b Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:43:50 +0000 Subject: [PATCH 1/3] Keep forced route discovery on retries when source routing is enabled --- bellows/zigbee/application.py | 23 +++++++++++------------ tests/test_application.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/bellows/zigbee/application.py b/bellows/zigbee/application.py index 9fe94ac5..cad18510 100644 --- a/bellows/zigbee/application.py +++ b/bellows/zigbee/application.py @@ -1158,23 +1158,22 @@ async def send_packet(self, packet: zigpy.types.ZigbeePacket) -> None: else: aps_frame.groupId = t.uint16_t(0x0000) - if self.config[zigpy.config.CONF_SOURCE_ROUTING]: - # Concentrator/source-routing uses address discovery; also keep AODV - # route discovery as a fallback for destinations that aren't yet in - # the NCP's source-route table (e.g. immediately after startup, - # before the first MTORR has propagated). Mirrors what - # zigbee-herdsman's ember adapter does for the same reason. - aps_frame.options |= ( - t.EmberApsOption.APS_OPTION_ENABLE_ADDRESS_DISCOVERY - | t.EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY - ) - elif zigpy.types.TransmitOptions.FORCE_ROUTE_DISCOVERY in packet.tx_options: - # Forcing route discovery requires retrying + if zigpy.types.TransmitOptions.FORCE_ROUTE_DISCOVERY in packet.tx_options: + # Forcing route discovery requires retrying. EmberZNet still uses a stored + # source route if it has one, so this only helps destinations without one. aps_frame.options |= t.EmberApsOption.APS_OPTION_FORCE_ROUTE_DISCOVERY aps_frame.options |= t.EmberApsOption.APS_OPTION_RETRY else: + # With source routing, route discovery is the fallback for destinations + # not yet in the NCP's source route table (e.g. right after startup, before + # the first MTORR has propagated). aps_frame.options |= t.EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY + if self.config[zigpy.config.CONF_SOURCE_ROUTING]: + # TODO: investigate whether this does anything. EmberZNet only reads it for + # sends through the address table or a binding, and we always send direct. + aps_frame.options |= t.EmberApsOption.APS_OPTION_ENABLE_ADDRESS_DISCOVERY + if zigpy.types.TransmitOptions.APS_Encryption in packet.tx_options: aps_frame.options |= t.EmberApsOption.APS_OPTION_ENCRYPTION diff --git a/tests/test_application.py b/tests/test_application.py index 326ef973..064c491e 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -1204,6 +1204,22 @@ async def test_send_packet_unicast_force_route_discovery(app, packet): ) +async def test_send_packet_unicast_force_route_discovery_source_routing( + make_app, packet +): + app = make_app({zigpy.config.CONF_SOURCE_ROUTING: True}) + + await _test_send_packet_unicast( + app, + packet.replace(tx_options=zigpy.types.TransmitOptions.FORCE_ROUTE_DISCOVERY), + options=( + t.EmberApsOption.APS_OPTION_RETRY + | t.EmberApsOption.APS_OPTION_FORCE_ROUTE_DISCOVERY + | t.EmberApsOption.APS_OPTION_ENABLE_ADDRESS_DISCOVERY + ), + ) + + async def test_send_packet_unicast_aps_encryption(app, packet): await _test_send_packet_unicast( app, From d587235228121b9794f6f3f26dcbcc81d0f9dee5 Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:43:52 +0000 Subject: [PATCH 2/3] Empty the NCP source route table when source routing is disabled --- bellows/zigbee/application.py | 19 +++++++++++++++++-- tests/test_application.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/bellows/zigbee/application.py b/bellows/zigbee/application.py index cad18510..4ec2fce7 100644 --- a/bellows/zigbee/application.py +++ b/bellows/zigbee/application.py @@ -208,7 +208,7 @@ async def connect(self) -> None: await self._ezsp.connect(use_thread=self.config[CONF_USE_THREAD]) # Writing config is required here because network info can't be loaded - await self._ezsp.write_config(self.config[CONF_EZSP_CONFIG]) + await self._ezsp.write_config(self._get_ezsp_config()) self._created_device_endpoints.clear() await self.register_endpoints() @@ -642,10 +642,25 @@ async def reset_network_info(self): else: await self._ezsp.leaveNetwork() + def _get_ezsp_config(self) -> dict[str, int | None]: + config = self.config[CONF_EZSP_CONFIG] + table_size = t.EzspConfigId.CONFIG_SOURCE_ROUTE_TABLE_SIZE.name + + # The NCP source-routes with every route record it stores, even when it is not + # a concentrator, and never drops a stale route. Without source routing, an + # empty table makes it use ordinary routing only. + if ( + not self.config[zigpy.config.CONF_SOURCE_ROUTING] + and table_size not in config + ): + config = {**config, table_size: 0} + + return config + async def _reset(self): self._ezsp.stop_ezsp() await self._ezsp.startup_reset() - await self._ezsp.write_config(self.config[CONF_EZSP_CONFIG]) + await self._ezsp.write_config(self._get_ezsp_config()) async def disconnect(self): # TODO: how do you shut down the stack? diff --git a/tests/test_application.py b/tests/test_application.py index 064c491e..37c9fa11 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -2318,6 +2318,39 @@ async def test_connect_failure(app: ControllerApplication) -> None: assert len(ezsp.disconnect.mock_calls) == 1 +@pytest.mark.parametrize( + ("source_routing", "ezsp_config", "written_config"), + [ + (False, {}, {"CONFIG_SOURCE_ROUTE_TABLE_SIZE": 0}), + (True, {}, {}), + ( + False, + {"CONFIG_SOURCE_ROUTE_TABLE_SIZE": 16}, + {"CONFIG_SOURCE_ROUTE_TABLE_SIZE": 16}, + ), + ], +) +async def test_connect_source_route_table_size( + make_app, source_routing, ezsp_config, written_config +) -> None: + """Without source routing, the NCP gets an empty source route table.""" + app = make_app( + { + zigpy.config.CONF_SOURCE_ROUTING: source_routing, + config.CONF_EZSP_CONFIG: ezsp_config, + } + ) + ezsp = app._ezsp + ezsp.write_config = AsyncMock() + ezsp.connect = AsyncMock() + app.register_endpoints = AsyncMock() + + with patch("bellows.ezsp.EZSP", return_value=ezsp): + await app.connect() + + assert ezsp.write_config.mock_calls == [call(written_config)] + + async def test_repair_tclk_partner_ieee( app: ControllerApplication, ieee: zigpy_t.EUI64 ) -> None: From a74409fbe13bceab2c718110f02f2cb4ad48f036 Mon Sep 17 00:00:00 2001 From: TheJulianJES Date: Mon, 4 May 2026 07:29:26 +0200 Subject: [PATCH 3/3] Use `RESCHEDULE` source route discovery mode when enabling source routing --- bellows/ezsp/__init__.py | 2 +- tests/test_ezsp.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bellows/ezsp/__init__.py b/bellows/ezsp/__init__.py index 1aa308bd..dee8a509 100644 --- a/bellows/ezsp/__init__.py +++ b/bellows/ezsp/__init__.py @@ -625,7 +625,7 @@ async def set_source_routing(self, enabled: bool) -> None: if self._ezsp_version >= 8: await self.setSourceRouteDiscoveryMode( mode=( - t.SourceRouteDiscoveryMode.ON + t.SourceRouteDiscoveryMode.RESCHEDULE if enabled else t.SourceRouteDiscoveryMode.OFF ) diff --git a/tests/test_ezsp.py b/tests/test_ezsp.py index 8a8ba278..fe402959 100644 --- a/tests/test_ezsp.py +++ b/tests/test_ezsp.py @@ -473,7 +473,7 @@ async def test_set_enable_source_routing(ezsp_f): assert len(ezsp_f.setSourceRouteDiscoveryMode.mock_calls) == 1 assert ( ezsp_f.setSourceRouteDiscoveryMode.mock_calls[0].kwargs["mode"] - == t.SourceRouteDiscoveryMode.ON + == t.SourceRouteDiscoveryMode.RESCHEDULE )