Refactor Airly config flow (#44330)

pull/44347/head
Maciej Bieniek 2020-12-17 19:34:40 +01:00 committed by GitHub
parent 418304c702
commit 97894bd718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 45 deletions

View File

@ -5,7 +5,13 @@ import async_timeout
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
from homeassistant.const import (
CONF_API_KEY,
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
HTTP_UNAUTHORIZED,
)
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
@ -37,23 +43,24 @@ class AirlyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
f"{user_input[CONF_LATITUDE]}-{user_input[CONF_LONGITUDE]}"
)
self._abort_if_unique_id_configured()
api_key_valid = await self._test_api_key(websession, user_input["api_key"])
if not api_key_valid:
self._errors["base"] = "invalid_api_key"
else:
location_valid = await self._test_location(
try:
location_valid = await test_location(
websession,
user_input["api_key"],
user_input["latitude"],
user_input["longitude"],
)
except AirlyError as err:
if err.status_code == HTTP_UNAUTHORIZED:
self._errors["base"] = "invalid_api_key"
else:
if not location_valid:
self._errors["base"] = "wrong_location"
if not self._errors:
return self.async_create_entry(
title=user_input[CONF_NAME], data=user_input
)
if not self._errors:
return self.async_create_entry(
title=user_input[CONF_NAME], data=user_input
)
return self._show_config_form(
name=DEFAULT_NAME,
@ -81,31 +88,19 @@ class AirlyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
errors=self._errors,
)
async def _test_api_key(self, client, api_key):
"""Return true if api_key is valid."""
with async_timeout.timeout(10):
airly = Airly(api_key, client)
measurements = airly.create_measurements_session_point(
latitude=52.24131, longitude=20.99101
)
try:
await measurements.update()
except AirlyError:
return False
return True
async def test_location(client, api_key, latitude, longitude):
"""Return true if location is valid."""
airly = Airly(api_key, client)
measurements = airly.create_measurements_session_point(
latitude=latitude, longitude=longitude
)
async def _test_location(self, client, api_key, latitude, longitude):
"""Return true if location is valid."""
with async_timeout.timeout(10):
await measurements.update()
with async_timeout.timeout(10):
airly = Airly(api_key, client)
measurements = airly.create_measurements_session_point(
latitude=latitude, longitude=longitude
)
current = measurements.current
await measurements.update()
current = measurements.current
if current["indexes"][0]["description"] == NO_AIRLY_SENSORS:
return False
return True
if current["indexes"][0]["description"] == NO_AIRLY_SENSORS:
return False
return True

View File

@ -3,9 +3,6 @@ from homeassistant.components.airly.const import DOMAIN
from tests.common import MockConfigEntry, load_fixture
API_KEY_VALIDATION_URL = (
"https://airapi.airly.eu/v2/measurements/point?lat=52.241310&lng=20.991010"
)
API_POINT_URL = (
"https://airapi.airly.eu/v2/measurements/point?lat=123.000000&lng=456.000000"
)

View File

@ -12,7 +12,7 @@ from homeassistant.const import (
HTTP_UNAUTHORIZED,
)
from . import API_KEY_VALIDATION_URL, API_POINT_URL
from . import API_POINT_URL
from tests.common import MockConfigEntry, load_fixture, patch
@ -37,7 +37,7 @@ async def test_show_form(hass):
async def test_invalid_api_key(hass, aioclient_mock):
"""Test that errors are shown when API key is invalid."""
aioclient_mock.get(
API_KEY_VALIDATION_URL,
API_POINT_URL,
exc=AirlyError(
HTTP_UNAUTHORIZED, {"message": "Invalid authentication credentials"}
),
@ -52,9 +52,6 @@ async def test_invalid_api_key(hass, aioclient_mock):
async def test_invalid_location(hass, aioclient_mock):
"""Test that errors are shown when location is invalid."""
aioclient_mock.get(
API_KEY_VALIDATION_URL, text=load_fixture("airly_valid_station.json")
)
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_no_station.json"))
result = await hass.config_entries.flow.async_init(
@ -79,9 +76,6 @@ async def test_duplicate_error(hass, aioclient_mock):
async def test_create_entry(hass, aioclient_mock):
"""Test that the user step works."""
aioclient_mock.get(
API_KEY_VALIDATION_URL, text=load_fixture("airly_valid_station.json")
)
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_valid_station.json"))
with patch("homeassistant.components.airly.async_setup_entry", return_value=True):