Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Alpha2 config flow."""
2 
3 import logging
4 from typing import Any
5 
6 import aiohttp
7 from moehlenhoff_alpha2 import Alpha2Base
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_HOST
12 
13 from .const import DOMAIN
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
18 
19 
20 async def validate_input(data: dict[str, Any]) -> dict[str, str]:
21  """Validate the user input allows us to connect.
22 
23  Data has the keys from DATA_SCHEMA with values provided by the user.
24  """
25 
26  base = Alpha2Base(data[CONF_HOST])
27  try:
28  await base.update_data()
29  except (aiohttp.client_exceptions.ClientConnectorError, TimeoutError):
30  return {"error": "cannot_connect"}
31  except Exception:
32  _LOGGER.exception("Unexpected exception")
33  return {"error": "unknown"}
34 
35  # Return info that you want to store in the config entry.
36  return {"title": base.name}
37 
38 
39 class Alpha2BaseConfigFlow(ConfigFlow, domain=DOMAIN):
40  """Möhlenhoff Alpha2 config flow."""
41 
42  VERSION = 1
43 
44  async def async_step_user(
45  self, user_input: dict[str, Any] | None = None
46  ) -> ConfigFlowResult:
47  """Handle a flow initialized by the user."""
48  errors = {}
49  if user_input is not None:
50  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
51  result = await validate_input(user_input)
52  if result.get("error"):
53  errors["base"] = result["error"]
54  else:
55  return self.async_create_entryasync_create_entryasync_create_entry(title=result["title"], data=user_input)
56 
57  return self.async_show_formasync_show_formasync_show_form(
58  step_id="user", data_schema=DATA_SCHEMA, errors=errors
59  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:46
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)
dict[str, str] validate_input(dict[str, Any] data)
Definition: config_flow.py:20