2020-12-30 19:25:57 +00:00
|
|
|
"""Test the AirNow config flow."""
|
2024-03-08 13:50:25 +00:00
|
|
|
|
2023-10-05 11:23:59 +00:00
|
|
|
from unittest.mock import AsyncMock, patch
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2023-07-26 19:30:25 +00:00
|
|
|
from pyairnow.errors import AirNowError, EmptyResponseError, InvalidKeyError
|
2022-10-08 22:32:51 +00:00
|
|
|
import pytest
|
2020-12-30 19:25:57 +00:00
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
from homeassistant import config_entries
|
2020-12-30 19:25:57 +00:00
|
|
|
from homeassistant.components.airnow.const import DOMAIN
|
2023-08-28 19:21:52 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_RADIUS
|
2023-02-09 15:09:13 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-04-02 21:22:05 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2020-12-30 19:25:57 +00:00
|
|
|
|
2023-08-28 19:21:52 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2020-12-30 19:25:57 +00:00
|
|
|
|
2023-08-28 19:21:52 +00:00
|
|
|
|
|
|
|
async def test_form(hass: HomeAssistant, config, options, setup_airnow) -> None:
|
2020-12-30 19:25:57 +00:00
|
|
|
"""Test we get the form."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2020-12-30 19:25:57 +00:00
|
|
|
assert result["errors"] == {}
|
|
|
|
|
2022-10-08 22:32:51 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(result["flow_id"], config)
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
2022-10-08 22:32:51 +00:00
|
|
|
assert result2["data"] == config
|
2023-08-28 19:21:52 +00:00
|
|
|
assert result2["options"] == options
|
2020-12-30 19:25:57 +00:00
|
|
|
|
|
|
|
|
2022-10-08 22:32:51 +00:00
|
|
|
@pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=InvalidKeyError)])
|
2023-02-09 15:09:13 +00:00
|
|
|
async def test_form_invalid_auth(hass: HomeAssistant, config, setup_airnow) -> None:
|
2020-12-30 19:25:57 +00:00
|
|
|
"""Test we handle invalid auth."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2022-10-08 22:32:51 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(result["flow_id"], config)
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2020-12-30 19:25:57 +00:00
|
|
|
assert result2["errors"] == {"base": "invalid_auth"}
|
|
|
|
|
|
|
|
|
2022-10-08 22:32:51 +00:00
|
|
|
@pytest.mark.parametrize("data", [{}])
|
2023-02-09 15:09:13 +00:00
|
|
|
async def test_form_invalid_location(hass: HomeAssistant, config, setup_airnow) -> None:
|
2020-12-30 19:25:57 +00:00
|
|
|
"""Test we handle invalid location."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2022-10-08 22:32:51 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(result["flow_id"], config)
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2020-12-30 19:25:57 +00:00
|
|
|
assert result2["errors"] == {"base": "invalid_location"}
|
|
|
|
|
|
|
|
|
2022-10-08 22:32:51 +00:00
|
|
|
@pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=AirNowError)])
|
2023-02-09 15:09:13 +00:00
|
|
|
async def test_form_cannot_connect(hass: HomeAssistant, config, setup_airnow) -> None:
|
2020-12-30 19:25:57 +00:00
|
|
|
"""Test we handle cannot connect error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2022-10-08 22:32:51 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(result["flow_id"], config)
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2020-12-30 19:25:57 +00:00
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
|
|
|
|
|
|
|
|
2023-07-26 19:30:25 +00:00
|
|
|
@pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=EmptyResponseError)])
|
|
|
|
async def test_form_empty_result(hass: HomeAssistant, config, setup_airnow) -> None:
|
|
|
|
"""Test we handle empty response error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(result["flow_id"], config)
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2023-07-26 19:30:25 +00:00
|
|
|
assert result2["errors"] == {"base": "invalid_location"}
|
|
|
|
|
|
|
|
|
2022-10-08 22:32:51 +00:00
|
|
|
@pytest.mark.parametrize("mock_api_get", [AsyncMock(side_effect=RuntimeError)])
|
2023-02-09 15:09:13 +00:00
|
|
|
async def test_form_unexpected(hass: HomeAssistant, config, setup_airnow) -> None:
|
2020-12-30 19:25:57 +00:00
|
|
|
"""Test we handle an unexpected error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2022-10-08 22:32:51 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(result["flow_id"], config)
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2020-12-30 19:25:57 +00:00
|
|
|
assert result2["errors"] == {"base": "unknown"}
|
|
|
|
|
|
|
|
|
2023-02-09 15:09:13 +00:00
|
|
|
async def test_entry_already_exists(hass: HomeAssistant, config, config_entry) -> None:
|
2020-12-30 19:25:57 +00:00
|
|
|
"""Test that the form aborts if the Lat/Lng is already configured."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2022-10-08 22:32:51 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(result["flow_id"], config)
|
2024-04-03 07:21:17 +00:00
|
|
|
assert result2["type"] is FlowResultType.ABORT
|
2020-12-30 19:25:57 +00:00
|
|
|
assert result2["reason"] == "already_configured"
|
2023-08-28 19:21:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_config_migration_v2(hass: HomeAssistant, setup_airnow) -> None:
|
|
|
|
"""Test that the config migration from Version 1 to Version 2 works."""
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
version=1,
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="AirNow",
|
|
|
|
data={
|
|
|
|
CONF_API_KEY: "1234",
|
|
|
|
CONF_LATITUDE: 33.6,
|
|
|
|
CONF_LONGITUDE: -118.1,
|
|
|
|
CONF_RADIUS: 25,
|
|
|
|
},
|
|
|
|
source=config_entries.SOURCE_USER,
|
|
|
|
options={CONF_RADIUS: 10},
|
|
|
|
unique_id="1234",
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert config_entry.version == 2
|
|
|
|
assert not config_entry.data.get(CONF_RADIUS)
|
|
|
|
assert config_entry.options.get(CONF_RADIUS) == 25
|
|
|
|
|
|
|
|
|
|
|
|
async def test_options_flow(hass: HomeAssistant, setup_airnow) -> None:
|
|
|
|
"""Test that the options flow works."""
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
version=2,
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="AirNow",
|
|
|
|
data={
|
|
|
|
CONF_API_KEY: "1234",
|
|
|
|
CONF_LATITUDE: 33.6,
|
|
|
|
CONF_LONGITUDE: -118.1,
|
|
|
|
},
|
|
|
|
source=config_entries.SOURCE_USER,
|
|
|
|
options={CONF_RADIUS: 10},
|
|
|
|
unique_id="1234",
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2023-08-28 19:21:52 +00:00
|
|
|
assert result["step_id"] == "init"
|
|
|
|
|
2023-10-05 11:23:59 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.airnow.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={CONF_RADIUS: 25},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2023-08-28 19:21:52 +00:00
|
|
|
|
2024-04-02 21:22:05 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2023-08-28 19:21:52 +00:00
|
|
|
assert config_entry.options == {
|
|
|
|
CONF_RADIUS: 25,
|
|
|
|
}
|
2023-10-05 11:23:59 +00:00
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|