Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid relay lists #1339

Merged
merged 3 commits into from Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions tests/test_zigbee_util.py
@@ -1,3 +1,5 @@
from __future__ import annotations

import asyncio
import logging
import sys
Expand Down Expand Up @@ -467,3 +469,16 @@ def test_singleton():
obj = {}
obj[singleton] = 5
assert obj[singleton] == 5


@pytest.mark.parametrize(
"input_relays, expected_relays",
[
([0x0000, 0x0000, 0x0001, 0x0001, 0x0002], [0x0001, 0x0002]),
([0x0001, 0x0002], [0x0001, 0x0002]),
([], []),
([0x0000], []),
],
)
def test_relay_filtering(input_relays: list[int], expected_relays: list[int]):
assert util.filter_relays(input_relays) == expected_relays
3 changes: 2 additions & 1 deletion zigpy/appdb.py
Expand Up @@ -745,7 +745,8 @@ async def _load_relays(self) -> None:
async with self.execute(f"SELECT * FROM relays{DB_V}") as cursor:
async for (ieee, value) in cursor:
dev = self._application.get_device(ieee)
dev.relays, _ = t.Relays.deserialize(value)
relays, _ = t.Relays.deserialize(value)
dev.relays = zigpy.util.filter_relays(relays)

async def _load_neighbors(self) -> None:
async with self.execute(f"SELECT * FROM neighbors{DB_V}") as cursor:
Expand Down
3 changes: 1 addition & 2 deletions zigpy/application.py
Expand Up @@ -598,8 +598,7 @@ def handle_relays(self, nwk: t.NWK, relays: list[t.NWK]) -> None:
f"discover_unknown_device_from_relays-nwk={nwk!r}",
)
else:
# `relays` is a property with a setter that emits an event
device.relays = relays
device.relays = zigpy.util.filter_relays(relays)

@classmethod
async def probe(cls, device_config: dict[str, Any]) -> bool | dict[str, Any]:
Expand Down
12 changes: 12 additions & 0 deletions zigpy/util.py
Expand Up @@ -450,3 +450,15 @@ def __repr__(self) -> str:

def __hash__(self) -> int:
return hash(self.name)


def filter_relays(relays: list[int]) -> list[int]:
"""Filter out invalid relays."""
filtered_relays = []

# BUG: relays sometimes include 0x0000 or duplicate entries
for relay in relays:
if relay != 0x0000 and relay not in filtered_relays:
filtered_relays.append(relay)

return filtered_relays