Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure Freedompro."""
2 
3 from typing import Any
4 
5 from pyfreedompro import get_list
6 import voluptuous as vol
7 
8 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
9 from homeassistant.const import CONF_API_KEY
10 from homeassistant.core import HomeAssistant
11 from homeassistant.exceptions import HomeAssistantError
12 from homeassistant.helpers import aiohttp_client
13 
14 from .const import DOMAIN
15 
16 STEP_USER_DATA_SCHEMA = vol.Schema({vol.Required(CONF_API_KEY): str})
17 
18 
19 class Hub:
20  """Freedompro Hub class."""
21 
22  def __init__(self, hass: HomeAssistant, api_key: str) -> None:
23  """Freedompro Hub class init."""
24  self._hass_hass = hass
25  self._api_key_api_key = api_key
26 
27  async def authenticate(self) -> dict[str, Any]:
28  """Freedompro Hub class authenticate."""
29  return await get_list(
30  aiohttp_client.async_get_clientsession(self._hass_hass), self._api_key_api_key
31  )
32 
33 
34 async def validate_input(hass: HomeAssistant, api_key: str) -> None:
35  """Validate api key."""
36  hub = Hub(hass, api_key)
37  result = await hub.authenticate()
38  if result["state"] is False:
39  if result["code"] == -201:
40  raise InvalidAuth
41  if result["code"] == -200:
42  raise CannotConnect
43 
44 
45 class FreedomProConfigFlow(ConfigFlow, domain=DOMAIN):
46  """Handle a config flow."""
47 
48  VERSION = 1
49 
50  async def async_step_user(
51  self, user_input: dict[str, Any] | None = None
52  ) -> ConfigFlowResult:
53  """Show the setup form to the user."""
54  if user_input is None:
55  return self.async_show_formasync_show_formasync_show_form(
56  step_id="user", data_schema=STEP_USER_DATA_SCHEMA
57  )
58 
59  errors = {}
60 
61  try:
62  await validate_input(self.hass, user_input[CONF_API_KEY])
63  except CannotConnect:
64  errors["base"] = "cannot_connect"
65  except InvalidAuth:
66  errors["base"] = "invalid_auth"
67  else:
68  return self.async_create_entryasync_create_entryasync_create_entry(title="Freedompro", data=user_input)
69 
70  return self.async_show_formasync_show_formasync_show_form(
71  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
72  )
73 
74 
76  """Error to indicate we cannot connect."""
77 
78 
79 class InvalidAuth(HomeAssistantError):
80  """Error to indicate there is invalid auth."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:52
None __init__(self, HomeAssistant hass, str api_key)
Definition: config_flow.py:22
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_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)
None validate_input(HomeAssistant hass, str api_key)
Definition: config_flow.py:34