Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Netgear LTE integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiohttp.cookiejar import CookieJar
8 from eternalegypt import Error, Modem
9 from eternalegypt.eternalegypt import Information
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_HOST, CONF_PASSWORD
14 from homeassistant.exceptions import HomeAssistantError
15 from homeassistant.helpers.aiohttp_client import async_create_clientsession
16 
17 from .const import DEFAULT_HOST, DOMAIN, LOGGER, MANUFACTURER
18 
19 
20 class NetgearLTEFlowHandler(ConfigFlow, domain=DOMAIN):
21  """Handle a config flow for Netgear LTE."""
22 
23  async def async_step_user(
24  self, user_input: dict[str, Any] | None = None
25  ) -> ConfigFlowResult:
26  """Handle a flow initiated by the user."""
27  errors = {}
28 
29  if user_input:
30  host = user_input[CONF_HOST]
31  password = user_input[CONF_PASSWORD]
32 
33  try:
34  info = await self._async_validate_input(host, password)
35  except InputValidationError as ex:
36  errors["base"] = ex.base
37  else:
38  await self.async_set_unique_idasync_set_unique_id(info.serial_number)
39  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
40  return self.async_create_entryasync_create_entryasync_create_entry(
41  title=f"{MANUFACTURER} {info.items['general.devicename']}",
42  data={CONF_HOST: host, CONF_PASSWORD: password},
43  )
44 
45  return self.async_show_formasync_show_formasync_show_form(
46  step_id="user",
47  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
48  vol.Schema(
49  {
50  vol.Required(CONF_HOST): str,
51  vol.Required(CONF_PASSWORD): str,
52  }
53  ),
54  user_input or {CONF_HOST: DEFAULT_HOST},
55  ),
56  errors=errors,
57  )
58 
59  async def _async_validate_input(self, host: str, password: str) -> Information:
60  """Validate login credentials."""
61  websession = async_create_clientsession(
62  self.hass, cookie_jar=CookieJar(unsafe=True)
63  )
64 
65  modem = Modem(
66  hostname=host,
67  password=password,
68  websession=websession,
69  )
70  try:
71  await modem.login()
72  info = await modem.information()
73  except Error as ex:
74  raise InputValidationError("cannot_connect") from ex
75  except Exception as ex:
76  LOGGER.exception("Unexpected exception")
77  raise InputValidationError("unknown") from ex
78  await modem.logout()
79  return info
80 
81 
83  """Error to indicate we cannot proceed due to invalid input."""
84 
85  def __init__(self, base: str) -> None:
86  """Initialize with error base."""
87  super().__init__()
88  self.basebase = base
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:25
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)
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_create_clientsession()
Definition: coordinator.py:51