1 """Config flow for the Ambient Weather Network integration."""
3 from __future__
import annotations
7 from aioambient
import OpenAPI
8 import voluptuous
as vol
21 LocationSelectorConfig,
28 from .const
import API_STATION_INDOOR, API_STATION_INFO, API_STATION_MAC_ADDRESS, DOMAIN
29 from .helper
import get_station_name
32 CONF_STATION =
"station"
35 CONF_RADIUS_DEFAULT = 1609.34
39 """Handle the config flow for the Ambient Weather Network integration."""
44 """Construct the config flow."""
49 self.
_stations_stations: dict[str, dict[str, Any]] = {}
53 user_input: dict[str, Any] |
None =
None,
54 ) -> ConfigFlowResult:
55 """Handle the initial step to select the location."""
57 errors: dict[str, str] |
None =
None
59 self.
_latitude_latitude = user_input[CONF_LOCATION][CONF_LATITUDE]
60 self.
_longitude_longitude = user_input[CONF_LOCATION][CONF_LONGITUDE]
61 self.
_radius_radius = user_input[CONF_LOCATION][CONF_RADIUS]
63 client: OpenAPI = OpenAPI()
65 x[API_STATION_MAC_ADDRESS]: x
66 for x
in await client.get_devices_by_location(
69 radius=DistanceConverter.convert(
80 lambda item:
not item[1]
81 .
get(API_STATION_INFO, {})
82 .
get(API_STATION_INDOOR,
False),
90 errors = {
"base":
"no_stations_found"}
102 CONF_LATITUDE: self.hass.config.latitude,
103 CONF_LONGITUDE: self.hass.config.longitude,
104 CONF_RADIUS: CONF_RADIUS_DEFAULT,
110 CONF_RADIUS: self.
_radius_radius,
116 step_id=CONF_USER, data_schema=schema, errors=errors
if errors
else {}
120 self, user_input: dict[str, Any] |
None =
None
121 ) -> ConfigFlowResult:
122 """Handle the second step to select the station."""
125 mac_address = user_input[CONF_STATION]
130 data={CONF_MAC: mac_address},
133 options: list[SelectOptionDict] = [
138 for mac_address, station
in self.
_stations_stations.items()
141 schema: vol.Schema = vol.Schema(
150 step_id=CONF_STATION,
ConfigFlowResult async_step_station(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(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|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)
str get_station_name(dict[str, Any] station)
web.Response get(self, web.Request request, str config_key)