Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Discovergy integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 import logging
7 from typing import Any
8 
9 from pydiscovergy import Discovergy
10 from pydiscovergy.authentication import BasicAuth
11 import pydiscovergy.error as discovergyError
12 import voluptuous as vol
13 
14 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
15 from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
16 from homeassistant.helpers.httpx_client import get_async_client
18  TextSelector,
19  TextSelectorConfig,
20  TextSelectorType,
21 )
22 
23 from .const import DOMAIN
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 
28 CONFIG_SCHEMA = vol.Schema(
29  {
30  vol.Required(
31  CONF_EMAIL,
32  ): TextSelector(
34  type=TextSelectorType.EMAIL,
35  autocomplete="email",
36  )
37  ),
38  vol.Required(
39  CONF_PASSWORD,
40  ): TextSelector(
42  type=TextSelectorType.PASSWORD,
43  autocomplete="current-password",
44  )
45  ),
46  }
47 )
48 
49 
50 class DiscovergyConfigFlow(ConfigFlow, domain=DOMAIN):
51  """Handle a config flow for Discovergy."""
52 
53  VERSION = 1
54 
55  async def async_step_reauth(
56  self, entry_data: Mapping[str, Any]
57  ) -> ConfigFlowResult:
58  """Handle the initial step."""
59  return await self.async_step_userasync_step_userasync_step_user()
60 
61  async def async_step_user(
62  self, user_input: Mapping[str, Any] | None = None
63  ) -> ConfigFlowResult:
64  """Validate user input and create config entry."""
65  errors = {}
66 
67  if user_input:
68  try:
69  await Discovergy(
70  email=user_input[CONF_EMAIL],
71  password=user_input[CONF_PASSWORD],
72  httpx_client=get_async_client(self.hass),
73  authentication=BasicAuth(),
74  ).meters()
75  except (discovergyError.HTTPError, discovergyError.DiscovergyClientError):
76  errors["base"] = "cannot_connect"
77  except discovergyError.InvalidLogin:
78  errors["base"] = "invalid_auth"
79  except Exception:
80  _LOGGER.exception("Unexpected error occurred while getting meters")
81  errors["base"] = "unknown"
82  else:
83  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_EMAIL].lower())
84 
85  if self.sourcesourcesourcesource == SOURCE_REAUTH:
86  self._abort_if_unique_id_mismatch_abort_if_unique_id_mismatch(reason="account_mismatch")
87  return self.async_update_reload_and_abortasync_update_reload_and_abort(
88  entry=self._get_reauth_entry_get_reauth_entry(),
89  data_updates={
90  CONF_PASSWORD: user_input[CONF_PASSWORD],
91  },
92  )
93 
94  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
95 
96  return self.async_create_entryasync_create_entryasync_create_entry(
97  title=user_input[CONF_EMAIL], data=user_input
98  )
99 
100  return self.async_show_formasync_show_formasync_show_form(
101  step_id="user",
102  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
103  CONFIG_SCHEMA,
104  self._get_reauth_entry_get_reauth_entry().data
105  if self.sourcesourcesourcesource == SOURCE_REAUTH
106  else user_input,
107  ),
108  errors=errors,
109  )
ConfigFlowResult async_step_user(self, Mapping[str, Any]|None user_input=None)
Definition: config_flow.py:63
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:57
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)
None _abort_if_unique_id_mismatch(self, *str reason="unique_id_mismatch", Mapping[str, str]|None description_placeholders=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)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)
httpx.AsyncClient get_async_client(HomeAssistant hass, bool verify_ssl=True)
Definition: httpx_client.py:41