Add WS command config_entries/flow/subscribe (#142459)

This commit is contained in:
Erik Montnemery
2025-04-10 16:58:46 +02:00
committed by GitHub
parent a26cdef427
commit eee6e8a2c3
5 changed files with 458 additions and 4 deletions

View File

@ -1375,6 +1375,7 @@ class ConfigEntriesFlowManager(
function=self._async_fire_discovery_event,
background=True,
)
self._flow_subscriptions: list[Callable[[str, str], None]] = []
async def async_wait_import_flow_initialized(self, handler: str) -> None:
"""Wait till all import flows in progress are initialized."""
@ -1461,6 +1462,13 @@ class ConfigEntriesFlowManager(
# Fire discovery event
await self._discovery_event_debouncer.async_call()
if result["type"] != data_entry_flow.FlowResultType.ABORT and source in (
DISCOVERY_SOURCES | {SOURCE_REAUTH}
):
# Notify listeners that a flow is created
for subscription in self._flow_subscriptions:
subscription("added", flow.flow_id)
return result
async def _async_init(
@ -1739,6 +1747,29 @@ class ConfigEntriesFlowManager(
return True
return False
@callback
def async_subscribe_flow(
self, listener: Callable[[str, str], None]
) -> CALLBACK_TYPE:
"""Subscribe to non user initiated flow init or remove."""
self._flow_subscriptions.append(listener)
return lambda: self._flow_subscriptions.remove(listener)
@callback
def _async_remove_flow_progress(self, flow_id: str) -> None:
"""Remove a flow from in progress."""
flow = self._progress.get(flow_id)
super()._async_remove_flow_progress(flow_id)
# Fire remove event for initialized non user initiated flows
if (
not flow
or flow.cur_step is None
or flow.source not in (DISCOVERY_SOURCES | {SOURCE_REAUTH})
):
return
for listeners in self._flow_subscriptions:
listeners("removed", flow_id)
class ConfigEntryItems(UserDict[str, ConfigEntry]):
"""Container for config items, maps config_entry_id -> entry.