Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Adds config flow for NextDNS."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from aiohttp.client_exceptions import ClientConnectorError
9 from nextdns import ApiError, InvalidApiKeyError, NextDns
10 from tenacity import RetryError
11 import voluptuous as vol
12 
13 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_API_KEY, CONF_PROFILE_NAME
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.aiohttp_client import async_get_clientsession
17 
18 from .const import CONF_PROFILE_ID, DOMAIN
19 
20 AUTH_SCHEMA = vol.Schema({vol.Required(CONF_API_KEY): str})
21 
22 
23 async def async_init_nextdns(hass: HomeAssistant, api_key: str) -> NextDns:
24  """Check if credentials are valid."""
25  websession = async_get_clientsession(hass)
26 
27  return await NextDns.create(websession, api_key)
28 
29 
30 class NextDnsFlowHandler(ConfigFlow, domain=DOMAIN):
31  """Config flow for NextDNS."""
32 
33  VERSION = 1
34 
35  def __init__(self) -> None:
36  """Initialize the config flow."""
37  self.nextdnsnextdns: NextDns
38  self.api_keyapi_key: str
39 
40  async def async_step_user(
41  self, user_input: dict[str, Any] | None = None
42  ) -> ConfigFlowResult:
43  """Handle a flow initialized by the user."""
44  errors: dict[str, str] = {}
45 
46  if user_input is not None:
47  self.api_keyapi_key = user_input[CONF_API_KEY]
48  try:
49  self.nextdnsnextdns = await async_init_nextdns(self.hass, self.api_keyapi_key)
50  except InvalidApiKeyError:
51  errors["base"] = "invalid_api_key"
52  except (ApiError, ClientConnectorError, RetryError, TimeoutError):
53  errors["base"] = "cannot_connect"
54  except Exception: # noqa: BLE001
55  errors["base"] = "unknown"
56  else:
57  return await self.async_step_profilesasync_step_profiles()
58 
59  return self.async_show_formasync_show_formasync_show_form(
60  step_id="user",
61  data_schema=AUTH_SCHEMA,
62  errors=errors,
63  )
64 
66  self, user_input: dict[str, Any] | None = None
67  ) -> ConfigFlowResult:
68  """Handle the profiles step."""
69  errors: dict[str, str] = {}
70 
71  if user_input is not None:
72  profile_name = user_input[CONF_PROFILE_NAME]
73  profile_id = self.nextdnsnextdns.get_profile_id(profile_name)
74 
75  await self.async_set_unique_idasync_set_unique_id(profile_id)
76  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
77 
78  return self.async_create_entryasync_create_entryasync_create_entry(
79  title=profile_name,
80  data={CONF_PROFILE_ID: profile_id, CONF_API_KEY: self.api_keyapi_key},
81  )
82 
83  return self.async_show_formasync_show_formasync_show_form(
84  step_id="profiles",
85  data_schema=vol.Schema(
86  {
87  vol.Required(CONF_PROFILE_NAME): vol.In(
88  [profile.name for profile in self.nextdnsnextdns.profiles]
89  )
90  }
91  ),
92  errors=errors,
93  )
94 
95  async def async_step_reauth(
96  self, entry_data: Mapping[str, Any]
97  ) -> ConfigFlowResult:
98  """Handle configuration by re-auth."""
99  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
100 
102  self, user_input: dict[str, Any] | None = None
103  ) -> ConfigFlowResult:
104  """Dialog that informs the user that reauth is required."""
105  errors: dict[str, str] = {}
106 
107  if user_input is not None:
108  try:
109  await async_init_nextdns(self.hass, user_input[CONF_API_KEY])
110  except InvalidApiKeyError:
111  errors["base"] = "invalid_api_key"
112  except (ApiError, ClientConnectorError, RetryError, TimeoutError):
113  errors["base"] = "cannot_connect"
114  except Exception: # noqa: BLE001
115  errors["base"] = "unknown"
116  else:
117  return self.async_update_reload_and_abortasync_update_reload_and_abort(
118  self._get_reauth_entry_get_reauth_entry(), data_updates=user_input
119  )
120 
121  return self.async_show_formasync_show_formasync_show_form(
122  step_id="reauth_confirm",
123  data_schema=AUTH_SCHEMA,
124  errors=errors,
125  )
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:97
ConfigFlowResult async_step_profiles(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:67
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:103
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:42
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)
NextDns async_init_nextdns(HomeAssistant hass, str api_key)
Definition: config_flow.py:23
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)