Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for EnergyFlip integration."""
2 
3 import logging
4 from typing import Any
5 
6 from energyflip import EnergyFlip, EnergyFlipConnectionException, EnergyFlipException
7 import voluptuous as vol
8 
9 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
10 from homeassistant.const import CONF_ID, CONF_PASSWORD, CONF_USERNAME
11 from homeassistant.data_entry_flow import AbortFlow
12 
13 from .const import DOMAIN
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 DATA_SCHEMA = vol.Schema(
18  {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
19 )
20 
21 
22 class EnergyFlipConfigFlow(ConfigFlow, domain=DOMAIN):
23  """Handle a config flow for EnergyFlip."""
24 
25  VERSION = 1
26 
27  async def async_step_user(
28  self, user_input: dict[str, Any] | None = None
29  ) -> ConfigFlowResult:
30  """Handle a flow initiated by the user."""
31  if user_input is None:
32  return await self._show_setup_form_show_setup_form(user_input)
33 
34  errors = {}
35 
36  try:
37  user_id = await self._validate_input_validate_input(user_input)
38  except EnergyFlipConnectionException as exception:
39  _LOGGER.warning(exception)
40  errors["base"] = "cannot_connect"
41  except EnergyFlipException as exception:
42  _LOGGER.warning(exception)
43  errors["base"] = "invalid_auth"
44  except AbortFlow:
45  raise
46  except Exception:
47  _LOGGER.exception("Unexpected exception")
48  errors["base"] = "unknown"
49  else:
50  # Set user id as unique id
51  await self.async_set_unique_idasync_set_unique_id(user_id)
52  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
53 
54  # Create entry
55  return self.async_create_entryasync_create_entryasync_create_entry(
56  title=user_input[CONF_USERNAME],
57  data={
58  CONF_ID: user_id,
59  CONF_USERNAME: user_input[CONF_USERNAME],
60  CONF_PASSWORD: user_input[CONF_PASSWORD],
61  },
62  )
63 
64  return await self._show_setup_form_show_setup_form(user_input, errors)
65 
66  async def _show_setup_form(self, user_input, errors=None):
67  return self.async_show_formasync_show_formasync_show_form(
68  step_id="user", data_schema=DATA_SCHEMA, errors=errors or {}
69  )
70 
71  async def _validate_input(self, user_input):
72  """Validate the user input allows us to connect.
73 
74  Data has the keys from DATA_SCHEMA with values provided by the user.
75  """
76  username = user_input[CONF_USERNAME]
77  password = user_input[CONF_PASSWORD]
78 
79  energyflip = EnergyFlip(username, password)
80 
81  # Attempt authentication. If this fails, an EnergyFlipException will be thrown
82  await energyflip.authenticate()
83 
84  # Request customer overview. This also sets the user id on the client
85  await energyflip.customer_overview()
86 
87  return energyflip.get_user_id()
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:29
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_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)