Handle Matter nodes that become available after startup is done (#109956)
parent
c7957f8e94
commit
e7213a4192
|
@ -52,11 +52,27 @@ class MatterAdapter:
|
|||
|
||||
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:
|
||||
|
@ -116,6 +132,11 @@ class MatterAdapter:
|
|||
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."""
|
||||
|
|
|
@ -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"]
|
||||
|
|
Loading…
Reference in New Issue