1 """Config flow for Tankerkoenig."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
8 from aiotankerkoenig
import (
13 TankerkoenigInvalidKeyError,
15 import voluptuous
as vol
42 from .const
import CONF_FUEL_TYPES, CONF_STATIONS, DEFAULT_RADIUS, DOMAIN, FUEL_TYPES
46 tankerkoenig: Tankerkoenig, data: Mapping[str, Any]
48 """Fetch nearby stations."""
49 return await tankerkoenig.nearby_stations(
51 data[CONF_LOCATION][CONF_LATITUDE],
52 data[CONF_LOCATION][CONF_LONGITUDE],
54 radius=data[CONF_RADIUS],
61 """Handle a config flow."""
66 """Init the FlowHandler."""
68 self.
_data_data: dict[str, Any] = {}
69 self._stations: dict[str, str] = {}
74 config_entry: ConfigEntry,
75 ) -> OptionsFlowHandler:
76 """Get the options flow for this handler."""
80 self, user_input: dict[str, Any] |
None =
None
81 ) -> ConfigFlowResult:
82 """Handle a flow initialized by the user."""
87 f
"{user_input[CONF_LOCATION][CONF_LATITUDE]}_{user_input[CONF_LOCATION][CONF_LONGITUDE]}"
91 tankerkoenig = Tankerkoenig(
92 api_key=user_input[CONF_API_KEY],
97 except TankerkoenigInvalidKeyError:
99 user_input, errors={CONF_API_KEY:
"invalid_auth"}
103 if len(stations) == 0:
104 return self.
_show_form_user_show_form_user(user_input, errors={CONF_RADIUS:
"no_stations"})
106 for station
in stations:
107 self._stations[station.id] = (
108 f
"{station.brand} {station.street} {station.house_number} -"
109 f
" ({station.distance}km)"
117 self, user_input: dict[str, Any] |
None =
None
118 ) -> ConfigFlowResult:
119 """Handle the step select_station of a flow initialized by the user."""
122 step_id=
"select_station",
123 description_placeholders={
"stations_count":
str(len(self._stations))},
124 data_schema=vol.Schema(
125 {vol.Required(CONF_STATIONS): cv.multi_select(self._stations)}
130 data={**self.
_data_data, **user_input},
131 options={CONF_SHOW_ON_MAP:
True},
135 self, entry_data: Mapping[str, Any]
136 ) -> ConfigFlowResult:
137 """Perform reauth upon an API authentication error."""
141 self, user_input: dict[str, Any] |
None =
None
142 ) -> ConfigFlowResult:
143 """Perform reauth confirm upon an API authentication error."""
148 user_input = {**reauth_entry.data, **user_input}
150 tankerkoenig = Tankerkoenig(
151 api_key=user_input[CONF_API_KEY],
156 except TankerkoenigInvalidKeyError:
157 return self.
_show_form_reauth_show_form_reauth(user_input, {CONF_API_KEY:
"invalid_auth"})
163 user_input: dict[str, Any] |
None =
None,
164 errors: dict[str, Any] |
None =
None,
165 ) -> ConfigFlowResult:
166 if user_input
is None:
170 data_schema=vol.Schema(
173 CONF_NAME, default=user_input.get(CONF_NAME,
"")
176 CONF_API_KEY, default=user_input.get(CONF_API_KEY,
"")
180 default=user_input.get(CONF_FUEL_TYPES,
list(FUEL_TYPES)),
181 ): cv.multi_select(FUEL_TYPES),
184 default=user_input.get(
187 "latitude": self.hass.config.latitude,
188 "longitude": self.hass.config.longitude,
193 CONF_RADIUS, default=user_input.get(CONF_RADIUS, DEFAULT_RADIUS)
199 unit_of_measurement=UnitOfLength.KILOMETERS,
209 user_input: dict[str, Any] |
None =
None,
210 errors: dict[str, Any] |
None =
None,
211 ) -> ConfigFlowResult:
212 if user_input
is None:
215 step_id=
"reauth_confirm",
216 data_schema=vol.Schema(
219 CONF_API_KEY, default=user_input.get(CONF_API_KEY,
"")
227 self, data: dict[str, Any], options: dict[str, Any]
228 ) -> ConfigFlowResult:
230 title=data[CONF_NAME],
237 """Handle an options flow."""
240 """Initialize options flow."""
241 self._stations: dict[str, str] = {}
244 self, user_input: dict[str, Any] |
None =
None
245 ) -> ConfigFlowResult:
246 """Handle options flow."""
247 if user_input
is not None:
248 self.hass.config_entries.async_update_entry(
252 CONF_STATIONS: user_input.pop(CONF_STATIONS),
257 tankerkoenig = Tankerkoenig(
265 except TankerkoenigInvalidKeyError:
266 return self.
async_show_formasync_show_form(step_id=
"init", errors={
"base":
"invalid_auth"})
269 for station
in stations:
270 self._stations[station.id] = (
271 f
"{station.brand} {station.street} {station.house_number} -"
272 f
" ({station.distance}km)"
277 if selected_station
not in self._stations:
278 self._stations[selected_station] = f
"id: {selected_station}"
282 data_schema=vol.Schema(
290 ): cv.multi_select(self._stations),
ConfigFlowResult async_step_select_station(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
OptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_reauth_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult _create_entry(self, dict[str, Any] data, dict[str, Any] options)
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
ConfigFlowResult _show_form_user(self, dict[str, Any]|None user_input=None, dict[str, Any]|None errors=None)
ConfigFlowResult _show_form_reauth(self, dict[str, Any]|None user_input=None, dict[str, Any]|None errors=None)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry _get_reauth_entry(self)
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_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)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
_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)
list[Station] async_get_nearby_stations(Tankerkoenig tankerkoenig, Mapping[str, Any] data)
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)