Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bellows/ezsp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
42 changes: 28 additions & 14 deletions bellows/zigbee/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -1158,23 +1173,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

Expand Down
49 changes: 49 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -2302,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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ezsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down
Loading