mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Refactor Airly config flow (#44330)
This commit is contained in:
parent
418304c702
commit
97894bd718
@ -5,7 +5,13 @@ import async_timeout
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
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
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
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]}"
|
f"{user_input[CONF_LATITUDE]}-{user_input[CONF_LONGITUDE]}"
|
||||||
)
|
)
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured()
|
||||||
api_key_valid = await self._test_api_key(websession, user_input["api_key"])
|
try:
|
||||||
if not api_key_valid:
|
location_valid = await test_location(
|
||||||
self._errors["base"] = "invalid_api_key"
|
|
||||||
else:
|
|
||||||
location_valid = await self._test_location(
|
|
||||||
websession,
|
websession,
|
||||||
user_input["api_key"],
|
user_input["api_key"],
|
||||||
user_input["latitude"],
|
user_input["latitude"],
|
||||||
user_input["longitude"],
|
user_input["longitude"],
|
||||||
)
|
)
|
||||||
|
except AirlyError as err:
|
||||||
|
if err.status_code == HTTP_UNAUTHORIZED:
|
||||||
|
self._errors["base"] = "invalid_api_key"
|
||||||
|
else:
|
||||||
if not location_valid:
|
if not location_valid:
|
||||||
self._errors["base"] = "wrong_location"
|
self._errors["base"] = "wrong_location"
|
||||||
|
|
||||||
if not self._errors:
|
if not self._errors:
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=user_input[CONF_NAME], data=user_input
|
title=user_input[CONF_NAME], data=user_input
|
||||||
)
|
)
|
||||||
|
|
||||||
return self._show_config_form(
|
return self._show_config_form(
|
||||||
name=DEFAULT_NAME,
|
name=DEFAULT_NAME,
|
||||||
@ -81,31 +88,19 @@ class AirlyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
errors=self._errors,
|
errors=self._errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _test_api_key(self, client, api_key):
|
|
||||||
"""Return true if api_key is valid."""
|
|
||||||
|
|
||||||
with async_timeout.timeout(10):
|
async def test_location(client, api_key, latitude, longitude):
|
||||||
airly = Airly(api_key, client)
|
"""Return true if location is valid."""
|
||||||
measurements = airly.create_measurements_session_point(
|
airly = Airly(api_key, client)
|
||||||
latitude=52.24131, longitude=20.99101
|
measurements = airly.create_measurements_session_point(
|
||||||
)
|
latitude=latitude, longitude=longitude
|
||||||
try:
|
)
|
||||||
await measurements.update()
|
|
||||||
except AirlyError:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
async def _test_location(self, client, api_key, latitude, longitude):
|
with async_timeout.timeout(10):
|
||||||
"""Return true if location is valid."""
|
await measurements.update()
|
||||||
|
|
||||||
with async_timeout.timeout(10):
|
current = measurements.current
|
||||||
airly = Airly(api_key, client)
|
|
||||||
measurements = airly.create_measurements_session_point(
|
|
||||||
latitude=latitude, longitude=longitude
|
|
||||||
)
|
|
||||||
|
|
||||||
await measurements.update()
|
if current["indexes"][0]["description"] == NO_AIRLY_SENSORS:
|
||||||
current = measurements.current
|
return False
|
||||||
if current["indexes"][0]["description"] == NO_AIRLY_SENSORS:
|
return True
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
@ -3,9 +3,6 @@ from homeassistant.components.airly.const import DOMAIN
|
|||||||
|
|
||||||
from tests.common import MockConfigEntry, load_fixture
|
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 = (
|
API_POINT_URL = (
|
||||||
"https://airapi.airly.eu/v2/measurements/point?lat=123.000000&lng=456.000000"
|
"https://airapi.airly.eu/v2/measurements/point?lat=123.000000&lng=456.000000"
|
||||||
)
|
)
|
||||||
|
@ -12,7 +12,7 @@ from homeassistant.const import (
|
|||||||
HTTP_UNAUTHORIZED,
|
HTTP_UNAUTHORIZED,
|
||||||
)
|
)
|
||||||
|
|
||||||
from . import API_KEY_VALIDATION_URL, API_POINT_URL
|
from . import API_POINT_URL
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, load_fixture, patch
|
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):
|
async def test_invalid_api_key(hass, aioclient_mock):
|
||||||
"""Test that errors are shown when API key is invalid."""
|
"""Test that errors are shown when API key is invalid."""
|
||||||
aioclient_mock.get(
|
aioclient_mock.get(
|
||||||
API_KEY_VALIDATION_URL,
|
API_POINT_URL,
|
||||||
exc=AirlyError(
|
exc=AirlyError(
|
||||||
HTTP_UNAUTHORIZED, {"message": "Invalid authentication credentials"}
|
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):
|
async def test_invalid_location(hass, aioclient_mock):
|
||||||
"""Test that errors are shown when location is invalid."""
|
"""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"))
|
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_no_station.json"))
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
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):
|
async def test_create_entry(hass, aioclient_mock):
|
||||||
"""Test that the user step works."""
|
"""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"))
|
aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_valid_station.json"))
|
||||||
|
|
||||||
with patch("homeassistant.components.airly.async_setup_entry", return_value=True):
|
with patch("homeassistant.components.airly.async_setup_entry", return_value=True):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user