Reduce discovery flow matching overhead (#107709)

This commit is contained in:
J. Nick Koston 2024-01-10 07:14:18 -10:00 committed by GitHub
parent 6a6c447c28
commit 7d18ad6fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View File

@ -201,11 +201,13 @@ class FlowManager(abc.ABC):
If match_context is passed, only return flows with a context that is a
superset of match_context.
"""
return any(
flow
for flow in self._async_progress_by_handler(handler, match_context)
if flow.init_data == data
)
if not (flows := self._handler_progress_index.get(handler)):
return False
match_items = match_context.items()
for progress in flows:
if match_items <= progress.context.items() and progress.init_data == data:
return True
return False
@callback
def async_get(self, flow_id: str) -> FlowResult:
@ -265,11 +267,11 @@ class FlowManager(abc.ABC):
is a superset of match_context.
"""
if not match_context:
return list(self._handler_progress_index.get(handler, []))
return list(self._handler_progress_index.get(handler, ()))
match_context_items = match_context.items()
return [
progress
for progress in self._handler_progress_index.get(handler, set())
for progress in self._handler_progress_index.get(handler, ())
if match_context_items <= progress.context.items()
]

View File

@ -546,6 +546,14 @@ async def test_async_has_matching_flow(
) -> None:
"""Test we can check for matching flows."""
manager.hass = hass
assert (
manager.async_has_matching_flow(
"test",
{"source": config_entries.SOURCE_HOMEKIT},
{"properties": {"id": "aa:bb:cc:dd:ee:ff"}},
)
is False
)
@manager.mock_reg_handler("test")
class TestFlow(data_entry_flow.FlowHandler):