1 """Define a config flow manager for AirVisual Pro."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
6 from dataclasses
import dataclass, field
9 from pyairvisual.node
import (
10 InvalidAuthenticationError,
15 import voluptuous
as vol
20 from .const
import DOMAIN, LOGGER
22 STEP_REAUTH_SCHEMA = vol.Schema(
24 vol.Required(CONF_PASSWORD): str,
28 STEP_USER_SCHEMA = vol.Schema(
30 vol.Required(CONF_IP_ADDRESS): str,
31 vol.Required(CONF_PASSWORD): str,
38 """Define a validation result."""
40 serial_number: str |
None =
None
41 errors: dict[str, Any] = field(default_factory=dict)
45 ip_address: str, password: str
46 ) -> ValidationResult:
47 """Validate an IP address/password combo."""
48 node = NodeSamba(ip_address, password)
52 await node.async_connect()
53 measurements = await node.async_get_latest_measurements()
54 except InvalidAuthenticationError
as err:
55 LOGGER.error(
"Invalid password for Pro at IP address %s: %s", ip_address, err)
56 errors[
"base"] =
"invalid_auth"
57 except NodeConnectionError
as err:
58 LOGGER.error(
"Cannot connect to Pro at IP address %s: %s", ip_address, err)
59 errors[
"base"] =
"cannot_connect"
60 except NodeProError
as err:
61 LOGGER.error(
"Unknown Pro error while connecting to %s: %s", ip_address, err)
62 errors[
"base"] =
"unknown"
63 except Exception
as err:
64 LOGGER.exception(
"Unknown error while connecting to %s: %s", ip_address, err)
65 errors[
"base"] =
"unknown"
69 await node.async_disconnect()
75 """Handle an AirVisual Pro config flow."""
79 _reauth_entry_data: Mapping[str, Any]
82 """Import a config entry from `airvisual` integration (see #83882)."""
86 self, entry_data: Mapping[str, Any]
87 ) -> ConfigFlowResult:
88 """Handle configuration by re-auth."""
93 self, user_input: dict[str, Any] |
None =
None
94 ) -> ConfigFlowResult:
95 """Handle the re-auth step."""
96 if user_input
is None:
98 step_id=
"reauth_confirm", data_schema=STEP_REAUTH_SCHEMA
105 if validation_result.errors:
107 step_id=
"reauth_confirm",
108 data_schema=STEP_REAUTH_SCHEMA,
109 errors=validation_result.errors,
117 self, user_input: dict[str, str] |
None =
None
118 ) -> ConfigFlowResult:
119 """Handle the initial step."""
123 ip_address = user_input[CONF_IP_ADDRESS]
126 ip_address, user_input[CONF_PASSWORD]
129 if validation_result.errors:
132 data_schema=STEP_USER_SCHEMA,
133 errors=validation_result.errors,
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
ConfigFlowResult async_step_user(self, dict[str, str]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult async_step_reauth_confirm(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)
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_step_user(self, dict[str, Any]|None user_input=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)
ValidationResult async_validate_credentials(str ip_address, str password)