Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Radarr."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from aiohttp import ClientConnectorError
9 from aiopyarr import exceptions
10 from aiopyarr.models.host_configuration import PyArrHostConfiguration
11 from aiopyarr.radarr_client import RadarrClient
12 import voluptuous as vol
13 from yarl import URL
14 
15 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
16 from homeassistant.const import CONF_API_KEY, CONF_URL, CONF_VERIFY_SSL
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.aiohttp_client import async_get_clientsession
19 
20 from .const import DEFAULT_NAME, DEFAULT_URL, DOMAIN
21 
22 
23 class RadarrConfigFlow(ConfigFlow, domain=DOMAIN):
24  """Handle a config flow for Radarr."""
25 
26  VERSION = 1
27 
28  async def async_step_reauth(
29  self, entry_data: Mapping[str, Any]
30  ) -> ConfigFlowResult:
31  """Handle configuration by re-auth."""
32  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
33 
35  self, user_input: dict[str, str] | None = None
36  ) -> ConfigFlowResult:
37  """Confirm reauth dialog."""
38  if user_input is not None:
39  return await self.async_step_userasync_step_userasync_step_user()
40 
41  self._set_confirm_only_set_confirm_only()
42  return self.async_show_formasync_show_formasync_show_form(step_id="reauth_confirm")
43 
44  async def async_step_user(
45  self, user_input: dict[str, Any] | None = None
46  ) -> ConfigFlowResult:
47  """Handle a flow initiated by the user."""
48  errors = {}
49 
50  if user_input is not None:
51  # aiopyarr defaults to the service port if one isn't given
52  # this is counter to standard practice where http = 80
53  # and https = 443.
54  url = URL(user_input[CONF_URL])
55  user_input[CONF_URL] = f"{url.scheme}://{url.host}:{url.port}{url.path}"
56 
57  try:
58  if result := await validate_input(self.hass, user_input):
59  user_input[CONF_API_KEY] = result[1]
60  except exceptions.ArrAuthenticationException:
61  errors = {"base": "invalid_auth"}
62  except (ClientConnectorError, exceptions.ArrConnectionException):
63  errors = {"base": "cannot_connect"}
64  except exceptions.ArrWrongAppException:
65  errors = {"base": "wrong_app"}
66  except exceptions.ArrZeroConfException:
67  errors = {"base": "zeroconf_failed"}
68  except exceptions.ArrException:
69  errors = {"base": "unknown"}
70  if not errors:
71  if self.sourcesourcesourcesource == SOURCE_REAUTH:
72  return self.async_update_reload_and_abortasync_update_reload_and_abort(
73  self._get_reauth_entry_get_reauth_entry(), data=user_input
74  )
75 
76  return self.async_create_entryasync_create_entryasync_create_entry(
77  title=DEFAULT_NAME,
78  data=user_input,
79  )
80 
81  if user_input is None:
82  user_input = {}
83  if self.sourcesourcesourcesource == SOURCE_REAUTH:
84  user_input = dict(self._get_reauth_entry_get_reauth_entry().data)
85 
86  return self.async_show_formasync_show_formasync_show_form(
87  step_id="user",
88  data_schema=vol.Schema(
89  {
90  vol.Required(
91  CONF_URL, default=user_input.get(CONF_URL, DEFAULT_URL)
92  ): str,
93  vol.Optional(CONF_API_KEY): str,
94  vol.Optional(
95  CONF_VERIFY_SSL,
96  default=user_input.get(CONF_VERIFY_SSL, False),
97  ): bool,
98  }
99  ),
100  errors=errors,
101  )
102 
103 
104 async def validate_input(
105  hass: HomeAssistant, data: dict[str, Any]
106 ) -> tuple[str, str, str] | None:
107  """Validate the user input allows us to connect."""
108  host_configuration = PyArrHostConfiguration(
109  api_token=data.get(CONF_API_KEY, ""),
110  verify_ssl=data[CONF_VERIFY_SSL],
111  url=data[CONF_URL],
112  )
113  radarr = RadarrClient(
114  host_configuration=host_configuration,
115  session=async_get_clientsession(hass),
116  )
117  if CONF_API_KEY not in data:
118  return await radarr.async_try_zeroconf()
119  await radarr.async_get_system_status()
120  return None
ConfigFlowResult async_step_reauth_confirm(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:36
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:46
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:30
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)
str|None source(self)
tuple[str, str, str]|None validate_input(HomeAssistant hass, dict[str, Any] data)
Definition: config_flow.py:106
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)