Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Elvia integration."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 from typing import TYPE_CHECKING, Any
7 
8 from elvia import Elvia, error as ElviaError
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_API_TOKEN
13 from homeassistant.util import dt as dt_util
14 
15 from .const import CONF_METERING_POINT_ID, DOMAIN, LOGGER
16 
17 
18 class ElviaConfigFlow(ConfigFlow, domain=DOMAIN):
19  """Handle a config flow for Elvia."""
20 
21  def __init__(self) -> None:
22  """Initialize."""
23  self._api_token_api_token: str | None = None
24  self._metering_point_ids_metering_point_ids: list[str] | None = None
25 
26  async def async_step_user(
27  self,
28  user_input: dict[str, Any] | None = None,
29  ) -> ConfigFlowResult:
30  """Handle the initial step."""
31  errors: dict[str, str] = {}
32  if user_input is not None:
33  self._api_token_api_token = api_token = user_input[CONF_API_TOKEN]
34  client = Elvia(meter_value_token=api_token).meter_value()
35  try:
36  end_time = dt_util.utcnow()
37  results = await client.get_meter_values(
38  start_time=(end_time - timedelta(hours=1)).isoformat(),
39  end_time=end_time.isoformat(),
40  )
41 
42  except ElviaError.AuthError as exception:
43  LOGGER.error("Authentication error %s", exception)
44  errors["base"] = "invalid_auth"
45  except ElviaError.ElviaException as exception:
46  LOGGER.error("Unknown error %s", exception)
47  errors["base"] = "unknown"
48  else:
49  try:
50  self._metering_point_ids_metering_point_ids = metering_point_ids = [
51  x["meteringPointId"] for x in results["meteringpoints"]
52  ]
53  except KeyError:
54  return self.async_abortasync_abortasync_abort(reason="no_metering_points")
55 
56  if (meter_count := len(metering_point_ids)) > 1:
57  return await self.async_step_select_meterasync_step_select_meter()
58  if meter_count == 1:
59  return await self._create_config_entry_create_config_entry(
60  api_token=api_token,
61  metering_point_id=metering_point_ids[0],
62  )
63 
64  return self.async_abortasync_abortasync_abort(reason="no_metering_points")
65 
66  return self.async_show_formasync_show_formasync_show_form(
67  step_id="user",
68  data_schema=vol.Schema(
69  {
70  vol.Required(CONF_API_TOKEN): str,
71  }
72  ),
73  errors=errors,
74  )
75 
77  self, user_input: dict[str, Any] | None = None
78  ) -> ConfigFlowResult:
79  """Handle selecting a metering point ID."""
80  if TYPE_CHECKING:
81  assert self._metering_point_ids_metering_point_ids is not None
82  assert self._api_token_api_token is not None
83 
84  if user_input is not None:
85  return await self._create_config_entry_create_config_entry(
86  api_token=self._api_token_api_token,
87  metering_point_id=user_input[CONF_METERING_POINT_ID],
88  )
89 
90  return self.async_show_formasync_show_formasync_show_form(
91  step_id="select_meter",
92  data_schema=vol.Schema(
93  {
94  vol.Required(
95  CONF_METERING_POINT_ID,
96  default=self._metering_point_ids_metering_point_ids[0],
97  ): vol.In(self._metering_point_ids_metering_point_ids),
98  }
99  ),
100  )
101 
103  self,
104  api_token: str,
105  metering_point_id: str,
106  ) -> ConfigFlowResult:
107  """Store metering point ID and API token."""
108  if (await self.async_set_unique_idasync_set_unique_id(metering_point_id)) is not None:
109  return self.async_abortasync_abortasync_abort(
110  reason="metering_point_id_already_configured",
111  description_placeholders={"metering_point_id": metering_point_id},
112  )
113  return self.async_create_entryasync_create_entryasync_create_entry(
114  title=metering_point_id,
115  data={
116  CONF_API_TOKEN: api_token,
117  CONF_METERING_POINT_ID: metering_point_id,
118  },
119  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:29
ConfigFlowResult _create_config_entry(self, str api_token, str metering_point_id)
Definition: config_flow.py:106
ConfigFlowResult async_step_select_meter(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:78
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)