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

Handle Matter nodes that become available after startup is done #109956

Merged
merged 2 commits into from
Feb 8, 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
21 changes: 21 additions & 0 deletions homeassistant/components/matter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,27 @@ def register_platform_handler(

async def setup_nodes(self) -> None:
"""Set up all existing nodes and subscribe to new nodes."""
initialized_nodes: set[int] = set()
for node in self.matter_client.get_nodes():
if not node.available:
# ignore un-initialized nodes at startup
# catch them later when they become available.
continue
initialized_nodes.add(node.node_id)
self._setup_node(node)
marcelveldt marked this conversation as resolved.
Show resolved Hide resolved

def node_added_callback(event: EventType, node: MatterNode) -> None:
"""Handle node added event."""
initialized_nodes.add(node.node_id)
self._setup_node(node)

def node_updated_callback(event: EventType, node: MatterNode) -> None:
"""Handle node updated event."""
if node.node_id in initialized_nodes:
return
if not node.available:
return
initialized_nodes.add(node.node_id)
self._setup_node(node)

def endpoint_added_callback(event: EventType, data: dict[str, int]) -> None:
Expand Down Expand Up @@ -116,6 +132,11 @@ def node_removed_callback(event: EventType, node_id: int) -> None:
callback=node_added_callback, event_filter=EventType.NODE_ADDED
)
)
self.config_entry.async_on_unload(
self.matter_client.subscribe_events(
callback=node_updated_callback, event_filter=EventType.NODE_UPDATED
)
)

def _setup_node(self, node: MatterNode) -> None:
"""Set up an node."""
Expand Down
4 changes: 2 additions & 2 deletions tests/components/matter/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ async def test_node_added_subscription(
integration: MagicMock,
) -> None:
"""Test subscription to new devices work."""
assert matter_client.subscribe_events.call_count == 4
assert matter_client.subscribe_events.call_count == 5
assert (
matter_client.subscribe_events.call_args.kwargs["event_filter"]
== EventType.NODE_ADDED
== EventType.NODE_UPDATED
)

node_added_callback = matter_client.subscribe_events.call_args.kwargs["callback"]
Expand Down