1 """Config flow for pyLoad integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
9 from aiohttp
import CookieJar
10 from pyloadapi.api
import PyLoadAPI
11 from pyloadapi.exceptions
import CannotConnect, InvalidAuth, ParserError
12 import voluptuous
as vol
33 from .const
import DEFAULT_HOST, DEFAULT_NAME, DEFAULT_PORT, DOMAIN
35 _LOGGER = logging.getLogger(__name__)
37 STEP_USER_DATA_SCHEMA = vol.Schema(
39 vol.Required(CONF_HOST): str,
40 vol.Required(CONF_PORT, default=DEFAULT_PORT): cv.port,
41 vol.Required(CONF_SSL, default=
False): cv.boolean,
42 vol.Required(CONF_VERIFY_SSL, default=
True): bool,
45 type=TextSelectorType.TEXT,
46 autocomplete=
"username",
51 type=TextSelectorType.PASSWORD,
52 autocomplete=
"current-password",
58 REAUTH_SCHEMA = vol.Schema(
62 type=TextSelectorType.TEXT,
63 autocomplete=
"username",
68 type=TextSelectorType.PASSWORD,
69 autocomplete=
"current-password",
76 async
def validate_input(hass: HomeAssistant, user_input: dict[str, Any]) ->
None:
77 """Validate the user input and try to connect to PyLoad."""
81 user_input[CONF_VERIFY_SSL],
82 cookie_jar=CookieJar(unsafe=
True),
86 f
"{"https
" if user_input[CONF_SSL] else "http
"}://"
87 f
"{user_input[CONF_HOST]}:{user_input[CONF_PORT]}/"
92 username=user_input[CONF_USERNAME],
93 password=user_input[CONF_PASSWORD],
100 """Handle a config flow for pyLoad."""
105 self, user_input: dict[str, Any] |
None =
None
106 ) -> ConfigFlowResult:
107 """Handle the initial step."""
108 errors: dict[str, str] = {}
109 if user_input
is not None:
111 {CONF_HOST: user_input[CONF_HOST], CONF_PORT: user_input[CONF_PORT]}
115 except (CannotConnect, ParserError):
116 errors[
"base"] =
"cannot_connect"
118 errors[
"base"] =
"invalid_auth"
120 _LOGGER.exception(
"Unexpected exception")
121 errors[
"base"] =
"unknown"
123 title = user_input.pop(CONF_NAME, DEFAULT_NAME)
129 STEP_USER_DATA_SCHEMA, user_input
135 """Import config from yaml."""
138 CONF_NAME: import_data.get(CONF_NAME),
139 CONF_HOST: import_data.get(CONF_HOST, DEFAULT_HOST),
140 CONF_PASSWORD: import_data.get(CONF_PASSWORD,
""),
141 CONF_PORT: import_data.get(CONF_PORT, DEFAULT_PORT),
142 CONF_SSL: import_data.get(CONF_SSL,
False),
143 CONF_USERNAME: import_data.get(CONF_USERNAME,
""),
144 CONF_VERIFY_SSL:
False,
149 if errors := result.get(
"errors"):
154 self, entry_data: Mapping[str, Any]
155 ) -> ConfigFlowResult:
156 """Perform reauth upon an API authentication error."""
160 self, user_input: dict[str, Any] |
None =
None
161 ) -> ConfigFlowResult:
162 """Dialog that informs the user that reauth is required."""
166 if user_input
is not None:
167 new_input = reauth_entry.data | user_input
170 except (CannotConnect, ParserError):
171 errors[
"base"] =
"cannot_connect"
173 errors[
"base"] =
"invalid_auth"
175 _LOGGER.exception(
"Unexpected exception")
176 errors[
"base"] =
"unknown"
181 step_id=
"reauth_confirm",
185 CONF_USERNAME: user_input[CONF_USERNAME]
186 if user_input
is not None
187 else reauth_entry.data[CONF_USERNAME]
190 description_placeholders={CONF_NAME: reauth_entry.data[CONF_USERNAME]},
195 self, user_input: dict[str, Any] |
None =
None
196 ) -> ConfigFlowResult:
197 """Handle the reconfiguration flow."""
201 if user_input
is not None:
204 except (CannotConnect, ParserError):
205 errors[
"base"] =
"cannot_connect"
207 errors[
"base"] =
"invalid_auth"
209 _LOGGER.exception(
"Unexpected exception")
210 errors[
"base"] =
"unknown"
215 reload_even_if_entry_is_unchanged=
False,
219 step_id=
"reconfigure",
221 STEP_USER_DATA_SCHEMA,
222 user_input
or reconfig_entry.data,
224 description_placeholders={CONF_NAME: reconfig_entry.data[CONF_USERNAME]},
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_import(self, dict[str, Any] import_data)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
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)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
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)
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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
None validate_input(HomeAssistant hass, dict[str, Any] user_input)
aiohttp.ClientSession async_create_clientsession()