Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for the Hydrawise integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable, Mapping
6 from typing import Any
7 
8 from aiohttp import ClientError
9 from pydrawise import auth, client
10 from pydrawise.exceptions import NotAuthorizedError
11 import voluptuous as vol
12 
13 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
14 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
15 
16 from .const import DOMAIN, LOGGER
17 
18 
19 class HydrawiseConfigFlow(ConfigFlow, domain=DOMAIN):
20  """Handle a config flow for Hydrawise."""
21 
22  VERSION = 1
23 
25  self,
26  username: str,
27  password: str,
28  *,
29  on_failure: Callable[[str], ConfigFlowResult],
30  ) -> ConfigFlowResult:
31  """Create the config entry."""
32 
33  # Verify that the provided credentials work."""
34  api = client.Hydrawise(auth.Auth(username, password))
35  try:
36  # Don't fetch zones because we don't need them yet.
37  user = await api.get_user(fetch_zones=False)
38  except NotAuthorizedError:
39  return on_failure("invalid_auth")
40  except TimeoutError:
41  return on_failure("timeout_connect")
42  except ClientError as ex:
43  LOGGER.error("Unable to connect to Hydrawise cloud service: %s", ex)
44  return on_failure("cannot_connect")
45 
46  await self.async_set_unique_idasync_set_unique_id(f"hydrawise-{user.customer_id}")
47 
48  if self.sourcesourcesource != SOURCE_REAUTH:
49  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
50  return self.async_create_entryasync_create_entryasync_create_entry(
51  title="Hydrawise",
52  data={CONF_USERNAME: username, CONF_PASSWORD: password},
53  )
54 
55  return self.async_update_reload_and_abortasync_update_reload_and_abort(
56  self._get_reauth_entry_get_reauth_entry(),
57  data_updates={CONF_USERNAME: username, CONF_PASSWORD: password},
58  )
59 
60  async def async_step_user(
61  self, user_input: dict[str, Any] | None = None
62  ) -> ConfigFlowResult:
63  """Handle the initial setup."""
64  if user_input is not None:
65  username = user_input[CONF_USERNAME]
66  password = user_input[CONF_PASSWORD]
67 
68  return await self._create_or_update_entry_create_or_update_entry(
69  username=username, password=password, on_failure=self._show_form_show_form
70  )
71  return self._show_form_show_form()
72 
73  def _show_form(self, error_type: str | None = None) -> ConfigFlowResult:
74  errors = {}
75  if error_type is not None:
76  errors["base"] = error_type
77  return self.async_show_formasync_show_formasync_show_form(
78  step_id="user",
79  data_schema=vol.Schema(
80  {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
81  ),
82  errors=errors,
83  )
84 
85  async def async_step_reauth(
86  self, entry_data: Mapping[str, Any]
87  ) -> ConfigFlowResult:
88  """Perform reauth after updating config to username/password."""
89  return await self.async_step_userasync_step_userasync_step_user()
ConfigFlowResult _create_or_update_entry(self, str username, str password, *Callable[[str], ConfigFlowResult] on_failure)
Definition: config_flow.py:30
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:87
ConfigFlowResult _show_form(self, str|None error_type=None)
Definition: config_flow.py:73
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:62
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)