Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config Flow for OSO Energy."""
2 
3 from collections.abc import Mapping
4 import logging
5 from typing import Any
6 
7 from apyosoenergyapi import OSOEnergy
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_API_KEY
12 from homeassistant.helpers import aiohttp_client
13 
14 from .const import DOMAIN
15 
16 _LOGGER = logging.getLogger(__name__)
17 _SCHEMA_STEP_USER = vol.Schema({vol.Required(CONF_API_KEY): str})
18 
19 
20 class OSOEnergyFlowHandler(ConfigFlow, domain=DOMAIN):
21  """Handle a OSO Energy config flow."""
22 
23  VERSION = 1
24 
25  async def async_step_user(self, user_input=None) -> ConfigFlowResult:
26  """Handle a flow initialized by the user."""
27  errors = {}
28 
29  if user_input is not None:
30  # Verify Subscription key
31  if user_email := await self.get_user_emailget_user_email(user_input[CONF_API_KEY]):
32  await self.async_set_unique_idasync_set_unique_id(user_email)
33 
34  if self.sourcesourcesourcesource == SOURCE_REAUTH:
35  return self.async_update_reload_and_abortasync_update_reload_and_abort(
36  self._get_reauth_entry_get_reauth_entry(), title=user_email, data=user_input
37  )
38 
39  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
40  return self.async_create_entryasync_create_entryasync_create_entry(title=user_email, data=user_input)
41 
42  errors["base"] = "invalid_auth"
43 
44  return self.async_show_formasync_show_formasync_show_form(
45  step_id="user",
46  data_schema=_SCHEMA_STEP_USER,
47  errors=errors,
48  )
49 
50  async def get_user_email(self, subscription_key: str) -> str | None:
51  """Return the user email for the provided subscription key."""
52  try:
53  websession = aiohttp_client.async_get_clientsession(self.hass)
54  client = OSOEnergy(subscription_key, websession)
55  return await client.get_user_email()
56  except Exception:
57  _LOGGER.exception("Unknown error occurred")
58  return None
59 
60  async def async_step_reauth(
61  self, entry_data: Mapping[str, Any]
62  ) -> ConfigFlowResult:
63  """Re Authenticate a user."""
64  return self.async_show_formasync_show_formasync_show_form(
65  step_id="user",
66  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
67  _SCHEMA_STEP_USER, self._get_reauth_entry_get_reauth_entry().data
68  ),
69  )
ConfigFlowResult async_step_user(self, user_input=None)
Definition: config_flow.py:25
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
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_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)