Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for UPB PIM integration."""
2 
3 import asyncio
4 from contextlib import suppress
5 import logging
6 from typing import Any
7 from urllib.parse import urlparse
8 
9 import upb_lib
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_ADDRESS, CONF_FILE_PATH, CONF_HOST, CONF_PROTOCOL
14 from homeassistant.exceptions import HomeAssistantError
15 
16 from .const import DOMAIN
17 
18 _LOGGER = logging.getLogger(__name__)
19 PROTOCOL_MAP = {"TCP": "tcp://", "Serial port": "serial://"}
20 DATA_SCHEMA = vol.Schema(
21  {
22  vol.Required(CONF_PROTOCOL, default="Serial port"): vol.In(
23  ["TCP", "Serial port"]
24  ),
25  vol.Required(CONF_ADDRESS): str,
26  vol.Required(CONF_FILE_PATH, default=""): str,
27  }
28 )
29 VALIDATE_TIMEOUT = 15
30 
31 
32 async def _validate_input(data):
33  """Validate the user input allows us to connect."""
34 
35  def _connected_callback():
36  connected_event.set()
37 
38  connected_event = asyncio.Event()
39  file_path = data.get(CONF_FILE_PATH)
40  url = _make_url_from_data(data)
41 
42  upb = upb_lib.UpbPim({"url": url, "UPStartExportFile": file_path})
43 
44  await upb.async_connect(_connected_callback)
45 
46  if not upb.config_ok:
47  _LOGGER.error("Missing or invalid UPB file: %s", file_path)
48  raise InvalidUpbFile
49 
50  with suppress(TimeoutError):
51  async with asyncio.timeout(VALIDATE_TIMEOUT):
52  await connected_event.wait()
53 
54  upb.disconnect()
55 
56  if not connected_event.is_set():
57  _LOGGER.error(
58  "Timed out after %d seconds trying to connect with UPB PIM at %s",
59  VALIDATE_TIMEOUT,
60  url,
61  )
62  raise CannotConnect
63 
64  # Return info that you want to store in the config entry.
65  return (upb.network_id, {"title": "UPB", CONF_HOST: url, CONF_FILE_PATH: file_path})
66 
67 
69  if host := data.get(CONF_HOST):
70  return host
71 
72  protocol = PROTOCOL_MAP[data[CONF_PROTOCOL]]
73  address = data[CONF_ADDRESS]
74  return f"{protocol}{address}"
75 
76 
77 class UPBConfigFlow(ConfigFlow, domain=DOMAIN):
78  """Handle a config flow for UPB PIM."""
79 
80  VERSION = 1
81  MINOR_VERSION = 2
82 
83  async def async_step_user(
84  self, user_input: dict[str, Any] | None = None
85  ) -> ConfigFlowResult:
86  """Handle the initial step."""
87  errors = {}
88  if user_input is not None:
89  try:
90  if self._url_already_configured_url_already_configured(_make_url_from_data(user_input)):
91  return self.async_abortasync_abortasync_abort(reason="already_configured")
92  network_id, info = await _validate_input(user_input)
93  except CannotConnect:
94  errors["base"] = "cannot_connect"
95  except InvalidUpbFile:
96  errors["base"] = "invalid_upb_file"
97  except Exception:
98  _LOGGER.exception("Unexpected exception")
99  errors["base"] = "unknown"
100 
101  if "base" not in errors:
102  await self.async_set_unique_idasync_set_unique_id(str(network_id))
103  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
104 
105  return self.async_create_entryasync_create_entryasync_create_entry(
106  title=info["title"],
107  data={
108  CONF_HOST: info[CONF_HOST],
109  CONF_FILE_PATH: user_input[CONF_FILE_PATH],
110  },
111  )
112 
113  return self.async_show_formasync_show_formasync_show_form(
114  step_id="user", data_schema=DATA_SCHEMA, errors=errors
115  )
116 
117  def _url_already_configured(self, url):
118  """See if we already have a UPB PIM matching user input configured."""
119  existing_hosts = {
120  urlparse(entry.data[CONF_HOST]).hostname
121  for entry in self._async_current_entries_async_current_entries()
122  }
123  return urlparse(url).hostname in existing_hosts
124 
125 
127  """Error to indicate we cannot connect."""
128 
129 
130 class InvalidUpbFile(HomeAssistantError):
131  """Error to indicate there is invalid or missing UPB config file."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:85
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)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=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)