Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Aurora."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from aiohttp import ClientError
9 from auroranoaa import AuroraForecast
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
14 from homeassistant.core import callback
15 from homeassistant.helpers import aiohttp_client, config_validation as cv
17  SchemaFlowFormStep,
18  SchemaOptionsFlowHandler,
19 )
20 
21 from .const import CONF_THRESHOLD, DEFAULT_THRESHOLD, DOMAIN
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 OPTIONS_SCHEMA = vol.Schema(
26  {
27  vol.Required(CONF_THRESHOLD, default=DEFAULT_THRESHOLD): vol.All(
28  vol.Coerce(int), vol.Range(min=0, max=100)
29  ),
30  }
31 )
32 OPTIONS_FLOW = {
33  "init": SchemaFlowFormStep(OPTIONS_SCHEMA),
34 }
35 
36 
37 class AuroraConfigFlow(ConfigFlow, domain=DOMAIN):
38  """Handle a config flow for NOAA Aurora Integration."""
39 
40  VERSION = 1
41 
42  @staticmethod
43  @callback
45  config_entry: ConfigEntry,
46  ) -> SchemaOptionsFlowHandler:
47  """Get the options flow for this handler."""
48  return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW)
49 
50  async def async_step_user(
51  self, user_input: dict[str, Any] | None = None
52  ) -> ConfigFlowResult:
53  """Handle the initial step."""
54  errors: dict[str, str] = {}
55 
56  if user_input is not None:
57  longitude = user_input[CONF_LONGITUDE]
58  latitude = user_input[CONF_LATITUDE]
59 
60  session = aiohttp_client.async_get_clientsession(self.hass)
61  api = AuroraForecast(session=session)
62 
63  try:
64  await api.get_forecast_data(longitude, latitude)
65  except ClientError:
66  errors["base"] = "cannot_connect"
67  except Exception:
68  _LOGGER.exception("Unexpected exception")
69  errors["base"] = "unknown"
70  else:
71  await self.async_set_unique_idasync_set_unique_id(
72  f"{user_input[CONF_LONGITUDE]}_{user_input[CONF_LATITUDE]}"
73  )
74  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
75  return self.async_create_entryasync_create_entryasync_create_entry(
76  title="Aurora visibility", data=user_input
77  )
78 
79  return self.async_show_formasync_show_formasync_show_form(
80  step_id="user",
81  data_schema=self.add_suggested_values_to_schemaadd_suggested_values_to_schema(
82  vol.Schema(
83  {
84  vol.Required(CONF_LONGITUDE): cv.longitude,
85  vol.Required(CONF_LATITUDE): cv.latitude,
86  }
87  ),
88  {
89  CONF_LONGITUDE: self.hass.config.longitude,
90  CONF_LATITUDE: self.hass.config.latitude,
91  },
92  ),
93  errors=errors,
94  )
SchemaOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:46
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:52
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)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)