Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Glances."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from glances_api.exceptions import (
9  GlancesApiAuthorizationError,
10  GlancesApiConnectionError,
11 )
12 import voluptuous as vol
13 
14 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
15 from homeassistant.const import (
16  CONF_HOST,
17  CONF_PASSWORD,
18  CONF_PORT,
19  CONF_SSL,
20  CONF_USERNAME,
21  CONF_VERIFY_SSL,
22 )
23 
24 from . import ServerVersionMismatch, get_api
25 from .const import DEFAULT_HOST, DEFAULT_PORT, DOMAIN
26 
27 DATA_SCHEMA = vol.Schema(
28  {
29  vol.Required(CONF_HOST, default=DEFAULT_HOST): str,
30  vol.Optional(CONF_USERNAME): str,
31  vol.Optional(CONF_PASSWORD): str,
32  vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
33  vol.Optional(CONF_SSL, default=False): bool,
34  vol.Optional(CONF_VERIFY_SSL, default=False): bool,
35  }
36 )
37 
38 
39 class GlancesFlowHandler(ConfigFlow, domain=DOMAIN):
40  """Handle a Glances config flow."""
41 
42  VERSION = 1
43 
44  async def async_step_reauth(
45  self, entry_data: Mapping[str, Any]
46  ) -> ConfigFlowResult:
47  """Perform reauth upon an API authentication error."""
48  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
49 
51  self, user_input: dict[str, str] | None = None
52  ) -> ConfigFlowResult:
53  """Confirm reauth dialog."""
54  errors = {}
55 
56  reauth_entry = self._get_reauth_entry_get_reauth_entry()
57  if user_input is not None:
58  user_input = {**reauth_entry.data, **user_input}
59  try:
60  await get_api(self.hass, user_input)
61  except GlancesApiAuthorizationError:
62  errors["base"] = "invalid_auth"
63  except GlancesApiConnectionError:
64  errors["base"] = "cannot_connect"
65  else:
66  self.hass.config_entries.async_update_entry(
67  reauth_entry, data=user_input
68  )
69  await self.hass.config_entries.async_reload(reauth_entry.entry_id)
70  return self.async_abortasync_abortasync_abort(reason="reauth_successful")
71 
72  return self.async_show_formasync_show_formasync_show_form(
73  description_placeholders={CONF_USERNAME: reauth_entry.data[CONF_USERNAME]},
74  step_id="reauth_confirm",
75  data_schema=vol.Schema(
76  {
77  vol.Required(CONF_PASSWORD): str,
78  }
79  ),
80  errors=errors,
81  )
82 
83  async def async_step_user(
84  self, user_input: dict[str, Any] | None = None
85  ) -> ConfigFlowResult:
86  """Handle the initial step."""
87  errors = {}
88  if user_input is not None:
89  self._async_abort_entries_match_async_abort_entries_match(
90  {CONF_HOST: user_input[CONF_HOST], CONF_PORT: user_input[CONF_PORT]}
91  )
92  try:
93  await get_api(self.hass, user_input)
94  except GlancesApiAuthorizationError:
95  errors["base"] = "invalid_auth"
96  except (GlancesApiConnectionError, ServerVersionMismatch):
97  errors["base"] = "cannot_connect"
98  else:
99  return self.async_create_entryasync_create_entryasync_create_entry(
100  title=f"{user_input[CONF_HOST]}:{user_input[CONF_PORT]}",
101  data=user_input,
102  )
103 
104  return self.async_show_formasync_show_formasync_show_form(
105  step_id="user", data_schema=DATA_SCHEMA, errors=errors
106  )
ConfigFlowResult async_step_reauth_confirm(self, dict[str, str]|None user_input=None)
Definition: config_flow.py:52
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:46
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:85
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)
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)
_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)
Glances get_api(HomeAssistant hass, dict[str, Any] entry_data)
Definition: __init__.py:69