From 6baa905448dbb80d857c109a9663fe0cc65ee167 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Fri, 13 Jan 2023 14:35:52 +0100 Subject: [PATCH] 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 --- homeassistant/components/airly/config_flow.py | 4 +++- homeassistant/components/airly/strings.json | 3 ++- tests/components/airly/test_config_flow.py | 16 ++++++++++++++++ tests/components/airly/test_sensor.py | 7 ++++++- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/airly/config_flow.py b/homeassistant/components/airly/config_flow.py index 10e4990ee05..5d41116eaa1 100644 --- a/homeassistant/components/airly/config_flow.py +++ b/homeassistant/components/airly/config_flow.py @@ -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], diff --git a/homeassistant/components/airly/strings.json b/homeassistant/components/airly/strings.json index 77f965b64c2..4f95f26afc0 100644 --- a/homeassistant/components/airly/strings.json +++ b/homeassistant/components/airly/strings.json @@ -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": { diff --git a/tests/components/airly/test_config_flow.py b/tests/components/airly/test_config_flow.py index b7c2a65812e..dc20888f532 100644 --- a/tests/components/airly/test_config_flow.py +++ b/tests/components/airly/test_config_flow.py @@ -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")) diff --git a/tests/components/airly/test_sensor.py b/tests/components/airly/test_sensor.py index c95d2d895fd..0d333fba756 100644 --- a/tests/components/airly/test_sensor.py +++ b/tests/components/airly/test_sensor.py @@ -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()