Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Wallbox integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 import voluptuous as vol
9 from wallbox import Wallbox
10 
11 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
13 from homeassistant.core import HomeAssistant
14 
15 from .const import CONF_STATION, DOMAIN
16 from .coordinator import InvalidAuth, async_validate_input
17 
18 COMPONENT_DOMAIN = DOMAIN
19 
20 STEP_USER_DATA_SCHEMA = vol.Schema(
21  {
22  vol.Required(CONF_STATION): str,
23  vol.Required(CONF_USERNAME): str,
24  vol.Required(CONF_PASSWORD): str,
25  }
26 )
27 
28 
29 async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, str]:
30  """Validate the user input allows to connect.
31 
32  Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
33  """
34  wallbox = Wallbox(data["username"], data["password"])
35 
36  await async_validate_input(hass, wallbox)
37 
38  # Return info that you want to store in the config entry.
39  return {"title": "Wallbox Portal"}
40 
41 
42 class WallboxConfigFlow(ConfigFlow, domain=COMPONENT_DOMAIN):
43  """Handle a config flow for Wallbox."""
44 
45  async def async_step_reauth(
46  self, entry_data: Mapping[str, Any]
47  ) -> ConfigFlowResult:
48  """Perform reauth upon an API authentication error."""
49  return await self.async_step_userasync_step_user()
50 
51  async def async_step_user(
52  self, user_input: dict[str, Any] | None = None
53  ) -> ConfigFlowResult:
54  """Handle the initial step."""
55  if user_input is None:
56  return self.async_show_formasync_show_formasync_show_form(
57  step_id="user",
58  data_schema=STEP_USER_DATA_SCHEMA,
59  )
60 
61  errors = {}
62 
63  try:
64  await self.async_set_unique_idasync_set_unique_id(user_input["station"])
65  if self.sourcesourcesource != SOURCE_REAUTH:
66  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
67  info = await validate_input(self.hass, user_input)
68  return self.async_create_entryasync_create_entryasync_create_entry(title=info["title"], data=user_input)
69  reauth_entry = self._get_reauth_entry_get_reauth_entry()
70  if user_input["station"] == reauth_entry.data[CONF_STATION]:
71  return self.async_update_reload_and_abortasync_update_reload_and_abort(reauth_entry, data=user_input)
72  errors["base"] = "reauth_invalid"
73  except ConnectionError:
74  errors["base"] = "cannot_connect"
75  except InvalidAuth:
76  errors["base"] = "invalid_auth"
77 
78  return self.async_show_formasync_show_formasync_show_form(
79  step_id="user",
80  data_schema=STEP_USER_DATA_SCHEMA,
81  errors=errors,
82  )
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:47
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_step_user(self, dict[str, Any]|None user_input=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)
str|None source(self)
tuple[dict[str, str], str] async_validate_input(HomeAssistant hass, dict[str, Any] user_input)
Definition: config_flow.py:108
dict[str, str] validate_input(HomeAssistant hass, dict[str, Any] data)
Definition: config_flow.py:29