Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for qBittorrent."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from qbittorrentapi import APIConnectionError, Forbidden403Error, LoginFailed
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME, CONF_VERIFY_SSL
13 
14 from .const import DEFAULT_NAME, DEFAULT_URL, DOMAIN
15 from .helpers import setup_client
16 
17 _LOGGER = logging.getLogger(__name__)
18 
19 USER_DATA_SCHEMA = vol.Schema(
20  {
21  vol.Required(CONF_URL, default=DEFAULT_URL): str,
22  vol.Required(CONF_USERNAME): str,
23  vol.Required(CONF_PASSWORD): str,
24  vol.Optional(CONF_VERIFY_SSL, default=True): bool,
25  }
26 )
27 
28 
29 class QbittorrentConfigFlow(ConfigFlow, domain=DOMAIN):
30  """Config flow for the qBittorrent integration."""
31 
32  async def async_step_user(
33  self, user_input: dict[str, Any] | None = None
34  ) -> ConfigFlowResult:
35  """Handle a user-initiated config flow."""
36  errors = {}
37 
38  if user_input is not None:
39  self._async_abort_entries_match_async_abort_entries_match({CONF_URL: user_input[CONF_URL]})
40  try:
41  await self.hass.async_add_executor_job(
42  setup_client,
43  user_input[CONF_URL],
44  user_input[CONF_USERNAME],
45  user_input[CONF_PASSWORD],
46  user_input[CONF_VERIFY_SSL],
47  )
48  except (LoginFailed, Forbidden403Error):
49  errors = {"base": "invalid_auth"}
50  except APIConnectionError:
51  errors = {"base": "cannot_connect"}
52  else:
53  return self.async_create_entryasync_create_entryasync_create_entry(title=DEFAULT_NAME, data=user_input)
54 
55  schema = self.add_suggested_values_to_schemaadd_suggested_values_to_schema(USER_DATA_SCHEMA, user_input)
56  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=schema, errors=errors)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:34
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)
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)