Skip to content

Commit

Permalink
Handle Matter nodes that become available after startup is done (#109956
Browse files Browse the repository at this point in the history
)
  • Loading branch information
marcelveldt authored Feb 8, 2024
1 parent c7957f8 commit e7213a4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
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)

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

0 comments on commit e7213a4

Please sign in to comment.