Add network and callback support to SSDP (#51019)
Co-authored-by: Ruslan Sayfutdinov <ruslan@sayfutdinov.com> Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
parent
02cbb2025e
commit
fb50cf9840
11 changed files with 900 additions and 314 deletions
50
homeassistant/components/ssdp/flow.py
Normal file
50
homeassistant/components/ssdp/flow.py
Normal file
|
@ -0,0 +1,50 @@
|
|||
"""The SSDP integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Coroutine
|
||||
from typing import Any, TypedDict
|
||||
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
|
||||
|
||||
class SSDPFlow(TypedDict):
|
||||
"""A queued ssdp discovery flow."""
|
||||
|
||||
domain: str
|
||||
context: dict[str, Any]
|
||||
data: dict
|
||||
|
||||
|
||||
class FlowDispatcher:
|
||||
"""Dispatch discovery flows."""
|
||||
|
||||
def __init__(self, hass: HomeAssistant) -> None:
|
||||
"""Init the discovery dispatcher."""
|
||||
self.hass = hass
|
||||
self.pending_flows: list[SSDPFlow] = []
|
||||
self.started = False
|
||||
|
||||
@callback
|
||||
def async_start(self, *_: Any) -> None:
|
||||
"""Start processing pending flows."""
|
||||
self.started = True
|
||||
self.hass.loop.call_soon(self._async_process_pending_flows)
|
||||
|
||||
def _async_process_pending_flows(self) -> None:
|
||||
for flow in self.pending_flows:
|
||||
self.hass.async_create_task(self._init_flow(flow))
|
||||
self.pending_flows = []
|
||||
|
||||
def create(self, flow: SSDPFlow) -> None:
|
||||
"""Create and add or queue a flow."""
|
||||
if self.started:
|
||||
self.hass.async_create_task(self._init_flow(flow))
|
||||
else:
|
||||
self.pending_flows.append(flow)
|
||||
|
||||
def _init_flow(self, flow: SSDPFlow) -> Coroutine[None, None, FlowResult]:
|
||||
"""Create a flow."""
|
||||
return self.hass.config_entries.flow.async_init(
|
||||
flow["domain"], context=flow["context"], data=flow["data"]
|
||||
)
|
Loading…
Add table
Add a link
Reference in a new issue