Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Co2signal integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from aioelectricitymaps import (
9  ElectricityMaps,
10  ElectricityMapsError,
11  ElectricityMapsInvalidTokenError,
12  ElectricityMapsNoDataError,
13 )
14 import voluptuous as vol
15 
16 from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
17 from homeassistant.const import (
18  CONF_API_KEY,
19  CONF_COUNTRY_CODE,
20  CONF_LATITUDE,
21  CONF_LONGITUDE,
22 )
23 from homeassistant.helpers.aiohttp_client import async_get_clientsession
26  SelectSelector,
27  SelectSelectorConfig,
28  SelectSelectorMode,
29 )
30 
31 from .const import DOMAIN
32 from .helpers import fetch_latest_carbon_intensity
33 from .util import get_extra_name
34 
35 TYPE_USE_HOME = "use_home_location"
36 TYPE_SPECIFY_COORDINATES = "specify_coordinates"
37 TYPE_SPECIFY_COUNTRY = "specify_country_code"
38 
39 
40 class ElectricityMapsConfigFlow(ConfigFlow, domain=DOMAIN):
41  """Handle a config flow for Co2signal."""
42 
43  VERSION = 1
44  _data: dict | None
45 
46  async def async_step_user(
47  self, user_input: dict[str, Any] | None = None
48  ) -> ConfigFlowResult:
49  """Handle the initial step."""
50  data_schema = vol.Schema(
51  {
52  vol.Required("location"): SelectSelector(
54  translation_key="location",
55  mode=SelectSelectorMode.LIST,
56  options=[
57  TYPE_USE_HOME,
58  TYPE_SPECIFY_COORDINATES,
59  TYPE_SPECIFY_COUNTRY,
60  ],
61  )
62  ),
63  vol.Required(CONF_API_KEY): cv.string,
64  }
65  )
66 
67  if user_input is None:
68  return self.async_show_formasync_show_formasync_show_form(
69  step_id="user",
70  data_schema=data_schema,
71  )
72 
73  data = {CONF_API_KEY: user_input[CONF_API_KEY]}
74 
75  if user_input["location"] == TYPE_SPECIFY_COORDINATES:
76  self._data_data = data
77  return await self.async_step_coordinatesasync_step_coordinates()
78 
79  if user_input["location"] == TYPE_SPECIFY_COUNTRY:
80  self._data_data = data
81  return await self.async_step_countryasync_step_country()
82 
83  return await self._validate_and_create_validate_and_create("user", data_schema, data)
84 
86  self, user_input: dict[str, Any] | None = None
87  ) -> ConfigFlowResult:
88  """Validate coordinates."""
89  data_schema = vol.Schema(
90  {
91  vol.Required(
92  CONF_LATITUDE,
93  ): cv.latitude,
94  vol.Required(
95  CONF_LONGITUDE,
96  ): cv.longitude,
97  }
98  )
99  if user_input is None:
100  return self.async_show_formasync_show_formasync_show_form(step_id="coordinates", data_schema=data_schema)
101 
102  assert self._data_data is not None
103 
104  return await self._validate_and_create_validate_and_create(
105  "coordinates", data_schema, {**self._data_data, **user_input}
106  )
107 
109  self, user_input: dict[str, Any] | None = None
110  ) -> ConfigFlowResult:
111  """Validate country."""
112  data_schema = vol.Schema(
113  {
114  vol.Required(CONF_COUNTRY_CODE): cv.string,
115  }
116  )
117  if user_input is None:
118  return self.async_show_formasync_show_formasync_show_form(step_id="country", data_schema=data_schema)
119 
120  assert self._data_data is not None
121 
122  return await self._validate_and_create_validate_and_create(
123  "country", data_schema, {**self._data_data, **user_input}
124  )
125 
126  async def async_step_reauth(
127  self, entry_data: Mapping[str, Any]
128  ) -> ConfigFlowResult:
129  """Handle the reauth step."""
130  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
131 
133  self, user_input: dict[str, Any] | None = None
134  ) -> ConfigFlowResult:
135  """Handle the reauth step."""
136  data_schema = vol.Schema(
137  {
138  vol.Required(CONF_API_KEY): cv.string,
139  }
140  )
141  return await self._validate_and_create_validate_and_create(
142  "reauth_confirm", data_schema, user_input
143  )
144 
146  self, step_id: str, data_schema: vol.Schema, data: Mapping[str, Any] | None
147  ) -> ConfigFlowResult:
148  """Validate data and show form if it is invalid."""
149  errors: dict[str, str] = {}
150 
151  if data:
152  session = async_get_clientsession(self.hass)
153  em = ElectricityMaps(token=data[CONF_API_KEY], session=session)
154 
155  try:
156  await fetch_latest_carbon_intensity(self.hass, em, data)
157  except ElectricityMapsInvalidTokenError:
158  errors["base"] = "invalid_auth"
159  except ElectricityMapsNoDataError:
160  errors["base"] = "no_data"
161  except ElectricityMapsError:
162  errors["base"] = "unknown"
163  else:
164  if self.sourcesourcesourcesource == SOURCE_REAUTH:
165  return self.async_update_reload_and_abortasync_update_reload_and_abort(
166  self._get_reauth_entry_get_reauth_entry(),
167  data_updates={CONF_API_KEY: data[CONF_API_KEY]},
168  )
169 
170  return self.async_create_entryasync_create_entryasync_create_entry(
171  title=get_extra_name(data) or "Electricity Maps",
172  data=data,
173  )
174 
175  return self.async_show_formasync_show_formasync_show_form(
176  step_id=step_id,
177  data_schema=data_schema,
178  errors=errors,
179  )
ConfigFlowResult async_step_country(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:110
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:128
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:48
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:134
ConfigFlowResult _validate_and_create(self, str step_id, vol.Schema data_schema, Mapping[str, Any]|None data)
Definition: config_flow.py:147
ConfigFlowResult async_step_coordinates(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:87
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)
str|None source(self)
CarbonIntensityResponse fetch_latest_carbon_intensity(HomeAssistant hass, ElectricityMaps em, Mapping[str, Any] config)
Definition: helpers.py:19
str|None get_extra_name(Mapping[str, Any] config)
Definition: util.py:11
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)