Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Transmission Bittorrent Client."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import (
11  ConfigEntry,
12  ConfigFlow,
13  ConfigFlowResult,
14  OptionsFlow,
15 )
16 from homeassistant.const import (
17  CONF_HOST,
18  CONF_NAME,
19  CONF_PASSWORD,
20  CONF_PATH,
21  CONF_PORT,
22  CONF_SSL,
23  CONF_USERNAME,
24 )
25 from homeassistant.core import callback
26 
27 from . import get_api
28 from .const import (
29  CONF_LIMIT,
30  CONF_ORDER,
31  DEFAULT_LIMIT,
32  DEFAULT_NAME,
33  DEFAULT_ORDER,
34  DEFAULT_PATH,
35  DEFAULT_PORT,
36  DEFAULT_SSL,
37  DOMAIN,
38  SUPPORTED_ORDER_MODES,
39 )
40 from .errors import AuthenticationError, CannotConnect, UnknownError
41 
42 DATA_SCHEMA = vol.Schema(
43  {
44  vol.Optional(CONF_SSL, default=DEFAULT_SSL): bool,
45  vol.Required(CONF_HOST): str,
46  vol.Required(CONF_PATH, default=DEFAULT_PATH): str,
47  vol.Optional(CONF_USERNAME): str,
48  vol.Optional(CONF_PASSWORD): str,
49  vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
50  }
51 )
52 
53 
54 class TransmissionFlowHandler(ConfigFlow, domain=DOMAIN):
55  """Handle Tansmission config flow."""
56 
57  VERSION = 1
58  MINOR_VERSION = 2
59 
60  @staticmethod
61  @callback
63  config_entry: ConfigEntry,
64  ) -> TransmissionOptionsFlowHandler:
65  """Get the options flow for this handler."""
67 
68  async def async_step_user(
69  self, user_input: dict[str, Any] | None = None
70  ) -> ConfigFlowResult:
71  """Handle a flow initialized by the user."""
72  errors = {}
73 
74  if user_input is not None:
75  self._async_abort_entries_match_async_abort_entries_match(
76  {CONF_HOST: user_input[CONF_HOST], CONF_PORT: user_input[CONF_PORT]}
77  )
78  try:
79  await get_api(self.hass, user_input)
80 
81  except AuthenticationError:
82  errors[CONF_USERNAME] = "invalid_auth"
83  errors[CONF_PASSWORD] = "invalid_auth"
84  except (CannotConnect, UnknownError):
85  errors["base"] = "cannot_connect"
86 
87  if not errors:
88  return self.async_create_entryasync_create_entryasync_create_entry(
89  title=DEFAULT_NAME,
90  data=user_input,
91  )
92 
93  return self.async_show_formasync_show_formasync_show_form(
94  step_id="user",
95  data_schema=DATA_SCHEMA,
96  errors=errors,
97  )
98 
99  async def async_step_reauth(
100  self, entry_data: Mapping[str, Any]
101  ) -> ConfigFlowResult:
102  """Perform reauth upon an API authentication error."""
103  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
104 
106  self, user_input: dict[str, str] | None = None
107  ) -> ConfigFlowResult:
108  """Confirm reauth dialog."""
109  errors = {}
110  reauth_entry = self._get_reauth_entry_get_reauth_entry()
111  if user_input is not None:
112  user_input = {**reauth_entry.data, **user_input}
113  try:
114  await get_api(self.hass, user_input)
115 
116  except AuthenticationError:
117  errors[CONF_PASSWORD] = "invalid_auth"
118  except (CannotConnect, UnknownError):
119  errors["base"] = "cannot_connect"
120  else:
121  return self.async_update_reload_and_abortasync_update_reload_and_abort(reauth_entry, data=user_input)
122 
123  return self.async_show_formasync_show_formasync_show_form(
124  description_placeholders={
125  CONF_USERNAME: reauth_entry.data[CONF_USERNAME],
126  CONF_NAME: reauth_entry.title,
127  },
128  step_id="reauth_confirm",
129  data_schema=vol.Schema(
130  {
131  vol.Required(CONF_PASSWORD): str,
132  }
133  ),
134  errors=errors,
135  )
136 
137 
139  """Handle Transmission client options."""
140 
141  async def async_step_init(
142  self, user_input: dict[str, Any] | None = None
143  ) -> ConfigFlowResult:
144  """Manage the Transmission options."""
145  if user_input is not None:
146  return self.async_create_entryasync_create_entry(title="", data=user_input)
147 
148  options = {
149  vol.Optional(
150  CONF_LIMIT,
151  default=self.config_entryconfig_entryconfig_entry.options.get(CONF_LIMIT, DEFAULT_LIMIT),
152  ): vol.All(vol.Coerce(int), vol.Range(min=1, max=500)),
153  vol.Optional(
154  CONF_ORDER,
155  default=self.config_entryconfig_entryconfig_entry.options.get(CONF_ORDER, DEFAULT_ORDER),
156  ): vol.All(vol.Coerce(str), vol.In(SUPPORTED_ORDER_MODES.keys())),
157  }
158 
159  return self.async_show_formasync_show_form(step_id="init", data_schema=vol.Schema(options))
ConfigFlowResult async_step_reauth_confirm(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:107
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:70
TransmissionOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:64
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:101
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:143
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)
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)
None config_entry(self, ConfigEntry value)
_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)
transmission_rpc.Client get_api(HomeAssistant hass, dict[str, Any] entry)
Definition: __init__.py:279