Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for AEMET OpenData."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aemet_opendata.exceptions import AuthError
8 from aemet_opendata.interface import AEMET, ConnectionOptions
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
13 from homeassistant.core import callback
14 from homeassistant.helpers import aiohttp_client, config_validation as cv
16  SchemaFlowFormStep,
17  SchemaOptionsFlowHandler,
18 )
19 
20 from .const import CONF_STATION_UPDATES, DEFAULT_NAME, DOMAIN
21 
22 OPTIONS_SCHEMA = vol.Schema(
23  {
24  vol.Required(CONF_STATION_UPDATES, default=True): bool,
25  }
26 )
27 OPTIONS_FLOW = {
28  "init": SchemaFlowFormStep(OPTIONS_SCHEMA),
29 }
30 
31 
32 class AemetConfigFlow(ConfigFlow, domain=DOMAIN):
33  """Config flow for AEMET OpenData."""
34 
35  async def async_step_user(
36  self, user_input: dict[str, Any] | None = None
37  ) -> ConfigFlowResult:
38  """Handle a flow initialized by the user."""
39  errors = {}
40 
41  if user_input is not None:
42  latitude = user_input[CONF_LATITUDE]
43  longitude = user_input[CONF_LONGITUDE]
44 
45  await self.async_set_unique_idasync_set_unique_id(f"{latitude}-{longitude}")
46  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
47 
48  options = ConnectionOptions(user_input[CONF_API_KEY])
49  aemet = AEMET(aiohttp_client.async_get_clientsession(self.hass), options)
50  try:
51  await aemet.select_coordinates(latitude, longitude)
52  except AuthError:
53  errors["base"] = "invalid_api_key"
54 
55  if not errors:
56  return self.async_create_entryasync_create_entryasync_create_entry(
57  title=user_input[CONF_NAME], data=user_input
58  )
59 
60  schema = vol.Schema(
61  {
62  vol.Required(CONF_API_KEY): str,
63  vol.Optional(CONF_NAME, default=DEFAULT_NAME): str,
64  vol.Optional(
65  CONF_LATITUDE, default=self.hass.config.latitude
66  ): cv.latitude,
67  vol.Optional(
68  CONF_LONGITUDE, default=self.hass.config.longitude
69  ): cv.longitude,
70  }
71  )
72 
73  return self.async_show_formasync_show_formasync_show_form(step_id="user", data_schema=schema, errors=errors)
74 
75  @staticmethod
76  @callback
78  config_entry: ConfigEntry,
79  ) -> SchemaOptionsFlowHandler:
80  """Get the options flow for this handler."""
81  return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:37
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)
OptionsFlow async_get_options_flow(ConfigEntry config_entry)
_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)