Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Linn / OpenHome."""
2 
3 import logging
4 from typing import Any
5 
7  ATTR_UPNP_FRIENDLY_NAME,
8  ATTR_UPNP_UDN,
9  SsdpServiceInfo,
10 )
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_HOST, CONF_NAME
13 
14 from .const import DOMAIN
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 
19 def _is_complete_discovery(discovery_info: SsdpServiceInfo) -> bool:
20  """Test if discovery is complete and usable."""
21  return bool(ATTR_UPNP_UDN in discovery_info.upnp and discovery_info.ssdp_location)
22 
23 
24 class OpenhomeConfigFlow(ConfigFlow, domain=DOMAIN):
25  """Handle an Openhome config flow."""
26 
27  _host: str | None
28  _name: str
29 
30  async def async_step_ssdp(
31  self, discovery_info: SsdpServiceInfo
32  ) -> ConfigFlowResult:
33  """Handle a flow initialized by discovery."""
34  _LOGGER.debug("async_step_ssdp: started")
35 
36  if not _is_complete_discovery(discovery_info):
37  _LOGGER.debug("async_step_ssdp: Incomplete discovery, ignoring")
38  return self.async_abortasync_abortasync_abort(reason="incomplete_discovery")
39 
40  _LOGGER.debug(
41  "async_step_ssdp: setting unique id %s", discovery_info.upnp[ATTR_UPNP_UDN]
42  )
43 
44  await self.async_set_unique_idasync_set_unique_id(discovery_info.upnp[ATTR_UPNP_UDN])
45  self._abort_if_unique_id_configured_abort_if_unique_id_configured({CONF_HOST: discovery_info.ssdp_location})
46 
47  _LOGGER.debug(
48  "async_step_ssdp: create entry %s", discovery_info.upnp[ATTR_UPNP_UDN]
49  )
50 
51  self._name_name = discovery_info.upnp[ATTR_UPNP_FRIENDLY_NAME]
52  self._host_host = discovery_info.ssdp_location
53 
54  return await self.async_step_confirm()
55 
56  async def async_step_confirm(
57  self, user_input: dict[str, Any] | None = None
58  ) -> ConfigFlowResult:
59  """Handle user-confirmation of discovered node."""
60 
61  if user_input is not None:
62  return self.async_create_entryasync_create_entryasync_create_entry(
63  title=self._name_name,
64  data={CONF_HOST: self._host_host},
65  )
66 
67  return self.async_show_formasync_show_formasync_show_form(
68  step_id="confirm",
69  description_placeholders={CONF_NAME: self._name_name},
70  )
ConfigFlowResult async_step_ssdp(self, SsdpServiceInfo discovery_info)
Definition: config_flow.py:32
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
bool _is_complete_discovery(SsdpServiceInfo discovery_info)
Definition: config_flow.py:19
config_entries.ConfigFlowResult async_step_confirm(self, dict[str, Any]|None user_input=None)