Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config Flow for Flick Electric integration."""
2 
3 import asyncio
4 import logging
5 from typing import Any
6 
7 from pyflick.authentication import AuthException, SimpleFlickAuth
8 from pyflick.const import DEFAULT_CLIENT_ID, DEFAULT_CLIENT_SECRET
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import (
13  CONF_CLIENT_ID,
14  CONF_CLIENT_SECRET,
15  CONF_PASSWORD,
16  CONF_USERNAME,
17 )
18 from homeassistant.exceptions import HomeAssistantError
19 from homeassistant.helpers import aiohttp_client
20 
21 from .const import DOMAIN
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 DATA_SCHEMA = vol.Schema(
26  {
27  vol.Required(CONF_USERNAME): str,
28  vol.Required(CONF_PASSWORD): str,
29  vol.Optional(CONF_CLIENT_ID): str,
30  vol.Optional(CONF_CLIENT_SECRET): str,
31  }
32 )
33 
34 
35 class FlickConfigFlow(ConfigFlow, domain=DOMAIN):
36  """Flick config flow."""
37 
38  VERSION = 1
39 
40  async def _validate_input(self, user_input):
41  auth = SimpleFlickAuth(
42  username=user_input[CONF_USERNAME],
43  password=user_input[CONF_PASSWORD],
44  websession=aiohttp_client.async_get_clientsession(self.hass),
45  client_id=user_input.get(CONF_CLIENT_ID, DEFAULT_CLIENT_ID),
46  client_secret=user_input.get(CONF_CLIENT_SECRET, DEFAULT_CLIENT_SECRET),
47  )
48 
49  try:
50  async with asyncio.timeout(60):
51  token = await auth.async_get_access_token()
52  except TimeoutError as err:
53  raise CannotConnect from err
54  except AuthException as err:
55  raise InvalidAuth from err
56 
57  return token is not None
58 
59  async def async_step_user(
60  self, user_input: dict[str, Any] | None = None
61  ) -> ConfigFlowResult:
62  """Handle gathering login info."""
63  errors = {}
64  if user_input is not None:
65  try:
66  await self._validate_input_validate_input(user_input)
67  except CannotConnect:
68  errors["base"] = "cannot_connect"
69  except InvalidAuth:
70  errors["base"] = "invalid_auth"
71  except Exception:
72  _LOGGER.exception("Unexpected exception")
73  errors["base"] = "unknown"
74  else:
75  await self.async_set_unique_idasync_set_unique_id(
76  f"flick_electric_{user_input[CONF_USERNAME]}"
77  )
78  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
79 
80  return self.async_create_entryasync_create_entryasync_create_entry(
81  title=f"Flick Electric: {user_input[CONF_USERNAME]}",
82  data=user_input,
83  )
84 
85  return self.async_show_formasync_show_formasync_show_form(
86  step_id="user", data_schema=DATA_SCHEMA, errors=errors
87  )
88 
89 
91  """Error to indicate we cannot connect."""
92 
93 
94 class InvalidAuth(HomeAssistantError):
95  """Error to indicate there is invalid auth."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:61
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)
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)