Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Bring! integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 from bring_api import (
10  Bring,
11  BringAuthException,
12  BringAuthResponse,
13  BringRequestException,
14 )
15 import voluptuous as vol
16 
17 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
18 from homeassistant.const import CONF_EMAIL, CONF_NAME, CONF_PASSWORD
19 from homeassistant.helpers.aiohttp_client import async_get_clientsession
21  TextSelector,
22  TextSelectorConfig,
23  TextSelectorType,
24 )
25 
26 from . import BringConfigEntry
27 from .const import DOMAIN
28 
29 _LOGGER = logging.getLogger(__name__)
30 
31 STEP_USER_DATA_SCHEMA = vol.Schema(
32  {
33  vol.Required(CONF_EMAIL): TextSelector(
35  type=TextSelectorType.EMAIL,
36  autocomplete="email",
37  ),
38  ),
39  vol.Required(CONF_PASSWORD): TextSelector(
41  type=TextSelectorType.PASSWORD,
42  autocomplete="current-password",
43  ),
44  ),
45  }
46 )
47 
48 
49 class BringConfigFlow(ConfigFlow, domain=DOMAIN):
50  """Handle a config flow for Bring!."""
51 
52  VERSION = 1
53  reauth_entry: BringConfigEntry
54  info: BringAuthResponse
55 
56  async def async_step_user(
57  self, user_input: dict[str, Any] | None = None
58  ) -> ConfigFlowResult:
59  """Handle the initial step."""
60  errors: dict[str, str] = {}
61  if user_input is not None and not (
62  errors := await self.validate_inputvalidate_input(user_input)
63  ):
64  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
65  return self.async_create_entryasync_create_entryasync_create_entry(
66  title=self.infoinfo.get("name") or user_input[CONF_EMAIL], data=user_input
67  )
68 
69  return self.async_show_formasync_show_formasync_show_form(
70  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
71  )
72 
73  async def async_step_reauth(
74  self, entry_data: Mapping[str, Any]
75  ) -> ConfigFlowResult:
76  """Perform reauth upon an API authentication error."""
77  self.reauth_entryreauth_entry = self._get_reauth_entry_get_reauth_entry()
78  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
79 
81  self, user_input: dict[str, Any] | None = None
82  ) -> ConfigFlowResult:
83  """Dialog that informs the user that reauth is required."""
84  errors: dict[str, str] = {}
85 
86  if user_input is not None:
87  if not (errors := await self.validate_inputvalidate_input(user_input)):
88  return self.async_update_reload_and_abortasync_update_reload_and_abort(
89  self.reauth_entryreauth_entry, data=user_input
90  )
91 
92  return self.async_show_formasync_show_formasync_show_form(
93  step_id="reauth_confirm",
94  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
95  data_schema=STEP_USER_DATA_SCHEMA,
96  suggested_values={CONF_EMAIL: self.reauth_entryreauth_entry.data[CONF_EMAIL]},
97  ),
98  description_placeholders={CONF_NAME: self.reauth_entryreauth_entry.title},
99  errors=errors,
100  )
101 
102  async def validate_input(self, user_input: Mapping[str, Any]) -> dict[str, str]:
103  """Auth Helper."""
104 
105  errors: dict[str, str] = {}
106  session = async_get_clientsession(self.hass)
107  bring = Bring(session, user_input[CONF_EMAIL], user_input[CONF_PASSWORD])
108 
109  try:
110  self.infoinfo = await bring.login()
111  except BringRequestException:
112  errors["base"] = "cannot_connect"
113  except BringAuthException:
114  errors["base"] = "invalid_auth"
115  except Exception:
116  _LOGGER.exception("Unexpected exception")
117  errors["base"] = "unknown"
118  else:
119  await self.async_set_unique_idasync_set_unique_id(bring.uuid)
120  return errors
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:75
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:82
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:58
dict[str, str] validate_input(self, Mapping[str, Any] user_input)
Definition: config_flow.py:102
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_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
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)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
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)