Abort config flow if Airly measuring station does not exist (#85652)

* Abort if there is no sensor in the area

* Add test

* Increase test coverage
This commit is contained in:
Maciej Bieniek 2023-01-13 14:35:52 +01:00 committed by GitHub
parent 87aacf9fbe
commit 6baa905448
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 3 deletions

View File

@ -46,7 +46,7 @@ class AirlyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
user_input["longitude"],
)
if not location_point_valid:
await test_location(
location_nearest_valid = await test_location(
websession,
user_input["api_key"],
user_input["latitude"],
@ -60,6 +60,8 @@ class AirlyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
errors["base"] = "wrong_location"
else:
if not location_point_valid:
if not location_nearest_valid:
return self.async_abort(reason="wrong_location")
use_nearest = True
return self.async_create_entry(
title=user_input[CONF_NAME],

View File

@ -16,7 +16,8 @@
"invalid_api_key": "[%key:common::config_flow::error::invalid_api_key%]"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_location%]"
"already_configured": "[%key:common::config_flow::abort::already_configured_location%]",
"wrong_location": "No Airly measuring stations in this area."
}
},
"system_health": {

View File

@ -62,6 +62,22 @@ async def test_invalid_location(hass, aioclient_mock):
assert result["errors"] == {"base": "wrong_location"}
async def test_invalid_location_for_point_and_nearest(hass, aioclient_mock):
"""Test an abort when the location is wrong for the point and nearest methods."""
aioclient_mock.get(API_POINT_URL, text=load_fixture("no_station.json", "airly"))
aioclient_mock.get(API_NEAREST_URL, text=load_fixture("no_station.json", "airly"))
with patch("homeassistant.components.airly.async_setup_entry", return_value=True):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG
)
assert result["type"] == data_entry_flow.FlowResultType.ABORT
assert result["reason"] == "wrong_location"
async def test_duplicate_error(hass, aioclient_mock):
"""Test that errors are shown when duplicates are added."""
aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))

View File

@ -1,5 +1,8 @@
"""Test sensor of Airly integration."""
from datetime import timedelta
from http import HTTPStatus
from airly.exceptions import AirlyError
from homeassistant.components.airly.sensor import ATTRIBUTION
from homeassistant.components.sensor import (
@ -195,7 +198,9 @@ async def test_availability(hass, aioclient_mock):
assert state.state == "68.3"
aioclient_mock.clear_requests()
aioclient_mock.get(API_POINT_URL, exc=ConnectionError())
aioclient_mock.get(
API_POINT_URL, exc=AirlyError(HTTPStatus.NOT_FOUND, {"message": "Not found"})
)
future = utcnow() + timedelta(minutes=60)
async_fire_time_changed(hass, future)
await hass.async_block_till_done()