Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Oncue 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 aiooncue import LoginFailedException, Oncue
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 
16 from .const import CONNECTION_EXCEPTIONS, DOMAIN
17 
18 _LOGGER = logging.getLogger(__name__)
19 
20 
21 class OncueConfigFlow(ConfigFlow, domain=DOMAIN):
22  """Handle a config flow for Oncue."""
23 
24  VERSION = 1
25 
26  async def async_step_user(
27  self, user_input: dict[str, Any] | None = None
28  ) -> ConfigFlowResult:
29  """Handle the initial step."""
30  errors: dict[str, str] = {}
31 
32  if user_input is not None:
33  if not (errors := await self._async_validate_or_error_async_validate_or_error(user_input)):
34  normalized_username = user_input[CONF_USERNAME].lower()
35  await self.async_set_unique_idasync_set_unique_id(normalized_username)
36  self._abort_if_unique_id_configured_abort_if_unique_id_configured(
37  updates={
38  CONF_USERNAME: user_input[CONF_USERNAME],
39  CONF_PASSWORD: user_input[CONF_PASSWORD],
40  }
41  )
42  return self.async_create_entryasync_create_entryasync_create_entry(
43  title=normalized_username, data=user_input
44  )
45 
46  return self.async_show_formasync_show_formasync_show_form(
47  step_id="user",
48  data_schema=vol.Schema(
49  {
50  vol.Required(CONF_USERNAME): str,
51  vol.Required(CONF_PASSWORD): str,
52  }
53  ),
54  errors=errors,
55  )
56 
57  async def _async_validate_or_error(self, config: dict[str, Any]) -> dict[str, str]:
58  """Validate the user input."""
59  errors: dict[str, str] = {}
60  try:
61  await Oncue(
62  config[CONF_USERNAME],
63  config[CONF_PASSWORD],
64  async_get_clientsession(self.hass),
65  ).async_login()
66  except CONNECTION_EXCEPTIONS:
67  errors["base"] = "cannot_connect"
68  except LoginFailedException:
69  errors[CONF_PASSWORD] = "invalid_auth"
70  except Exception:
71  _LOGGER.exception("Unexpected exception")
72  errors["base"] = "unknown"
73  return errors
74 
75  async def async_step_reauth(
76  self, entry_data: Mapping[str, Any]
77  ) -> ConfigFlowResult:
78  """Handle reauth."""
79  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
80 
82  self, user_input: dict[str, Any] | None = None
83  ) -> ConfigFlowResult:
84  """Handle reauth input."""
85  errors: dict[str, str] = {}
86  reauth_entry = self._get_reauth_entry_get_reauth_entry()
87  existing_data = reauth_entry.data
88  description_placeholders: dict[str, str] = {
89  CONF_USERNAME: existing_data[CONF_USERNAME]
90  }
91  if user_input is not None:
92  new_config = {**existing_data, CONF_PASSWORD: user_input[CONF_PASSWORD]}
93  if not (errors := await self._async_validate_or_error_async_validate_or_error(new_config)):
94  return self.async_update_reload_and_abortasync_update_reload_and_abort(reauth_entry, data=new_config)
95 
96  return self.async_show_formasync_show_formasync_show_form(
97  description_placeholders=description_placeholders,
98  step_id="reauth_confirm",
99  data_schema=vol.Schema({vol.Required(CONF_PASSWORD): str}),
100  errors=errors,
101  )
dict[str, str] _async_validate_or_error(self, dict[str, Any] config)
Definition: config_flow.py:57
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:77
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:83
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:28
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)
_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)