Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for buienradar integration."""
2 
3 from __future__ import annotations
4 
5 import copy
6 from typing import Any, cast
7 
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_COUNTRY_CODE, CONF_LATITUDE, CONF_LONGITUDE
12 from homeassistant.core import callback
13 from homeassistant.helpers import selector
16  SchemaCommonFlowHandler,
17  SchemaFlowFormStep,
18  SchemaOptionsFlowHandler,
19 )
20 
21 from .const import (
22  CONF_DELTA,
23  CONF_TIMEFRAME,
24  DEFAULT_COUNTRY,
25  DEFAULT_DELTA,
26  DEFAULT_TIMEFRAME,
27  DOMAIN,
28  SUPPORTED_COUNTRY_CODES,
29 )
30 
31 OPTIONS_SCHEMA = vol.Schema(
32  {
33  vol.Optional(
34  CONF_COUNTRY_CODE, default=DEFAULT_COUNTRY
35  ): selector.CountrySelector(
36  selector.CountrySelectorConfig(countries=SUPPORTED_COUNTRY_CODES)
37  ),
38  vol.Optional(CONF_DELTA, default=DEFAULT_DELTA): selector.NumberSelector(
39  selector.NumberSelectorConfig(
40  min=0,
41  step=1,
42  mode=selector.NumberSelectorMode.BOX,
43  unit_of_measurement="seconds",
44  ),
45  ),
46  vol.Optional(
47  CONF_TIMEFRAME, default=DEFAULT_TIMEFRAME
48  ): selector.NumberSelector(
49  selector.NumberSelectorConfig(
50  min=5,
51  max=120,
52  step=5,
53  mode=selector.NumberSelectorMode.BOX,
54  unit_of_measurement="minutes",
55  ),
56  ),
57  }
58 )
59 
60 
61 async def _options_suggested_values(handler: SchemaCommonFlowHandler) -> dict[str, Any]:
62  parent_handler = cast(SchemaOptionsFlowHandler, handler.parent_handler)
63  suggested_values = copy.deepcopy(dict(parent_handler.config_entry.data))
64  suggested_values.update(parent_handler.options)
65  return suggested_values
66 
67 
68 OPTIONS_FLOW = {
69  "init": SchemaFlowFormStep(
70  OPTIONS_SCHEMA, suggested_values=_options_suggested_values
71  ),
72 }
73 
74 
75 class BuienradarFlowHandler(ConfigFlow, domain=DOMAIN):
76  """Handle a config flow for buienradar."""
77 
78  VERSION = 1
79 
80  @staticmethod
81  @callback
83  config_entry: ConfigEntry,
84  ) -> SchemaOptionsFlowHandler:
85  """Get the options flow for this handler."""
86  return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW)
87 
88  async def async_step_user(
89  self, user_input: dict[str, Any] | None = None
90  ) -> ConfigFlowResult:
91  """Handle a flow initialized by the user."""
92  if user_input is not None:
93  lat = user_input.get(CONF_LATITUDE)
94  lon = user_input.get(CONF_LONGITUDE)
95 
96  await self.async_set_unique_idasync_set_unique_id(f"{lat}-{lon}")
97  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
98 
99  return self.async_create_entryasync_create_entryasync_create_entry(title=f"{lat},{lon}", data=user_input)
100 
101  data_schema = vol.Schema(
102  {
103  vol.Required(
104  CONF_LATITUDE, default=self.hass.config.latitude
105  ): cv.latitude,
106  vol.Required(
107  CONF_LONGITUDE, default=self.hass.config.longitude
108  ): cv.longitude,
109  }
110  )
111 
112  return self.async_show_formasync_show_formasync_show_form(
113  step_id="user",
114  data_schema=data_schema,
115  errors={},
116  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:90
SchemaOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:84
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)
_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)
dict[str, Any] _options_suggested_values(SchemaCommonFlowHandler handler)
Definition: config_flow.py:61