Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for UpCloud."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import requests.exceptions
9 import upcloud_api
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import (
13  ConfigEntry,
14  ConfigFlow,
15  ConfigFlowResult,
16  OptionsFlow,
17 )
18 from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME
19 from homeassistant.core import callback
20 
21 from .const import DEFAULT_SCAN_INTERVAL, DOMAIN
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
26 class UpCloudConfigFlow(ConfigFlow, domain=DOMAIN):
27  """UpCloud config flow."""
28 
29  VERSION = 1
30 
31  username: str
32  password: str
33 
34  async def async_step_user(
35  self, user_input: dict[str, Any] | None = None
36  ) -> ConfigFlowResult:
37  """Handle user initiated flow."""
38  if user_input is None:
39  return self._async_show_form_async_show_form(step_id="user")
40 
41  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_USERNAME])
42 
43  manager = upcloud_api.CloudManager(
44  user_input[CONF_USERNAME], user_input[CONF_PASSWORD]
45  )
46 
47  errors = {}
48  try:
49  await self.hass.async_add_executor_job(manager.authenticate)
50  except upcloud_api.UpCloudAPIError:
51  errors["base"] = "invalid_auth"
52  _LOGGER.debug("invalid_auth", exc_info=True)
53  except requests.exceptions.RequestException:
54  errors["base"] = "cannot_connect"
55  _LOGGER.debug("cannot_connect", exc_info=True)
56 
57  if errors:
58  return self._async_show_form_async_show_form(
59  step_id="user", user_input=user_input, errors=errors
60  )
61 
62  self._abort_if_unique_id_configured_abort_if_unique_id_configured(
63  updates={CONF_PASSWORD: user_input[CONF_PASSWORD]}
64  )
65  return self.async_create_entryasync_create_entryasync_create_entry(title=user_input[CONF_USERNAME], data=user_input)
66 
67  @callback
69  self,
70  step_id: str,
71  user_input: dict[str, Any] | None = None,
72  errors: dict[str, str] | None = None,
73  ) -> ConfigFlowResult:
74  """Show our form."""
75  if user_input is None:
76  user_input = {}
77  return self.async_show_formasync_show_formasync_show_form(
78  step_id=step_id,
79  data_schema=vol.Schema(
80  {
81  vol.Required(
82  CONF_USERNAME, default=user_input.get(CONF_USERNAME, "")
83  ): str,
84  vol.Required(
85  CONF_PASSWORD, default=user_input.get(CONF_PASSWORD, "")
86  ): str,
87  }
88  ),
89  errors=errors or {},
90  )
91 
92  @staticmethod
93  @callback
95  config_entry: ConfigEntry,
96  ) -> UpCloudOptionsFlow:
97  """Get options flow."""
98  return UpCloudOptionsFlow()
99 
100 
102  """UpCloud options flow."""
103 
104  async def async_step_init(
105  self, user_input: dict[str, Any] | None = None
106  ) -> ConfigFlowResult:
107  """Handle options flow."""
108 
109  if user_input is not None:
110  return self.async_create_entryasync_create_entry(title="", data=user_input)
111 
112  data_schema = vol.Schema(
113  {
114  vol.Optional(
115  CONF_SCAN_INTERVAL,
116  default=self.config_entryconfig_entryconfig_entry.options.get(CONF_SCAN_INTERVAL)
117  or DEFAULT_SCAN_INTERVAL.total_seconds(),
118  ): vol.All(vol.Coerce(int), vol.Range(min=30)),
119  }
120  )
121  return self.async_show_formasync_show_form(step_id="init", data_schema=data_schema)
ConfigFlowResult _async_show_form(self, str step_id, dict[str, Any]|None user_input=None, dict[str, str]|None errors=None)
Definition: config_flow.py:73
UpCloudOptionsFlow async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:96
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:36
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:106
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)
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)