1 """Config flow for brunt integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
9 from aiohttp
import ClientResponseError
10 from aiohttp.client_exceptions
import ServerDisconnectedError
11 from brunt
import BruntClientAsync
12 import voluptuous
as vol
17 from .const
import DOMAIN
19 _LOGGER = logging.getLogger(__name__)
21 DATA_SCHEMA = vol.Schema(
22 {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
24 REAUTH_SCHEMA = vol.Schema({vol.Required(CONF_PASSWORD): str})
27 async
def validate_input(user_input: dict[str, Any]) -> dict[str, str] |
None:
28 """Login to the brunt api and return errors if any."""
30 bapi = BruntClientAsync(
31 username=user_input[CONF_USERNAME],
32 password=user_input[CONF_PASSWORD],
35 await bapi.async_login()
36 except ClientResponseError
as exc:
38 _LOGGER.warning(
"Brunt Credentials are incorrect")
39 errors = {
"base":
"invalid_auth"}
41 _LOGGER.exception(
"Unknown error when trying to login to Brunt")
42 errors = {
"base":
"unknown"}
43 except ServerDisconnectedError:
44 _LOGGER.warning(
"Cannot connect to Brunt")
45 errors = {
"base":
"cannot_connect"}
47 _LOGGER.exception(
"Unknown error when trying to login to Brunt")
48 errors = {
"base":
"unknown"}
50 await bapi.async_close()
55 """Handle a config flow for Brunt."""
60 self, user_input: dict[str, Any] |
None =
None
61 ) -> ConfigFlowResult:
62 """Handle the initial step."""
63 if user_input
is None:
67 if errors
is not None:
69 step_id=
"user", data_schema=DATA_SCHEMA, errors=errors
75 title=user_input[CONF_USERNAME],
80 self, entry_data: Mapping[str, Any]
81 ) -> ConfigFlowResult:
82 """Perform reauth upon an API authentication error."""
86 self, user_input: dict[str, Any] |
None =
None
87 ) -> ConfigFlowResult:
88 """Dialog that informs the user that reauth is required."""
90 username = reauth_entry.data[CONF_USERNAME]
91 if user_input
is None:
93 step_id=
"reauth_confirm",
94 data_schema=REAUTH_SCHEMA,
95 description_placeholders={
96 CONF_USERNAME: username,
97 CONF_NAME: reauth_entry.title,
100 user_input[CONF_USERNAME] = username
102 if errors
is not None:
104 step_id=
"reauth_confirm",
105 data_schema=REAUTH_SCHEMA,
107 description_placeholders={
108 CONF_USERNAME: username,
109 CONF_NAME: reauth_entry.title,
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
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_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)
dict[str, str]|None validate_input(dict[str, Any] user_input)