core/homeassistant/components/airly/config_flow.py

111 lines
3.6 KiB
Python
Raw Normal View History

"""Adds config flow for Airly."""
from airly import Airly
from airly.exceptions import AirlyError
import async_timeout
import voluptuous as vol
from homeassistant import config_entries
2020-12-17 18:34:40 +00:00
from homeassistant.const import (
CONF_API_KEY,
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
HTTP_NOT_FOUND,
2020-12-17 18:34:40 +00:00
HTTP_UNAUTHORIZED,
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from .const import ( # pylint:disable=unused-import
CONF_USE_NEAREST,
DOMAIN,
NO_AIRLY_SENSORS,
)
class AirlyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for Airly."""
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
2020-12-19 16:30:45 +00:00
errors = {}
use_nearest = False
websession = async_get_clientsession(self.hass)
if user_input is not None:
await self.async_set_unique_id(
f"{user_input[CONF_LATITUDE]}-{user_input[CONF_LONGITUDE]}"
)
self._abort_if_unique_id_configured()
2020-12-17 18:34:40 +00:00
try:
location_point_valid = await test_location(
websession,
user_input["api_key"],
user_input["latitude"],
user_input["longitude"],
)
if not location_point_valid:
await test_location(
websession,
user_input["api_key"],
user_input["latitude"],
user_input["longitude"],
use_nearest=True,
)
2020-12-17 18:34:40 +00:00
except AirlyError as err:
if err.status_code == HTTP_UNAUTHORIZED:
2020-12-19 16:30:45 +00:00
errors["base"] = "invalid_api_key"
if err.status_code == HTTP_NOT_FOUND:
2020-12-19 16:30:45 +00:00
errors["base"] = "wrong_location"
else:
if not location_point_valid:
use_nearest = True
return self.async_create_entry(
title=user_input[CONF_NAME],
data={**user_input, CONF_USE_NEAREST: use_nearest},
)
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
2020-12-19 16:30:45 +00:00
vol.Required(CONF_API_KEY): str,
vol.Optional(
CONF_LATITUDE, default=self.hass.config.latitude
): cv.latitude,
vol.Optional(
CONF_LONGITUDE, default=self.hass.config.longitude
): cv.longitude,
2020-12-19 16:30:45 +00:00
vol.Optional(
CONF_NAME, default=self.hass.config.location_name
): str,
}
),
2020-12-19 16:30:45 +00:00
errors=errors,
)
async def test_location(client, api_key, latitude, longitude, use_nearest=False):
2020-12-17 18:34:40 +00:00
"""Return true if location is valid."""
airly = Airly(api_key, client)
if use_nearest:
measurements = airly.create_measurements_session_nearest(
latitude=latitude, longitude=longitude, max_distance_km=5
)
else:
measurements = airly.create_measurements_session_point(
latitude=latitude, longitude=longitude
)
2020-12-17 18:34:40 +00:00
with async_timeout.timeout(10):
await measurements.update()
current = measurements.current
2020-12-17 18:34:40 +00:00
if current["indexes"][0]["description"] == NO_AIRLY_SENSORS:
return False
return True