Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for NZBGet."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import (
12  CONF_HOST,
13  CONF_NAME,
14  CONF_PASSWORD,
15  CONF_PORT,
16  CONF_SSL,
17  CONF_USERNAME,
18  CONF_VERIFY_SSL,
19 )
20 
21 from .const import DEFAULT_NAME, DEFAULT_PORT, DEFAULT_SSL, DEFAULT_VERIFY_SSL, DOMAIN
22 from .coordinator import NZBGetAPI, NZBGetAPIException
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 
27 def _validate_input(data: dict[str, Any]) -> None:
28  """Validate the user input allows us to connect.
29 
30  Data has the keys from DATA_SCHEMA with values provided by the user.
31  """
32  nzbget_api = NZBGetAPI(
33  data[CONF_HOST],
34  data.get(CONF_USERNAME),
35  data.get(CONF_PASSWORD),
36  data[CONF_SSL],
37  data[CONF_VERIFY_SSL],
38  data[CONF_PORT],
39  )
40 
41  nzbget_api.version()
42 
43 
44 class NZBGetConfigFlow(ConfigFlow, domain=DOMAIN):
45  """Handle a config flow for NZBGet."""
46 
47  VERSION = 1
48 
49  async def async_step_user(
50  self, user_input: dict[str, Any] | None = None
51  ) -> ConfigFlowResult:
52  """Handle a flow initiated by the user."""
53  errors = {}
54 
55  if user_input is not None:
56  if CONF_VERIFY_SSL not in user_input:
57  user_input[CONF_VERIFY_SSL] = DEFAULT_VERIFY_SSL
58 
59  try:
60  await self.hass.async_add_executor_job(_validate_input, user_input)
61  except NZBGetAPIException:
62  errors["base"] = "cannot_connect"
63  except Exception:
64  _LOGGER.exception("Unexpected exception")
65  return self.async_abortasync_abortasync_abort(reason="unknown")
66  else:
67  return self.async_create_entryasync_create_entryasync_create_entry(
68  title=user_input[CONF_HOST],
69  data=user_input,
70  )
71 
72  data_schema = {
73  vol.Required(CONF_HOST): str,
74  vol.Optional(CONF_NAME, default=DEFAULT_NAME): str,
75  vol.Optional(CONF_USERNAME): str,
76  vol.Optional(CONF_PASSWORD): str,
77  vol.Optional(CONF_PORT, default=DEFAULT_PORT): int,
78  vol.Optional(CONF_SSL, default=DEFAULT_SSL): bool,
79  }
80 
81  if self.show_advanced_optionsshow_advanced_options:
82  data_schema[vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL)] = (
83  bool
84  )
85 
86  return self.async_show_formasync_show_formasync_show_form(
87  step_id="user",
88  data_schema=vol.Schema(data_schema),
89  errors=errors or {},
90  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:51
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_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)
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(dict[str, Any] data)
Definition: config_flow.py:27