|
| 1 | +"""Config flow for the Google Air Quality integration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import logging |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from google_air_quality_api.api import GoogleAirQualityApi |
| 9 | +from google_air_quality_api.auth import Auth |
| 10 | +from google_air_quality_api.exceptions import GoogleAirQualityApiError |
| 11 | +import voluptuous as vol |
| 12 | + |
| 13 | +from homeassistant.config_entries import ( |
| 14 | + ConfigEntry, |
| 15 | + ConfigEntryState, |
| 16 | + ConfigFlow, |
| 17 | + ConfigFlowResult, |
| 18 | + ConfigSubentryFlow, |
| 19 | + SubentryFlowResult, |
| 20 | +) |
| 21 | +from homeassistant.const import ( |
| 22 | + CONF_API_KEY, |
| 23 | + CONF_LATITUDE, |
| 24 | + CONF_LOCATION, |
| 25 | + CONF_LONGITUDE, |
| 26 | + CONF_NAME, |
| 27 | +) |
| 28 | +from homeassistant.core import HomeAssistant, callback |
| 29 | +from homeassistant.data_entry_flow import section |
| 30 | +from homeassistant.helpers.aiohttp_client import async_get_clientsession |
| 31 | +from homeassistant.helpers.selector import LocationSelector, LocationSelectorConfig |
| 32 | + |
| 33 | +from .const import CONF_REFERRER, DOMAIN, SECTION_API_KEY_OPTIONS |
| 34 | + |
| 35 | +_LOGGER = logging.getLogger(__name__) |
| 36 | + |
| 37 | +STEP_USER_DATA_SCHEMA = vol.Schema( |
| 38 | + { |
| 39 | + vol.Required(CONF_API_KEY): str, |
| 40 | + vol.Optional(SECTION_API_KEY_OPTIONS): section( |
| 41 | + vol.Schema({vol.Optional(CONF_REFERRER): str}), {"collapsed": True} |
| 42 | + ), |
| 43 | + } |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +async def _validate_input( |
| 48 | + user_input: dict[str, Any], |
| 49 | + api: GoogleAirQualityApi, |
| 50 | + errors: dict[str, str], |
| 51 | + description_placeholders: dict[str, str], |
| 52 | +) -> bool: |
| 53 | + try: |
| 54 | + await api.async_air_quality( |
| 55 | + lat=user_input[CONF_LOCATION][CONF_LATITUDE], |
| 56 | + long=user_input[CONF_LOCATION][CONF_LONGITUDE], |
| 57 | + ) |
| 58 | + except GoogleAirQualityApiError as err: |
| 59 | + errors["base"] = "cannot_connect" |
| 60 | + description_placeholders["error_message"] = str(err) |
| 61 | + except Exception: |
| 62 | + _LOGGER.exception("Unexpected exception") |
| 63 | + errors["base"] = "unknown" |
| 64 | + else: |
| 65 | + return True |
| 66 | + return False |
| 67 | + |
| 68 | + |
| 69 | +def _get_location_schema(hass: HomeAssistant) -> vol.Schema: |
| 70 | + """Return the schema for a location with default values from the hass config.""" |
| 71 | + return vol.Schema( |
| 72 | + { |
| 73 | + vol.Required(CONF_NAME, default=hass.config.location_name): str, |
| 74 | + vol.Required( |
| 75 | + CONF_LOCATION, |
| 76 | + default={ |
| 77 | + CONF_LATITUDE: hass.config.latitude, |
| 78 | + CONF_LONGITUDE: hass.config.longitude, |
| 79 | + }, |
| 80 | + ): LocationSelector(LocationSelectorConfig(radius=False)), |
| 81 | + } |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +def _is_location_already_configured( |
| 86 | + hass: HomeAssistant, new_data: dict[str, float], epsilon: float = 1e-4 |
| 87 | +) -> bool: |
| 88 | + """Check if the location is already configured.""" |
| 89 | + for entry in hass.config_entries.async_entries(DOMAIN): |
| 90 | + for subentry in entry.subentries.values(): |
| 91 | + # A more accurate way is to use the haversine formula, but for simplicity |
| 92 | + # we use a simple distance check. The epsilon value is small anyway. |
| 93 | + # This is mostly to capture cases where the user has slightly moved the location pin. |
| 94 | + if ( |
| 95 | + abs(subentry.data[CONF_LATITUDE] - new_data[CONF_LATITUDE]) <= epsilon |
| 96 | + and abs(subentry.data[CONF_LONGITUDE] - new_data[CONF_LONGITUDE]) |
| 97 | + <= epsilon |
| 98 | + ): |
| 99 | + return True |
| 100 | + return False |
| 101 | + |
| 102 | + |
| 103 | +class GoogleAirQualityConfigFlow(ConfigFlow, domain=DOMAIN): |
| 104 | + """Handle a config flow for Google AirQuality.""" |
| 105 | + |
| 106 | + VERSION = 1 |
| 107 | + |
| 108 | + async def async_step_user( |
| 109 | + self, user_input: dict[str, Any] | None = None |
| 110 | + ) -> ConfigFlowResult: |
| 111 | + """Handle the initial step.""" |
| 112 | + errors: dict[str, str] = {} |
| 113 | + description_placeholders: dict[str, str] = { |
| 114 | + "api_key_url": "https://developers.google.com/maps/documentation/air-quality/get-api-key", |
| 115 | + "restricting_api_keys_url": "https://developers.google.com/maps/api-security-best-practices#restricting-api-keys", |
| 116 | + } |
| 117 | + if user_input is not None: |
| 118 | + api_key = user_input[CONF_API_KEY] |
| 119 | + referrer = user_input.get(SECTION_API_KEY_OPTIONS, {}).get(CONF_REFERRER) |
| 120 | + self._async_abort_entries_match({CONF_API_KEY: api_key}) |
| 121 | + if _is_location_already_configured(self.hass, user_input[CONF_LOCATION]): |
| 122 | + return self.async_abort(reason="already_configured") |
| 123 | + session = async_get_clientsession(self.hass) |
| 124 | + referrer = user_input.get(SECTION_API_KEY_OPTIONS, {}).get(CONF_REFERRER) |
| 125 | + auth = Auth(session, user_input[CONF_API_KEY], referrer=referrer) |
| 126 | + api = GoogleAirQualityApi(auth) |
| 127 | + if await _validate_input(user_input, api, errors, description_placeholders): |
| 128 | + return self.async_create_entry( |
| 129 | + title="Google Air Quality", |
| 130 | + data={ |
| 131 | + CONF_API_KEY: api_key, |
| 132 | + CONF_REFERRER: referrer, |
| 133 | + }, |
| 134 | + subentries=[ |
| 135 | + { |
| 136 | + "subentry_type": "location", |
| 137 | + "data": user_input[CONF_LOCATION], |
| 138 | + "title": user_input[CONF_NAME], |
| 139 | + "unique_id": None, |
| 140 | + }, |
| 141 | + ], |
| 142 | + ) |
| 143 | + else: |
| 144 | + user_input = {} |
| 145 | + schema = STEP_USER_DATA_SCHEMA.schema.copy() |
| 146 | + schema.update(_get_location_schema(self.hass).schema) |
| 147 | + return self.async_show_form( |
| 148 | + step_id="user", |
| 149 | + data_schema=self.add_suggested_values_to_schema( |
| 150 | + vol.Schema(schema), user_input |
| 151 | + ), |
| 152 | + errors=errors, |
| 153 | + description_placeholders=description_placeholders, |
| 154 | + ) |
| 155 | + |
| 156 | + @classmethod |
| 157 | + @callback |
| 158 | + def async_get_supported_subentry_types( |
| 159 | + cls, config_entry: ConfigEntry |
| 160 | + ) -> dict[str, type[ConfigSubentryFlow]]: |
| 161 | + """Return subentries supported by this integration.""" |
| 162 | + return {"location": LocationSubentryFlowHandler} |
| 163 | + |
| 164 | + |
| 165 | +class LocationSubentryFlowHandler(ConfigSubentryFlow): |
| 166 | + """Handle a subentry flow for location.""" |
| 167 | + |
| 168 | + async def async_step_location( |
| 169 | + self, |
| 170 | + user_input: dict[str, Any] | None = None, |
| 171 | + ) -> SubentryFlowResult: |
| 172 | + """Handle the location step.""" |
| 173 | + if self._get_entry().state != ConfigEntryState.LOADED: |
| 174 | + return self.async_abort(reason="entry_not_loaded") |
| 175 | + |
| 176 | + errors: dict[str, str] = {} |
| 177 | + description_placeholders: dict[str, str] = {} |
| 178 | + if user_input is not None: |
| 179 | + if _is_location_already_configured(self.hass, user_input[CONF_LOCATION]): |
| 180 | + return self.async_abort(reason="already_configured") |
| 181 | + api: GoogleAirQualityApi = self._get_entry().runtime_data.api |
| 182 | + if await _validate_input(user_input, api, errors, description_placeholders): |
| 183 | + return self.async_create_entry( |
| 184 | + title=user_input[CONF_NAME], |
| 185 | + data=user_input[CONF_LOCATION], |
| 186 | + ) |
| 187 | + else: |
| 188 | + user_input = {} |
| 189 | + return self.async_show_form( |
| 190 | + step_id="location", |
| 191 | + data_schema=self.add_suggested_values_to_schema( |
| 192 | + _get_location_schema(self.hass), user_input |
| 193 | + ), |
| 194 | + errors=errors, |
| 195 | + description_placeholders=description_placeholders, |
| 196 | + ) |
| 197 | + |
| 198 | + async_step_user = async_step_location |
0 commit comments