Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Lidarr."""
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.lidarr_client import LidarrClient
11 import voluptuous as vol
12 
13 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_API_KEY, CONF_URL, CONF_VERIFY_SSL
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.aiohttp_client import async_get_clientsession
17 
18 from .const import DEFAULT_NAME, DOMAIN
19 
20 
21 class LidarrConfigFlow(ConfigFlow, domain=DOMAIN):
22  """Handle a config flow for Lidarr."""
23 
24  VERSION = 1
25 
26  async def async_step_reauth(
27  self, entry_data: Mapping[str, Any]
28  ) -> ConfigFlowResult:
29  """Handle configuration by re-auth."""
30  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
31 
33  self, user_input: dict[str, str] | None = None
34  ) -> ConfigFlowResult:
35  """Confirm reauth dialog."""
36  if user_input is not None:
37  return await self.async_step_userasync_step_userasync_step_user()
38 
39  self._set_confirm_only_set_confirm_only()
40  return self.async_show_formasync_show_formasync_show_form(step_id="reauth_confirm")
41 
42  async def async_step_user(
43  self, user_input: dict[str, Any] | None = None
44  ) -> ConfigFlowResult:
45  """Handle a flow initiated by the user."""
46  errors = {}
47 
48  if user_input is not None:
49  try:
50  if result := await validate_input(self.hass, user_input):
51  user_input[CONF_API_KEY] = result[1]
52  except exceptions.ArrAuthenticationException:
53  errors = {"base": "invalid_auth"}
54  except (ClientConnectorError, exceptions.ArrConnectionException):
55  errors = {"base": "cannot_connect"}
56  except exceptions.ArrWrongAppException:
57  errors = {"base": "wrong_app"}
58  except exceptions.ArrZeroConfException:
59  errors = {"base": "zeroconf_failed"}
60  except exceptions.ArrException:
61  errors = {"base": "unknown"}
62  if not errors:
63  if self.sourcesourcesourcesource == SOURCE_REAUTH:
64  return self.async_update_reload_and_abortasync_update_reload_and_abort(
65  self._get_reauth_entry_get_reauth_entry(), data=user_input
66  )
67 
68  return self.async_create_entryasync_create_entryasync_create_entry(title=DEFAULT_NAME, data=user_input)
69 
70  if user_input is None:
71  user_input = {}
72  if self.sourcesourcesourcesource == SOURCE_REAUTH:
73  user_input = dict(self._get_reauth_entry_get_reauth_entry().data)
74 
75  return self.async_show_formasync_show_formasync_show_form(
76  step_id="user",
77  data_schema=vol.Schema(
78  {
79  vol.Required(CONF_URL, default=user_input.get(CONF_URL, "")): str,
80  vol.Optional(CONF_API_KEY): str,
81  vol.Optional(
82  CONF_VERIFY_SSL,
83  default=user_input.get(CONF_VERIFY_SSL, False),
84  ): bool,
85  }
86  ),
87  errors=errors,
88  )
89 
90 
91 async def validate_input(
92  hass: HomeAssistant, data: dict[str, Any]
93 ) -> tuple[str, str, str] | None:
94  """Validate the user input allows us to connect.
95 
96  Data has the keys from DATA_SCHEMA with values provided by the user.
97  """
98  lidarr = LidarrClient(
99  api_token=data.get(CONF_API_KEY, ""),
100  url=data[CONF_URL],
101  session=async_get_clientsession(hass),
102  verify_ssl=data[CONF_VERIFY_SSL],
103  )
104  if CONF_API_KEY not in data:
105  return await lidarr.async_try_zeroconf()
106  await lidarr.async_get_system_status()
107  return None
ConfigFlowResult async_step_reauth_confirm(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:34
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:44
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:28
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:93
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)