Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the OVO Energy integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 import aiohttp
9 from ovoenergy import OVOEnergy
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
14 from homeassistant.helpers.aiohttp_client import async_get_clientsession
15 
16 from .const import CONF_ACCOUNT, DOMAIN
17 
18 REAUTH_SCHEMA = vol.Schema({vol.Required(CONF_PASSWORD): str})
19 USER_SCHEMA = vol.Schema(
20  {
21  vol.Required(CONF_USERNAME): str,
22  vol.Required(CONF_PASSWORD): str,
23  vol.Optional(CONF_ACCOUNT): str,
24  }
25 )
26 
27 
28 class OVOEnergyFlowHandler(ConfigFlow, domain=DOMAIN):
29  """Handle a OVO Energy config flow."""
30 
31  VERSION = 1
32 
33  def __init__(self) -> None:
34  """Initialize the flow."""
35  self.usernameusername = None
36  self.accountaccount = None
37 
38  async def async_step_user(
39  self,
40  user_input: Mapping[str, Any] | None = None,
41  ) -> ConfigFlowResult:
42  """Handle a flow initiated by the user."""
43  errors = {}
44  if user_input is not None:
45  client = OVOEnergy(
46  client_session=async_get_clientsession(self.hass),
47  )
48 
49  if (custom_account := user_input.get(CONF_ACCOUNT)) is not None:
50  client.custom_account_id = custom_account
51 
52  try:
53  authenticated = await client.authenticate(
54  user_input[CONF_USERNAME],
55  user_input[CONF_PASSWORD],
56  )
57  await client.bootstrap_accounts()
58  except aiohttp.ClientError:
59  errors["base"] = "cannot_connect"
60  else:
61  if authenticated:
62  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_USERNAME])
63  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
64 
65  return self.async_create_entryasync_create_entryasync_create_entry(
66  title=client.username,
67  data={
68  CONF_USERNAME: user_input[CONF_USERNAME],
69  CONF_PASSWORD: user_input[CONF_PASSWORD],
70  CONF_ACCOUNT: client.account_id,
71  },
72  )
73 
74  errors["base"] = "invalid_auth"
75 
76  return self.async_show_formasync_show_formasync_show_form(
77  step_id="user", data_schema=USER_SCHEMA, errors=errors
78  )
79 
80  async def async_step_reauth(
81  self,
82  entry_data: Mapping[str, Any],
83  ) -> ConfigFlowResult:
84  """Handle configuration by re-auth."""
85  self.usernameusername = entry_data.get(CONF_USERNAME)
86  self.accountaccount = entry_data.get(CONF_ACCOUNT)
87 
88  if self.usernameusername:
89  # If we have a username, use it as flow title
90  self.context["title_placeholders"] = {CONF_USERNAME: self.usernameusername}
91 
92  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
93 
95  self,
96  user_input: Mapping[str, Any] | None = None,
97  ) -> ConfigFlowResult:
98  """Handle configuration by re-auth."""
99  errors = {}
100 
101  if user_input is not None:
102  client = OVOEnergy(
103  client_session=async_get_clientsession(self.hass),
104  )
105 
106  if self.accountaccount is not None:
107  client.custom_account_id = self.accountaccount
108 
109  try:
110  authenticated = await client.authenticate(
111  self.usernameusername,
112  user_input[CONF_PASSWORD],
113  )
114  except aiohttp.ClientError:
115  errors["base"] = "connection_error"
116  else:
117  if authenticated:
118  return self.async_update_reload_and_abortasync_update_reload_and_abort(
119  self._get_reauth_entry_get_reauth_entry(),
120  data_updates={CONF_PASSWORD: user_input[CONF_PASSWORD]},
121  )
122 
123  errors["base"] = "authorization_error"
124 
125  return self.async_show_formasync_show_formasync_show_form(
126  step_id="reauth_confirm", data_schema=REAUTH_SCHEMA, errors=errors
127  )
ConfigFlowResult async_step_reauth_confirm(self, Mapping[str, Any]|None user_input=None)
Definition: config_flow.py:97
ConfigFlowResult async_step_user(self, Mapping[str, Any]|None user_input=None)
Definition: config_flow.py:41
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:83
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)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)