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

pull/109940/head^2
Marcel van der Veldt 2024-02-08 13:14:10 +01:00 committed by GitHub
parent c7957f8e94
commit e7213a4192
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 2 deletions

View File

@ -52,11 +52,27 @@ class MatterAdapter:
async def setup_nodes(self) -> None: async def setup_nodes(self) -> None:
"""Set up all existing nodes and subscribe to new nodes.""" """Set up all existing nodes and subscribe to new nodes."""
initialized_nodes: set[int] = set()
for node in self.matter_client.get_nodes(): 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) self._setup_node(node)
def node_added_callback(event: EventType, node: MatterNode) -> None: def node_added_callback(event: EventType, node: MatterNode) -> None:
"""Handle node added event.""" """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) self._setup_node(node)
def endpoint_added_callback(event: EventType, data: dict[str, int]) -> None: def endpoint_added_callback(event: EventType, data: dict[str, int]) -> None:
@ -116,6 +132,11 @@ class MatterAdapter:
callback=node_added_callback, event_filter=EventType.NODE_ADDED 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: def _setup_node(self, node: MatterNode) -> None:
"""Set up an node.""" """Set up an node."""

View File

@ -144,10 +144,10 @@ async def test_node_added_subscription(
integration: MagicMock, integration: MagicMock,
) -> None: ) -> None:
"""Test subscription to new devices work.""" """Test subscription to new devices work."""
assert matter_client.subscribe_events.call_count == 4 assert matter_client.subscribe_events.call_count == 5
assert ( assert (
matter_client.subscribe_events.call_args.kwargs["event_filter"] 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"] node_added_callback = matter_client.subscribe_events.call_args.kwargs["callback"]