1 """Adds config flow for Airly."""
3 from __future__
import annotations
5 from asyncio
import timeout
6 from http
import HTTPStatus
9 from aiohttp
import ClientSession
10 from airly
import Airly
11 from airly.exceptions
import AirlyError
12 import voluptuous
as vol
19 from .const
import CONF_USE_NEAREST, DOMAIN, NO_AIRLY_SENSORS
23 """Config flow for Airly."""
28 self, user_input: dict[str, Any] |
None =
None
29 ) -> ConfigFlowResult:
30 """Handle a flow initialized by the user."""
36 if user_input
is not None:
38 f
"{user_input[CONF_LATITUDE]}-{user_input[CONF_LONGITUDE]}"
44 user_input[
"api_key"],
45 user_input[
"latitude"],
46 user_input[
"longitude"],
48 if not location_point_valid:
51 user_input[
"api_key"],
52 user_input[
"latitude"],
53 user_input[
"longitude"],
56 except AirlyError
as err:
57 if err.status_code == HTTPStatus.UNAUTHORIZED:
58 errors[
"base"] =
"invalid_api_key"
59 if err.status_code == HTTPStatus.NOT_FOUND:
60 errors[
"base"] =
"wrong_location"
62 if not location_point_valid:
63 if not location_nearest_valid:
67 title=user_input[CONF_NAME],
68 data={**user_input, CONF_USE_NEAREST: use_nearest},
73 data_schema=vol.Schema(
75 vol.Required(CONF_API_KEY): str,
77 CONF_LATITUDE, default=self.hass.config.latitude
80 CONF_LONGITUDE, default=self.hass.config.longitude
83 CONF_NAME, default=self.hass.config.location_name
92 client: ClientSession,
96 use_nearest: bool =
False,
98 """Return true if location is valid."""
99 airly = Airly(api_key, client)
101 measurements = airly.create_measurements_session_nearest(
102 latitude=latitude, longitude=longitude, max_distance_km=5
105 measurements = airly.create_measurements_session_point(
106 latitude=latitude, longitude=longitude
108 async
with timeout(10):
109 await measurements.update()
111 current = measurements.current
113 if current[
"indexes"][0][
"description"] == NO_AIRLY_SENSORS:
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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
bool test_location(ClientSession client, str api_key, float latitude, float longitude, bool use_nearest=False)
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)