1 """Config flow for AirNow integration."""
3 from __future__
import annotations
8 from pyairnow
import WebServiceAPI
9 from pyairnow.errors
import AirNowError, EmptyResponseError, InvalidKeyError
10 import voluptuous
as vol
24 from .const
import DOMAIN
26 _LOGGER = logging.getLogger(__name__)
30 """Validate the user input allows us to connect.
32 Data has the keys from DATA_SCHEMA with values provided by the user.
35 client = WebServiceAPI(data[CONF_API_KEY], session=session)
37 lat = data[CONF_LATITUDE]
38 lng = data[CONF_LONGITUDE]
39 distance = data[CONF_RADIUS]
43 test_data = await client.observations.latLong(lat, lng, distance=distance)
45 except InvalidKeyError
as exc:
46 raise InvalidAuth
from exc
47 except AirNowError
as exc:
48 raise CannotConnect
from exc
49 except EmptyResponseError
as exc:
50 raise InvalidLocation
from exc
60 """Handle a config flow for AirNow."""
65 self, user_input: dict[str, Any] |
None =
None
66 ) -> ConfigFlowResult:
67 """Handle the initial step."""
69 if user_input
is not None:
72 f
"{user_input[CONF_LATITUDE]}-{user_input[CONF_LONGITUDE]}"
81 errors[
"base"] =
"cannot_connect"
83 errors[
"base"] =
"invalid_auth"
84 except InvalidLocation:
85 errors[
"base"] =
"invalid_location"
87 _LOGGER.exception(
"Unexpected exception")
88 errors[
"base"] =
"unknown"
91 radius = user_input.pop(CONF_RADIUS)
94 f
"AirNow Sensor at {user_input[CONF_LATITUDE]},"
95 f
" {user_input[CONF_LONGITUDE]}"
98 options={CONF_RADIUS: radius},
103 data_schema=vol.Schema(
105 vol.Required(CONF_API_KEY): str,
107 CONF_LATITUDE, default=self.hass.config.latitude
110 CONF_LONGITUDE, default=self.hass.config.longitude
112 vol.Optional(CONF_RADIUS, default=150): vol.All(
113 int, vol.Range(min=5)
123 config_entry: ConfigEntry,
124 ) -> AirNowOptionsFlowHandler:
125 """Return the options flow."""
130 """Handle an options flow for AirNow."""
133 self, user_input: dict[str, Any] |
None =
None
134 ) -> ConfigFlowResult:
135 """Manage the options."""
136 if user_input
is not None:
139 options_schema = vol.Schema(
140 {vol.Optional(CONF_RADIUS): vol.All(int, vol.Range(min=5))}
152 """Error to indicate we cannot connect."""
155 class InvalidAuth(HomeAssistantError):
156 """Error to indicate there is invalid auth."""
160 """Error to indicate the location is invalid."""
AirNowOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=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|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)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
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)
bool validate_input(HomeAssistant hass, dict[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)