1 """Config flow for Sonarr."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
9 from aiopyarr
import ArrAuthenticationException, ArrException
10 from aiopyarr.models.host_configuration
import PyArrHostConfiguration
11 from aiopyarr.sonarr_client
import SonarrClient
12 import voluptuous
as vol
28 CONF_WANTED_MAX_ITEMS,
29 DEFAULT_UPCOMING_DAYS,
31 DEFAULT_WANTED_MAX_ITEMS,
35 _LOGGER = logging.getLogger(__name__)
39 """Validate the user input allows us to connect.
41 Data has the keys from DATA_SCHEMA with values provided by the user.
43 host_configuration = PyArrHostConfiguration(
44 api_token=data[CONF_API_KEY],
46 verify_ssl=data[CONF_VERIFY_SSL],
49 sonarr = SonarrClient(
50 host_configuration=host_configuration,
54 await sonarr.async_get_system_status()
58 """Handle a config flow for Sonarr."""
65 """Get the options flow for this handler."""
69 self, entry_data: Mapping[str, Any]
70 ) -> ConfigFlowResult:
71 """Handle configuration by re-auth."""
75 self, user_input: dict[str, Any] |
None =
None
76 ) -> ConfigFlowResult:
77 """Confirm reauth dialog."""
78 if user_input
is None:
80 step_id=
"reauth_confirm",
81 description_placeholders={
90 self, user_input: dict[str, Any] |
None =
None
91 ) -> ConfigFlowResult:
92 """Handle a flow initiated by the user."""
95 if user_input
is not None:
99 if CONF_URL
in user_input:
100 url = yarl.URL(user_input[CONF_URL])
101 user_input[CONF_URL] = f
"{url.scheme}://{url.host}:{url.port}{url.path}"
106 if CONF_VERIFY_SSL
not in user_input:
107 user_input[CONF_VERIFY_SSL] = DEFAULT_VERIFY_SSL
111 except ArrAuthenticationException:
112 errors = {
"base":
"invalid_auth"}
114 errors = {
"base":
"cannot_connect"}
116 _LOGGER.exception(
"Unexpected exception")
124 parsed = yarl.URL(user_input[CONF_URL])
127 title=parsed.host
or "Sonarr", data=user_input
133 data_schema=vol.Schema(data_schema),
138 """Get the data schema to display user form."""
140 return {vol.Required(CONF_API_KEY): str}
142 data_schema: dict[vol.Marker, type] = {
143 vol.Required(CONF_URL): str,
144 vol.Required(CONF_API_KEY): str,
148 data_schema[vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL)] = (
156 """Handle Sonarr client options."""
159 self, user_input: dict[str, int] |
None =
None
160 ) -> ConfigFlowResult:
161 """Manage Sonarr options."""
162 if user_input
is not None:
169 CONF_UPCOMING_DAYS, DEFAULT_UPCOMING_DAYS
173 CONF_WANTED_MAX_ITEMS,
175 CONF_WANTED_MAX_ITEMS, DEFAULT_WANTED_MAX_ITEMS
180 return self.
async_show_formasync_show_form(step_id=
"init", data_schema=vol.Schema(options))
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)
dict[vol.Marker, type] _get_user_data_schema(self)
SonarrOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_init(self, dict[str, int]|None user_input=None)
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)
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)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
bool show_advanced_options(self)
_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] 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)