Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for APCUPSd integration."""
2 
3 from __future__ import annotations
4 
5 import asyncio
6 from typing import Any
7 
8 import aioapcaccess
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_HOST, CONF_PORT
13 from homeassistant.helpers import selector
15 
16 from .const import CONNECTION_TIMEOUT, DOMAIN
17 from .coordinator import APCUPSdData
18 
19 _PORT_SELECTOR = vol.All(
20  selector.NumberSelector(
21  selector.NumberSelectorConfig(
22  min=1, max=65535, mode=selector.NumberSelectorMode.BOX
23  ),
24  ),
25  vol.Coerce(int),
26 )
27 
28 _SCHEMA = vol.Schema(
29  {
30  vol.Required(CONF_HOST, default="localhost"): cv.string,
31  vol.Required(CONF_PORT, default=3551): _PORT_SELECTOR,
32  }
33 )
34 
35 
36 class ConfigFlowHandler(ConfigFlow, domain=DOMAIN):
37  """APCUPSd integration config flow."""
38 
39  VERSION = 1
40 
41  async def async_step_user(
42  self, user_input: dict[str, Any] | None = None
43  ) -> ConfigFlowResult:
44  """Handle the initial step."""
45 
46  if user_input is None:
47  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=_SCHEMA)
48 
49  host, port = user_input[CONF_HOST], user_input[CONF_PORT]
50 
51  # Abort if an entry with same host and port is present.
52  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: host, CONF_PORT: port})
53 
54  # Test the connection to the host and get the current status for serial number.
55  try:
56  async with asyncio.timeout(CONNECTION_TIMEOUT):
57  data = APCUPSdData(await aioapcaccess.request_status(host, port))
58  except (OSError, asyncio.IncompleteReadError, TimeoutError):
59  errors = {"base": "cannot_connect"}
60  return self.async_show_formasync_show_formasync_show_form(
61  step_id="user", data_schema=_SCHEMA, errors=errors
62  )
63 
64  # We _try_ to use the serial number of the UPS as the unique id since this field
65  # is not guaranteed to exist on all APC UPS models.
66  await self.async_set_unique_idasync_set_unique_id(data.serial_no)
67  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
68 
69  title = data.name or data.model or data.serial_no or "APC UPS"
70  return self.async_create_entryasync_create_entryasync_create_entry(title=title, data=user_input)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:43
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)