2020-02-28 20:14:17 -07:00
|
|
|
"""Define tests for the AirVisual config flow."""
|
2023-01-04 13:05:37 -07:00
|
|
|
from unittest.mock import AsyncMock, patch
|
2021-01-01 22:31:56 +01:00
|
|
|
|
2022-11-05 11:58:47 -06:00
|
|
|
from pyairvisual.cloud_api import (
|
2021-02-01 14:38:03 -07:00
|
|
|
InvalidKeyError,
|
2022-07-16 14:04:44 -06:00
|
|
|
KeyExpiredError,
|
2021-02-01 14:38:03 -07:00
|
|
|
NotFoundError,
|
2022-07-16 14:04:44 -06:00
|
|
|
UnauthorizedError,
|
2021-02-01 14:38:03 -07:00
|
|
|
)
|
2022-11-05 11:58:47 -06:00
|
|
|
from pyairvisual.errors import AirVisualError
|
2022-01-19 11:52:24 -07:00
|
|
|
import pytest
|
2020-02-28 20:14:17 -07:00
|
|
|
|
|
|
|
from homeassistant import data_entry_flow
|
2022-12-19 10:48:36 -07:00
|
|
|
from homeassistant.components.airvisual import (
|
2021-02-01 14:38:03 -07:00
|
|
|
CONF_CITY,
|
2020-04-24 11:11:17 -06:00
|
|
|
CONF_INTEGRATION_TYPE,
|
2020-04-22 17:41:14 -06:00
|
|
|
DOMAIN,
|
2021-02-01 14:38:03 -07:00
|
|
|
INTEGRATION_TYPE_GEOGRAPHY_COORDS,
|
|
|
|
INTEGRATION_TYPE_GEOGRAPHY_NAME,
|
2020-04-22 17:41:14 -06:00
|
|
|
)
|
2021-04-25 12:27:40 +03:00
|
|
|
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
2023-01-04 13:05:37 -07:00
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_SHOW_ON_MAP
|
2023-02-09 16:09:13 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-01-04 13:05:37 -07:00
|
|
|
|
|
|
|
from .conftest import (
|
|
|
|
COORDS_CONFIG,
|
|
|
|
NAME_CONFIG,
|
|
|
|
TEST_CITY,
|
|
|
|
TEST_COUNTRY,
|
|
|
|
TEST_LATITUDE,
|
|
|
|
TEST_LONGITUDE,
|
|
|
|
TEST_STATE,
|
2020-03-10 14:16:25 -06:00
|
|
|
)
|
2020-02-28 20:14:17 -07:00
|
|
|
|
|
|
|
|
2022-01-19 11:52:24 -07:00
|
|
|
@pytest.mark.parametrize(
|
2023-01-04 13:05:37 -07:00
|
|
|
"integration_type,input_form_step,patched_method,config,entry_title",
|
2022-01-19 11:52:24 -07:00
|
|
|
[
|
|
|
|
(
|
2023-01-04 13:05:37 -07:00
|
|
|
INTEGRATION_TYPE_GEOGRAPHY_COORDS,
|
|
|
|
"geography_by_coords",
|
|
|
|
"nearest_city",
|
|
|
|
COORDS_CONFIG,
|
|
|
|
f"Cloud API ({TEST_LATITUDE}, {TEST_LONGITUDE})",
|
2022-07-16 14:04:44 -06:00
|
|
|
),
|
2022-01-19 11:52:24 -07:00
|
|
|
(
|
|
|
|
INTEGRATION_TYPE_GEOGRAPHY_NAME,
|
2023-01-04 13:05:37 -07:00
|
|
|
"geography_by_name",
|
|
|
|
"city",
|
|
|
|
NAME_CONFIG,
|
|
|
|
f"Cloud API ({TEST_CITY}, {TEST_STATE}, {TEST_COUNTRY})",
|
2022-01-19 11:52:24 -07:00
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
2023-01-04 13:05:37 -07:00
|
|
|
"response,errors",
|
2022-01-19 11:52:24 -07:00
|
|
|
[
|
2023-01-04 13:05:37 -07:00
|
|
|
(AsyncMock(side_effect=AirVisualError), {"base": "unknown"}),
|
|
|
|
(AsyncMock(side_effect=InvalidKeyError), {CONF_API_KEY: "invalid_api_key"}),
|
|
|
|
(AsyncMock(side_effect=KeyExpiredError), {CONF_API_KEY: "invalid_api_key"}),
|
|
|
|
(AsyncMock(side_effect=NotFoundError), {CONF_CITY: "location_not_found"}),
|
|
|
|
(AsyncMock(side_effect=UnauthorizedError), {CONF_API_KEY: "invalid_api_key"}),
|
2022-01-19 11:52:24 -07:00
|
|
|
],
|
|
|
|
)
|
2023-01-04 13:05:37 -07:00
|
|
|
async def test_create_entry(
|
2023-02-09 16:09:13 +01:00
|
|
|
hass: HomeAssistant,
|
2023-01-04 13:05:37 -07:00
|
|
|
cloud_api,
|
|
|
|
config,
|
|
|
|
entry_title,
|
|
|
|
errors,
|
|
|
|
input_form_step,
|
|
|
|
integration_type,
|
|
|
|
mock_pyairvisual,
|
|
|
|
patched_method,
|
|
|
|
response,
|
2023-02-09 16:09:13 +01:00
|
|
|
) -> None:
|
2023-01-04 13:05:37 -07:00
|
|
|
"""Test creating a config entry."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
2022-12-30 14:47:41 -07:00
|
|
|
)
|
2023-01-04 13:05:37 -07:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "user"
|
2022-12-30 14:47:41 -07:00
|
|
|
|
2023-01-04 13:05:37 -07:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}, data={"type": integration_type}
|
2022-12-30 14:47:41 -07:00
|
|
|
)
|
2023-01-04 13:05:37 -07:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
|
|
assert result["step_id"] == input_form_step
|
2022-12-30 14:47:41 -07:00
|
|
|
|
2023-01-04 13:05:37 -07:00
|
|
|
# Test errors that can arise:
|
|
|
|
with patch.object(cloud_api.air_quality, patched_method, response):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=config
|
|
|
|
)
|
2022-07-07 18:57:36 +02:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2023-01-04 13:05:37 -07:00
|
|
|
assert result["errors"] == errors
|
2020-03-10 14:16:25 -06:00
|
|
|
|
2023-01-04 13:05:37 -07:00
|
|
|
# Test that we can recover and finish the flow after errors occur:
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=config
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
|
|
|
assert result["title"] == entry_title
|
|
|
|
assert result["data"] == {**config, CONF_INTEGRATION_TYPE: integration_type}
|
2020-03-10 14:16:25 -06:00
|
|
|
|
|
|
|
|
2023-02-09 16:09:13 +01:00
|
|
|
async def test_duplicate_error(hass: HomeAssistant, config, setup_config_entry) -> None:
|
2023-01-04 13:05:37 -07:00
|
|
|
"""Test that errors are shown when duplicate entries are added."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "user"
|
2020-03-10 14:16:25 -06:00
|
|
|
|
2022-01-19 11:52:24 -07:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_USER},
|
|
|
|
data={"type": INTEGRATION_TYPE_GEOGRAPHY_COORDS},
|
|
|
|
)
|
2023-01-04 13:05:37 -07:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "geography_by_coords"
|
|
|
|
|
2022-01-19 11:52:24 -07:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input=config
|
|
|
|
)
|
2023-01-04 13:05:37 -07:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "already_configured"
|
2022-01-19 11:52:24 -07:00
|
|
|
|
2020-02-28 20:14:17 -07:00
|
|
|
|
2023-02-09 16:09:13 +01:00
|
|
|
async def test_options_flow(
|
|
|
|
hass: HomeAssistant, config_entry, setup_config_entry
|
|
|
|
) -> None:
|
2023-01-04 13:05:37 -07:00
|
|
|
"""Test config flow options."""
|
|
|
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "init"
|
2021-02-01 14:38:03 -07:00
|
|
|
|
2023-01-04 13:05:37 -07:00
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_SHOW_ON_MAP: False}
|
2022-01-19 11:52:24 -07:00
|
|
|
)
|
2022-07-07 18:57:36 +02:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2023-01-04 13:05:37 -07:00
|
|
|
assert config_entry.options == {CONF_SHOW_ON_MAP: False}
|
2021-02-01 14:38:03 -07:00
|
|
|
|
|
|
|
|
2023-02-09 16:09:13 +01:00
|
|
|
async def test_step_reauth(
|
|
|
|
hass: HomeAssistant, config_entry, setup_config_entry
|
|
|
|
) -> None:
|
2022-01-19 11:52:24 -07:00
|
|
|
"""Test that the reauth step works."""
|
2020-09-09 16:41:07 -06:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2022-01-19 11:52:24 -07:00
|
|
|
DOMAIN, context={"source": SOURCE_REAUTH}, data=config_entry.data
|
2020-09-09 16:41:07 -06:00
|
|
|
)
|
2023-01-04 13:05:37 -07:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-09-09 16:41:07 -06:00
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
2022-07-07 18:57:36 +02:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-09-09 16:41:07 -06:00
|
|
|
assert result["step_id"] == "reauth_confirm"
|
|
|
|
|
2021-11-15 15:30:26 -07:00
|
|
|
new_api_key = "defgh67890"
|
|
|
|
|
2023-01-04 13:05:37 -07:00
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_API_KEY: new_api_key}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "reauth_successful"
|
2020-04-22 17:41:14 -06:00
|
|
|
|
2020-09-09 16:41:07 -06:00
|
|
|
assert len(hass.config_entries.async_entries()) == 1
|
2021-11-15 15:30:26 -07:00
|
|
|
assert hass.config_entries.async_entries()[0].data[CONF_API_KEY] == new_api_key
|