Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure qnap component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from qnapstats import QNAPStats
9 from requests.exceptions import ConnectTimeout
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import (
14  CONF_HOST,
15  CONF_PASSWORD,
16  CONF_PORT,
17  CONF_SSL,
18  CONF_USERNAME,
19  CONF_VERIFY_SSL,
20 )
21 from homeassistant.helpers import config_validation as cv
22 
23 from .const import (
24  DEFAULT_PORT,
25  DEFAULT_SSL,
26  DEFAULT_TIMEOUT,
27  DEFAULT_VERIFY_SSL,
28  DOMAIN,
29 )
30 
31 DATA_SCHEMA = vol.Schema(
32  {
33  vol.Required(CONF_HOST): cv.string,
34  vol.Required(CONF_USERNAME): cv.string,
35  vol.Required(CONF_PASSWORD): cv.string,
36  vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
37  vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
38  vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
39  }
40 )
41 
42 _LOGGER = logging.getLogger(__name__)
43 
44 
45 class QnapConfigFlow(ConfigFlow, domain=DOMAIN):
46  """Qnap configuration flow."""
47 
48  VERSION = 1
49 
50  async def async_step_user(
51  self,
52  user_input: dict[str, Any] | None = None,
53  ) -> ConfigFlowResult:
54  """Handle a flow initialized by the user."""
55  errors = {}
56  if user_input is not None:
57  host = user_input[CONF_HOST]
58  protocol = "https" if user_input[CONF_SSL] else "http"
59  api = QNAPStats(
60  host=f"{protocol}://{host}",
61  port=user_input[CONF_PORT],
62  username=user_input[CONF_USERNAME],
63  password=user_input[CONF_PASSWORD],
64  verify_ssl=user_input[CONF_VERIFY_SSL],
65  timeout=DEFAULT_TIMEOUT,
66  )
67  try:
68  stats = await self.hass.async_add_executor_job(api.get_system_stats)
69  except ConnectTimeout:
70  errors["base"] = "cannot_connect"
71  except TypeError:
72  errors["base"] = "invalid_auth"
73  except Exception as error: # noqa: BLE001
74  _LOGGER.error(error)
75  errors["base"] = "unknown"
76  else:
77  unique_id = stats["system"]["serial_number"]
78  await self.async_set_unique_idasync_set_unique_id(unique_id)
79  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
80  title = stats["system"]["name"]
81  return self.async_create_entryasync_create_entryasync_create_entry(title=title, data=user_input)
82 
83  return self.async_show_formasync_show_formasync_show_form(
84  step_id="user",
85  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(DATA_SCHEMA, user_input),
86  errors=errors,
87  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:53
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
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_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)