Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for PEGELONLINE."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiopegelonline import CONNECT_ERRORS, PegelOnline
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import (
12  CONF_LATITUDE,
13  CONF_LOCATION,
14  CONF_LONGITUDE,
15  CONF_RADIUS,
16  UnitOfLength,
17 )
18 from homeassistant.helpers.aiohttp_client import async_get_clientsession
20  LocationSelector,
21  NumberSelector,
22  NumberSelectorConfig,
23  SelectOptionDict,
24  SelectSelector,
25  SelectSelectorConfig,
26  SelectSelectorMode,
27 )
28 
29 from .const import CONF_STATION, DEFAULT_RADIUS, DOMAIN
30 
31 
32 class FlowHandler(ConfigFlow, domain=DOMAIN):
33  """Handle a config flow."""
34 
35  VERSION = 1
36 
37  def __init__(self) -> None:
38  """Init the FlowHandler."""
39  super().__init__()
40  self._data_data: dict[str, Any] = {}
41  self._stations: dict[str, str] = {}
42 
43  async def async_step_user(
44  self, user_input: dict[str, Any] | None = None
45  ) -> ConfigFlowResult:
46  """Handle a flow initialized by the user."""
47  if not user_input:
48  return self._show_form_user_show_form_user()
49 
50  api = PegelOnline(async_get_clientsession(self.hass))
51  try:
52  stations = await api.async_get_nearby_stations(
53  user_input[CONF_LOCATION][CONF_LATITUDE],
54  user_input[CONF_LOCATION][CONF_LONGITUDE],
55  user_input[CONF_RADIUS],
56  )
57  except CONNECT_ERRORS:
58  return self._show_form_user_show_form_user(user_input, errors={"base": "cannot_connect"})
59 
60  if len(stations) == 0:
61  return self._show_form_user_show_form_user(user_input, errors={CONF_RADIUS: "no_stations"})
62 
63  for uuid, station in stations.items():
64  self._stations[uuid] = f"{station.name} {station.water_name}"
65 
66  self._data_data = user_input
67 
68  return await self.async_step_select_stationasync_step_select_station()
69 
71  self, user_input: dict[str, Any] | None = None
72  ) -> ConfigFlowResult:
73  """Handle the step select_station of a flow initialized by the user."""
74  if not user_input:
75  stations = [
76  SelectOptionDict(value=k, label=v) for k, v in self._stations.items()
77  ]
78  return self.async_show_formasync_show_formasync_show_form(
79  step_id="select_station",
80  description_placeholders={"stations_count": str(len(self._stations))},
81  data_schema=vol.Schema(
82  {
83  vol.Required(CONF_STATION): SelectSelector(
85  options=stations, mode=SelectSelectorMode.DROPDOWN
86  )
87  )
88  }
89  ),
90  )
91 
92  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_STATION])
93  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
94 
95  return self.async_create_entryasync_create_entryasync_create_entry(
96  title=self._stations[user_input[CONF_STATION]],
97  data=user_input,
98  )
99 
101  self,
102  user_input: dict[str, Any] | None = None,
103  errors: dict[str, Any] | None = None,
104  ) -> ConfigFlowResult:
105  if user_input is None:
106  user_input = {}
107  return self.async_show_formasync_show_formasync_show_form(
108  step_id="user",
109  data_schema=vol.Schema(
110  {
111  vol.Required(
112  CONF_LOCATION,
113  default=user_input.get(
114  CONF_LOCATION,
115  {
116  "latitude": self.hass.config.latitude,
117  "longitude": self.hass.config.longitude,
118  },
119  ),
120  ): LocationSelector(),
121  vol.Required(
122  CONF_RADIUS, default=user_input.get(CONF_RADIUS, DEFAULT_RADIUS)
123  ): NumberSelector(
125  min=1,
126  max=100,
127  step=1,
128  unit_of_measurement=UnitOfLength.KILOMETERS,
129  ),
130  ),
131  }
132  ),
133  errors=errors,
134  )
ConfigFlowResult _show_form_user(self, dict[str, Any]|None user_input=None, dict[str, Any]|None errors=None)
Definition: config_flow.py:104
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:45
ConfigFlowResult async_step_select_station(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:72
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)
str
_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)
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)