Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the Arcam FMJ component."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 from urllib.parse import urlparse
7 
8 from arcam.fmj.client import Client, ConnectionFailed
9 from arcam.fmj.utils import get_uniqueid_from_host, get_uniqueid_from_udn
10 import voluptuous as vol
11 
12 from homeassistant.components import ssdp
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_HOST, CONF_PORT
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 
17 from .const import DEFAULT_NAME, DEFAULT_PORT, DOMAIN
18 
19 
20 class ArcamFmjFlowHandler(ConfigFlow, domain=DOMAIN):
21  """Handle config flow."""
22 
23  VERSION = 1
24 
25  host: str
26  port: int
27 
29  self, host: str, port: int, uuid: str
30  ) -> None:
31  await self.async_set_unique_idasync_set_unique_id(uuid)
32  self._abort_if_unique_id_configured_abort_if_unique_id_configured({CONF_HOST: host, CONF_PORT: port})
33 
34  async def _async_check_and_create(self, host: str, port: int) -> ConfigFlowResult:
35  client = Client(host, port)
36  try:
37  await client.start()
38  except ConnectionFailed:
39  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
40  finally:
41  await client.stop()
42 
43  return self.async_create_entryasync_create_entryasync_create_entry(
44  title=f"{DEFAULT_NAME} ({host})",
45  data={CONF_HOST: host, CONF_PORT: port},
46  )
47 
48  async def async_step_user(
49  self, user_input: dict[str, Any] | None = None
50  ) -> ConfigFlowResult:
51  """Handle a discovered device."""
52  errors: dict[str, str] = {}
53 
54  if user_input is not None:
55  uuid = await get_uniqueid_from_host(
56  async_get_clientsession(self.hass), user_input[CONF_HOST]
57  )
58  if uuid:
59  await self._async_set_unique_id_and_update_async_set_unique_id_and_update(
60  user_input[CONF_HOST], user_input[CONF_PORT], uuid
61  )
62 
63  return await self._async_check_and_create_async_check_and_create(
64  user_input[CONF_HOST], user_input[CONF_PORT]
65  )
66 
67  fields = {
68  vol.Required(CONF_HOST): str,
69  vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
70  }
71 
72  return self.async_show_formasync_show_formasync_show_form(
73  step_id="user", data_schema=vol.Schema(fields), errors=errors
74  )
75 
76  async def async_step_confirm(
77  self, user_input: dict[str, Any] | None = None
78  ) -> ConfigFlowResult:
79  """Handle user-confirmation of discovered node."""
80  placeholders = {"host": self.hosthost}
81  self.context["title_placeholders"] = placeholders
82 
83  if user_input is not None:
84  return await self._async_check_and_create_async_check_and_create(self.hosthost, self.portport)
85 
86  return self.async_show_formasync_show_formasync_show_form(
87  step_id="confirm", description_placeholders=placeholders
88  )
89 
90  async def async_step_ssdp(
91  self, discovery_info: ssdp.SsdpServiceInfo
92  ) -> ConfigFlowResult:
93  """Handle a discovered device."""
94  host = str(urlparse(discovery_info.ssdp_location).hostname)
95  port = DEFAULT_PORT
96  uuid = get_uniqueid_from_udn(discovery_info.upnp[ssdp.ATTR_UPNP_UDN])
97  if not uuid:
98  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
99 
100  await self._async_set_unique_id_and_update_async_set_unique_id_and_update(host, port, uuid)
101 
102  self.hosthost = host
103  self.portport = DEFAULT_PORT
104  return await self.async_step_confirmasync_step_confirm()
ConfigFlowResult async_step_ssdp(self, ssdp.SsdpServiceInfo discovery_info)
Definition: config_flow.py:92
ConfigFlowResult _async_check_and_create(self, str host, int port)
Definition: config_flow.py:34
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:50
ConfigFlowResult async_step_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:78
None _async_set_unique_id_and_update(self, str host, int port, str uuid)
Definition: config_flow.py:30
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)
str
_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)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)