From f89919ebac470f7dd915a624d018715916cc7ea1 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Tue, 1 Sep 2026 16:30:35 +0200 Subject: [PATCH] Add recovery path to the Airly config flow tests (#181011) --- .../components/airly/quality_scale.yaml | 4 +- tests/components/airly/test_config_flow.py | 101 ++++++++++++------ 2 files changed, 67 insertions(+), 38 deletions(-) diff --git a/homeassistant/components/airly/quality_scale.yaml b/homeassistant/components/airly/quality_scale.yaml index a369dc3fee63..d63e6312017b 100644 --- a/homeassistant/components/airly/quality_scale.yaml +++ b/homeassistant/components/airly/quality_scale.yaml @@ -6,9 +6,7 @@ rules: appropriate-polling: done brands: done common-modules: done - config-flow-test-coverage: - status: todo - comment: The config flow test should either result in an entry being created or an abort. + config-flow-test-coverage: done config-flow: done dependency-transparency: done docs-actions: diff --git a/tests/components/airly/test_config_flow.py b/tests/components/airly/test_config_flow.py index 6da67ebf321f..756858ca168d 100644 --- a/tests/components/airly/test_config_flow.py +++ b/tests/components/airly/test_config_flow.py @@ -1,8 +1,11 @@ """Define tests for the Airly config flow.""" +from collections.abc import Generator from http import HTTPStatus +from unittest.mock import AsyncMock from airly.exceptions import AirlyError +import pytest from homeassistant.components.airly.const import CONF_USE_NEAREST, DEFAULT_NAME, DOMAIN from homeassistant.config_entries import SOURCE_USER @@ -22,14 +25,13 @@ CONFIG = { } -async def test_show_form(hass: HomeAssistant) -> None: - """Test that the form is served with no input.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER} - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "user" +@pytest.fixture(autouse=True) +def mock_setup_entry() -> Generator[AsyncMock]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.airly.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry async def test_invalid_api_key( @@ -56,6 +58,22 @@ async def test_invalid_api_key( assert result["errors"] == {"base": "invalid_api_key"} + aioclient_mock.clear_requests() + aioclient_mock.get( + API_POINT_URL, text=await async_load_fixture(hass, "valid_station.json", DOMAIN) + ) + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input=CONFIG + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == DEFAULT_NAME + assert result["data"][CONF_LATITUDE] == CONFIG[CONF_LATITUDE] + assert result["data"][CONF_LONGITUDE] == CONFIG[CONF_LONGITUDE] + assert result["data"][CONF_API_KEY] == CONFIG[CONF_API_KEY] + assert result["data"][CONF_USE_NEAREST] is False + async def test_invalid_location( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker @@ -83,6 +101,22 @@ async def test_invalid_location( assert result["errors"] == {"base": "wrong_location"} + aioclient_mock.clear_requests() + aioclient_mock.get( + API_POINT_URL, text=await async_load_fixture(hass, "valid_station.json", DOMAIN) + ) + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input=CONFIG + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == DEFAULT_NAME + assert result["data"][CONF_LATITUDE] == CONFIG[CONF_LATITUDE] + assert result["data"][CONF_LONGITUDE] == CONFIG[CONF_LONGITUDE] + assert result["data"][CONF_API_KEY] == CONFIG[CONF_API_KEY] + assert result["data"][CONF_USE_NEAREST] is False + async def test_invalid_location_for_point_and_nearest( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker @@ -97,17 +131,16 @@ async def test_invalid_location_for_point_and_nearest( API_NEAREST_URL, text=await async_load_fixture(hass, "no_station.json", DOMAIN) ) - with patch("homeassistant.components.airly.async_setup_entry", return_value=True): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER} - ) + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "user" + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" - result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input=CONFIG - ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input=CONFIG + ) assert result["type"] is FlowResultType.ABORT assert result["reason"] == "wrong_location" @@ -145,17 +178,16 @@ async def test_create_entry( API_POINT_URL, text=await async_load_fixture(hass, "valid_station.json", DOMAIN) ) - with patch("homeassistant.components.airly.async_setup_entry", return_value=True): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER} - ) + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "user" + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" - result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input=CONFIG - ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input=CONFIG + ) assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == DEFAULT_NAME @@ -179,17 +211,16 @@ async def test_create_entry_with_nearest_method( text=await async_load_fixture(hass, "valid_station.json", DOMAIN), ) - with patch("homeassistant.components.airly.async_setup_entry", return_value=True): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER} - ) + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "user" + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" - result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input=CONFIG - ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input=CONFIG + ) assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == DEFAULT_NAME