1 """Config flow for FYTA integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
9 from fyta_cli.fyta_connector
import FytaConnector
10 from fyta_cli.fyta_exceptions
import (
11 FytaAuthentificationError,
15 from fyta_cli.fyta_models
import Credentials
16 import voluptuous
as vol
26 from .const
import CONF_EXPIRATION, DOMAIN
28 _LOGGER = logging.getLogger(__name__)
31 DATA_SCHEMA = vol.Schema(
35 type=TextSelectorType.TEXT,
36 autocomplete=
"username",
41 type=TextSelectorType.PASSWORD,
42 autocomplete=
"current-password",
50 """Handle a config flow for Fyta."""
52 credentials: Credentials
56 async
def async_auth(self, user_input: Mapping[str, Any]) -> dict[str, str]:
57 """Reusable Auth Helper."""
58 fyta = FytaConnector(user_input[CONF_USERNAME], user_input[CONF_PASSWORD])
62 except FytaConnectionError:
63 return {
"base":
"cannot_connect"}
64 except FytaAuthentificationError:
65 return {
"base":
"invalid_auth"}
66 except FytaPasswordError:
67 return {
"base":
"invalid_auth", CONF_PASSWORD:
"password_error"}
68 except Exception
as e:
70 return {
"base":
"unknown"}
72 await fyta.client.close()
77 self, user_input: dict[str, Any] |
None =
None
78 ) -> ConfigFlowResult:
79 """Handle the initial step."""
84 if not (errors := await self.
async_authasync_auth(user_input)):
86 CONF_ACCESS_TOKEN: self.
credentialscredentials.access_token,
87 CONF_EXPIRATION: self.
credentialscredentials.expiration.isoformat(),
90 title=user_input[CONF_USERNAME], data=user_input
94 step_id=
"user", data_schema=DATA_SCHEMA, errors=errors
98 self, entry_data: Mapping[str, Any]
99 ) -> ConfigFlowResult:
100 """Handle flow upon an API authentication error."""
104 self, user_input: dict[str, Any] |
None =
None
105 ) -> ConfigFlowResult:
106 """Handle reauthorization flow."""
110 if user_input
and not (errors := await self.
async_authasync_auth(user_input)):
112 CONF_ACCESS_TOKEN: self.
credentialscredentials.access_token,
113 CONF_EXPIRATION: self.
credentialscredentials.expiration.isoformat(),
117 data_updates=user_input,
122 {CONF_USERNAME: reauth_entry.data[CONF_USERNAME], **(user_input
or {})},
125 step_id=
"reauth_confirm",
126 data_schema=data_schema,
ConfigFlowResult async_step_user(self, dict[str, Any]|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)
dict[str, str] async_auth(self, Mapping[str, Any] user_input)
ConfigEntry _get_reauth_entry(self)
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)