Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Aussie Broadband integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from aiohttp import ClientError
9 from aussiebb.asyncio import AussieBB, AuthenticationException
10 from aussiebb.const import FETCH_TYPES
11 import voluptuous as vol
12 
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 
17 from .const import CONF_SERVICES, DOMAIN
18 
19 
20 class AussieBroadbandConfigFlow(ConfigFlow, domain=DOMAIN):
21  """Handle a config flow for Aussie Broadband."""
22 
23  VERSION = 1
24 
25  _reauth_username: str
26 
27  def __init__(self) -> None:
28  """Initialize the config flow."""
29  self.datadata: dict = {}
30  self.options: dict = {CONF_SERVICES: []}
31  self.servicesservices: list[dict[str, Any]] = []
32  self.clientclient: AussieBB | None = None
33 
34  async def async_auth(self, user_input: dict[str, str]) -> dict[str, str] | None:
35  """Reusable Auth Helper."""
36  self.clientclient = AussieBB(
37  user_input[CONF_USERNAME],
38  user_input[CONF_PASSWORD],
39  async_get_clientsession(self.hass),
40  )
41  try:
42  await self.clientclient.login()
43  except AuthenticationException:
44  return {"base": "invalid_auth"}
45  except ClientError:
46  return {"base": "cannot_connect"}
47  return None
48 
49  async def async_step_user(
50  self, user_input: dict[str, Any] | None = None
51  ) -> ConfigFlowResult:
52  """Handle the initial step."""
53  errors: dict[str, str] | None = None
54  if user_input is not None:
55  if not (errors := await self.async_authasync_auth(user_input)):
56  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_USERNAME].lower())
57  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
58 
59  self.datadata = user_input
60  self.servicesservices = await self.clientclient.get_services(drop_types=FETCH_TYPES) # type: ignore[union-attr]
61 
62  if not self.servicesservices:
63  return self.async_abortasync_abortasync_abort(reason="no_services_found")
64 
65  return self.async_create_entryasync_create_entryasync_create_entry(
66  title=self.datadata[CONF_USERNAME],
67  data=self.datadata,
68  )
69 
70  return self.async_show_formasync_show_formasync_show_form(
71  step_id="user",
72  data_schema=vol.Schema(
73  {
74  vol.Required(CONF_USERNAME): str,
75  vol.Required(CONF_PASSWORD): str,
76  }
77  ),
78  errors=errors,
79  )
80 
81  async def async_step_reauth(
82  self, entry_data: Mapping[str, Any]
83  ) -> ConfigFlowResult:
84  """Handle reauth on credential failure."""
85  self._reauth_username_reauth_username = entry_data[CONF_USERNAME]
86 
87  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
88 
90  self, user_input: dict[str, str] | None = None
91  ) -> ConfigFlowResult:
92  """Handle users reauth credentials."""
93 
94  errors: dict[str, str] | None = None
95 
96  if user_input:
97  data = {
98  CONF_USERNAME: self._reauth_username_reauth_username,
99  CONF_PASSWORD: user_input[CONF_PASSWORD],
100  }
101 
102  if not (errors := await self.async_authasync_auth(data)):
103  return self.async_update_reload_and_abortasync_update_reload_and_abort(
104  self._get_reauth_entry_get_reauth_entry(), data=data
105  )
106 
107  return self.async_show_formasync_show_formasync_show_form(
108  step_id="reauth_confirm",
109  description_placeholders={"username": self._reauth_username_reauth_username},
110  data_schema=vol.Schema(
111  {
112  vol.Required(CONF_PASSWORD): str,
113  }
114  ),
115  errors=errors,
116  )
ConfigFlowResult async_step_reauth_confirm(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:91
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:83
dict[str, str]|None async_auth(self, dict[str, str] user_input)
Definition: config_flow.py:34
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:51
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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_FlowResultT async_abort(self, *str reason, 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)