Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for SRP Energy."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from srpenergy.client import SrpEnergyClient
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_ID, CONF_NAME, CONF_PASSWORD, CONF_USERNAME
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.exceptions import HomeAssistantError
14 
15 from .const import CONF_IS_TOU, DOMAIN, LOGGER
16 
17 
18 async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
19  """Validate the user input allows us to connect.
20 
21  Data has the keys from DATA_SCHEMA with values provided by the user.
22  """
23  srp_client = SrpEnergyClient(
24  data[CONF_ID],
25  data[CONF_USERNAME],
26  data[CONF_PASSWORD],
27  )
28 
29  is_valid = await hass.async_add_executor_job(srp_client.validate)
30 
31  LOGGER.debug("Is user input valid: %s", is_valid)
32  if not is_valid:
33  raise InvalidAuth
34 
35  return is_valid
36 
37 
38 class SRPEnergyConfigFlow(ConfigFlow, domain=DOMAIN):
39  """Handle an SRP Energy config flow."""
40 
41  VERSION = 1
42 
43  @callback
44  def _show_form(self, errors: dict[str, Any]) -> ConfigFlowResult:
45  """Show the form to the user."""
46  LOGGER.debug("Show Form")
47  return self.async_show_formasync_show_formasync_show_form(
48  step_id="user",
49  data_schema=vol.Schema(
50  {
51  vol.Required(
52  CONF_NAME, default=self.hass.config.location_name
53  ): str,
54  vol.Required(CONF_ID): str,
55  vol.Required(CONF_USERNAME): str,
56  vol.Required(CONF_PASSWORD): str,
57  vol.Optional(CONF_IS_TOU, default=False): bool,
58  }
59  ),
60  errors=errors,
61  )
62 
63  async def async_step_user(
64  self, user_input: dict[str, Any] | None = None
65  ) -> ConfigFlowResult:
66  """Handle a flow initialized by the user."""
67  LOGGER.debug("Config entry")
68  errors: dict[str, str] = {}
69  if not user_input:
70  return self._show_form_show_form(errors)
71 
72  try:
73  await validate_input(self.hass, user_input)
74  except ValueError:
75  # Thrown when the account id is malformed
76  errors["base"] = "invalid_account"
77  return self._show_form_show_form(errors)
78  except InvalidAuth:
79  errors["base"] = "invalid_auth"
80  return self._show_form_show_form(errors)
81  except Exception: # noqa: BLE001
82  LOGGER.exception("Unexpected exception")
83  return self.async_abortasync_abortasync_abort(reason="unknown")
84 
85  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_ID])
86  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
87 
88  return self.async_create_entryasync_create_entryasync_create_entry(title=user_input[CONF_NAME], data=user_input)
89 
90 
92  """Error to indicate there is invalid auth."""
ConfigFlowResult _show_form(self, dict[str, Any] errors)
Definition: config_flow.py:44
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:65
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_abort(self, *str reason, 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)
_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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
dict[str, Any] validate_input(HomeAssistant hass, dict[str, Any] data)
Definition: config_flow.py:18