1 """Adds config flow for Nettigo Air Monitor."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
6 from dataclasses
import dataclass
10 from aiohttp.client_exceptions
import ClientConnectorError
11 from nettigo_air_monitor
import (
18 import voluptuous
as vol
27 from .const
import DOMAIN
32 """NAM device configuration class."""
38 _LOGGER = logging.getLogger(__name__)
40 AUTH_SCHEMA = vol.Schema(
41 {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
46 """Get device MAC address and auth_enabled property."""
49 options = ConnectionOptions(host)
50 nam = await NettigoAirMonitor.create(websession, options)
52 mac = await nam.async_get_mac_address()
58 hass: HomeAssistant, host: str, data: dict[str, Any]
60 """Check if credentials are valid."""
63 options = ConnectionOptions(host, data.get(CONF_USERNAME), data.get(CONF_PASSWORD))
65 nam = await NettigoAirMonitor.create(websession, options)
67 await nam.async_check_credentials()
71 """Config flow for Nettigo Air Monitor."""
79 self, user_input: dict[str, Any] |
None =
None
80 ) -> ConfigFlowResult:
81 """Handle a flow initialized by the user."""
82 errors: dict[str, str] = {}
84 if user_input
is not None:
85 self.
hosthost = user_input[CONF_HOST]
89 except (ApiError, ClientConnectorError, TimeoutError):
90 errors[
"base"] =
"cannot_connect"
91 except CannotGetMacError:
94 _LOGGER.exception(
"Unexpected exception")
95 errors[
"base"] =
"unknown"
100 if config.auth_enabled
is True:
110 data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
115 self, user_input: dict[str, Any] |
None =
None
116 ) -> ConfigFlowResult:
117 """Handle the credentials step."""
118 errors: dict[str, str] = {}
120 if user_input
is not None:
123 except AuthFailedError:
124 errors[
"base"] =
"invalid_auth"
125 except (ApiError, ClientConnectorError, TimeoutError):
126 errors[
"base"] =
"cannot_connect"
128 _LOGGER.exception(
"Unexpected exception")
129 errors[
"base"] =
"unknown"
133 data={**user_input, CONF_HOST: self.
hosthost},
137 step_id=
"credentials", data_schema=AUTH_SCHEMA, errors=errors
141 self, discovery_info: zeroconf.ZeroconfServiceInfo
142 ) -> ConfigFlowResult:
143 """Handle zeroconf discovery."""
144 self.
hosthost = discovery_info.host
145 self.context[
"title_placeholders"] = {
"host": self.
hosthost}
152 except (ApiError, ClientConnectorError, TimeoutError):
154 except CannotGetMacError:
163 self, user_input: dict[str, Any] |
None =
None
164 ) -> ConfigFlowResult:
165 """Handle discovery confirm."""
166 errors: dict[str, str] = {}
168 if user_input
is not None:
171 data={CONF_HOST: self.
hosthost},
174 if self.
_config_config.auth_enabled
is True:
180 step_id=
"confirm_discovery",
181 description_placeholders={
"host": self.
hosthost},
186 self, entry_data: Mapping[str, Any]
187 ) -> ConfigFlowResult:
188 """Handle configuration by re-auth."""
189 self.
hosthost = entry_data[CONF_HOST]
190 self.context[
"title_placeholders"] = {
"host": self.
hosthost}
194 self, user_input: dict[str, Any] |
None =
None
195 ) -> ConfigFlowResult:
196 """Dialog that informs the user that reauth is required."""
197 errors: dict[str, str] = {}
199 if user_input
is not None:
205 ClientConnectorError,
215 step_id=
"reauth_confirm",
216 description_placeholders={
"host": self.
hosthost},
217 data_schema=AUTH_SCHEMA,
222 self, user_input: dict[str, Any] |
None =
None
223 ) -> ConfigFlowResult:
224 """Handle a reconfiguration flow initialized by the user."""
227 self.
hosthost = reconfigure_entry.data[CONF_HOST]
229 if user_input
is not None:
232 except (ApiError, ClientConnectorError, TimeoutError):
233 errors[
"base"] =
"cannot_connect"
239 reconfigure_entry, data_updates={CONF_HOST: user_input[CONF_HOST]}
243 step_id=
"reconfigure",
244 data_schema=vol.Schema(
246 vol.Required(CONF_HOST, default=self.
hosthost): str,
249 description_placeholders={
"device_name": reconfigure_entry.title},
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_credentials(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_confirm_discovery(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry _get_reauth_entry(self)
None _set_confirm_only(self)
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=None)
ConfigEntry _get_reconfigure_entry(self)
None _abort_if_unique_id_mismatch(self, *str reason="unique_id_mismatch", 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)
NamConfig async_get_config(HomeAssistant hass, str host)
None async_check_credentials(HomeAssistant hass, str host, dict[str, Any] data)
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)