Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Adds config flow for Sensibo integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from pysensibo.exceptions import AuthenticationError
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_API_KEY
13 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.selector import TextSelector
15 
16 from .const import DEFAULT_NAME, DOMAIN
17 from .util import NoDevicesError, NoUsernameError, async_validate_api
18 
19 DATA_SCHEMA = vol.Schema(
20  {
21  vol.Required(CONF_API_KEY): TextSelector(),
22  }
23 )
24 
25 
26 async def validate_api(
27  hass: HomeAssistant, api_key: str
28 ) -> tuple[str | None, dict[str, str]]:
29  """Validate the API key."""
30  errors: dict[str, str] = {}
31  username: str | None = None
32  try:
33  username = await async_validate_api(hass, api_key)
34  except AuthenticationError:
35  errors["base"] = "invalid_auth"
36  except ConnectionError:
37  errors["base"] = "cannot_connect"
38  except NoDevicesError:
39  errors["base"] = "no_devices"
40  except NoUsernameError:
41  errors["base"] = "no_username"
42  return (username, errors)
43 
44 
45 class SensiboConfigFlow(ConfigFlow, domain=DOMAIN):
46  """Handle a config flow for Sensibo integration."""
47 
48  VERSION = 2
49 
50  async def async_step_reauth(
51  self, entry_data: Mapping[str, Any]
52  ) -> ConfigFlowResult:
53  """Handle re-authentication with Sensibo."""
54  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
55 
57  self, user_input: dict[str, Any] | None = None
58  ) -> ConfigFlowResult:
59  """Confirm re-authentication with Sensibo."""
60  errors: dict[str, str] = {}
61 
62  if user_input:
63  api_key = user_input[CONF_API_KEY]
64  username, errors = await validate_api(self.hass, api_key)
65  if username:
66  reauth_entry = self._get_reauth_entry_get_reauth_entry()
67  if username == reauth_entry.unique_id:
68  return self.async_update_reload_and_abortasync_update_reload_and_abort(
69  reauth_entry,
70  data_updates={
71  CONF_API_KEY: api_key,
72  },
73  )
74  errors["base"] = "incorrect_api_key"
75 
76  return self.async_show_formasync_show_formasync_show_form(
77  step_id="reauth_confirm",
78  data_schema=DATA_SCHEMA,
79  errors=errors,
80  )
81 
83  self, user_input: dict[str, Any] | None = None
84  ) -> ConfigFlowResult:
85  """Reconfigure Sensibo."""
86  errors: dict[str, str] = {}
87 
88  if user_input:
89  api_key = user_input[CONF_API_KEY]
90  username, errors = await validate_api(self.hass, api_key)
91  if username:
92  reconfigure_entry = self._get_reconfigure_entry_get_reconfigure_entry()
93  if username == reconfigure_entry.unique_id:
94  return self.async_update_reload_and_abortasync_update_reload_and_abort(
95  reconfigure_entry,
96  data_updates={
97  CONF_API_KEY: api_key,
98  },
99  )
100  errors["base"] = "incorrect_api_key"
101 
102  return self.async_show_formasync_show_formasync_show_form(
103  step_id="reconfigure",
104  data_schema=DATA_SCHEMA,
105  errors=errors,
106  )
107 
108  async def async_step_user(
109  self, user_input: dict[str, Any] | None = None
110  ) -> ConfigFlowResult:
111  """Handle the initial step."""
112 
113  errors: dict[str, str] = {}
114 
115  if user_input:
116  api_key = user_input[CONF_API_KEY]
117  username, errors = await validate_api(self.hass, api_key)
118  if username:
119  await self.async_set_unique_idasync_set_unique_id(username)
120  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
121 
122  return self.async_create_entryasync_create_entryasync_create_entry(
123  title=DEFAULT_NAME,
124  data={CONF_API_KEY: api_key},
125  )
126 
127  return self.async_show_formasync_show_formasync_show_form(
128  step_id="user",
129  data_schema=DATA_SCHEMA,
130  errors=errors,
131  )
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:52
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:58
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:84
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:110
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_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)
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)
tuple[str|None, dict[str, str]] validate_api(HomeAssistant hass, str api_key)
Definition: config_flow.py:28
str async_validate_api(HomeAssistant hass, str api_key)
Definition: util.py:16