Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Autarco integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from autarco import Autarco, AutarcoAuthenticationError, AutarcoConnectionError
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
12 from homeassistant.helpers.aiohttp_client import async_get_clientsession
13 
14 from .const import DOMAIN
15 
16 DATA_SCHEMA = vol.Schema(
17  {
18  vol.Required(CONF_EMAIL): str,
19  vol.Required(CONF_PASSWORD): str,
20  }
21 )
22 
23 
24 class AutarcoConfigFlow(ConfigFlow, domain=DOMAIN):
25  """Handle a config flow for Autarco."""
26 
27  async def async_step_user(
28  self, user_input: dict[str, Any] | None = None
29  ) -> ConfigFlowResult:
30  """Handle the initial step."""
31  errors: dict[str, str] = {}
32  if user_input is not None:
33  self._async_abort_entries_match_async_abort_entries_match({CONF_EMAIL: user_input[CONF_EMAIL]})
34  client = Autarco(
35  email=user_input[CONF_EMAIL],
36  password=user_input[CONF_PASSWORD],
37  session=async_get_clientsession(self.hass),
38  )
39  try:
40  await client.get_account()
41  except AutarcoAuthenticationError:
42  errors["base"] = "invalid_auth"
43  except AutarcoConnectionError:
44  errors["base"] = "cannot_connect"
45  else:
46  return self.async_create_entryasync_create_entryasync_create_entry(
47  title=user_input[CONF_EMAIL],
48  data={
49  CONF_EMAIL: user_input[CONF_EMAIL],
50  CONF_PASSWORD: user_input[CONF_PASSWORD],
51  },
52  )
53  return self.async_show_formasync_show_formasync_show_form(
54  step_id="user",
55  errors=errors,
56  data_schema=DATA_SCHEMA,
57  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:29
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)
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)