Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Nice G.O. integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from datetime import datetime
7 import logging
8 from typing import Any
9 
10 from nice_go import AuthFailedError, NiceGOApi
11 import voluptuous as vol
12 
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_EMAIL, CONF_NAME, CONF_PASSWORD
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 
17 from .const import CONF_REFRESH_TOKEN, CONF_REFRESH_TOKEN_CREATION_TIME, DOMAIN
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 STEP_USER_DATA_SCHEMA = vol.Schema(
22  {
23  vol.Required(CONF_EMAIL): str,
24  vol.Required(CONF_PASSWORD): str,
25  }
26 )
27 
28 
29 class NiceGOConfigFlow(ConfigFlow, domain=DOMAIN):
30  """Handle a config flow for Nice G.O."""
31 
32  VERSION = 1
33 
34  async def async_step_user(
35  self, user_input: dict[str, Any] | None = None
36  ) -> ConfigFlowResult:
37  """Handle the initial step."""
38  errors: dict[str, str] = {}
39  if user_input is not None:
40  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_EMAIL])
41  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
42 
43  hub = NiceGOApi()
44 
45  try:
46  refresh_token = await hub.authenticate(
47  user_input[CONF_EMAIL],
48  user_input[CONF_PASSWORD],
49  async_get_clientsession(self.hass),
50  )
51  except AuthFailedError:
52  errors["base"] = "invalid_auth"
53  except Exception: # noqa: BLE001
54  _LOGGER.exception("Unexpected exception")
55  errors["base"] = "unknown"
56  else:
57  return self.async_create_entryasync_create_entryasync_create_entry(
58  title=user_input[CONF_EMAIL],
59  data={
60  CONF_EMAIL: user_input[CONF_EMAIL],
61  CONF_PASSWORD: user_input[CONF_PASSWORD],
62  CONF_REFRESH_TOKEN: refresh_token,
63  CONF_REFRESH_TOKEN_CREATION_TIME: datetime.now().timestamp(),
64  },
65  )
66 
67  return self.async_show_formasync_show_formasync_show_form(
68  step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
69  )
70 
71  async def async_step_reauth(
72  self, entry_data: Mapping[str, Any]
73  ) -> ConfigFlowResult:
74  """Handle re-authentication."""
75  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
76 
78  self, user_input: dict[str, Any] | None = None
79  ) -> ConfigFlowResult:
80  """Confirm re-authentication."""
81  errors = {}
82 
83  reauth_entry = self._get_reauth_entry_get_reauth_entry()
84  if user_input is not None:
85  hub = NiceGOApi()
86 
87  try:
88  refresh_token = await hub.authenticate(
89  user_input[CONF_EMAIL],
90  user_input[CONF_PASSWORD],
91  async_get_clientsession(self.hass),
92  )
93  except AuthFailedError:
94  errors["base"] = "invalid_auth"
95  except Exception: # noqa: BLE001
96  _LOGGER.exception("Unexpected exception")
97  errors["base"] = "unknown"
98  else:
99  return self.async_update_reload_and_abortasync_update_reload_and_abort(
100  reauth_entry,
101  data={
102  **user_input,
103  CONF_REFRESH_TOKEN: refresh_token,
104  CONF_REFRESH_TOKEN_CREATION_TIME: datetime.now().timestamp(),
105  },
106  unique_id=user_input[CONF_EMAIL],
107  )
108 
109  return self.async_show_formasync_show_formasync_show_form(
110  step_id="reauth_confirm",
111  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
112  STEP_USER_DATA_SCHEMA,
113  user_input or {CONF_EMAIL: reauth_entry.data[CONF_EMAIL]},
114  ),
115  description_placeholders={CONF_NAME: reauth_entry.title},
116  errors=errors,
117  )
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:79
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:73
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:36
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)
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)