mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Renovate PurpleAir tests (#84894)
This commit is contained in:
parent
9af17fa5a0
commit
0a77232444
@ -6,24 +6,29 @@ from aiopurpleair.models.sensors import GetSensorsResponse
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.purpleair import DOMAIN
|
from homeassistant.components.purpleair import DOMAIN
|
||||||
from homeassistant.setup import async_setup_component
|
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, load_fixture
|
from tests.common import MockConfigEntry, load_fixture
|
||||||
|
|
||||||
|
TEST_API_KEY = "abcde12345"
|
||||||
|
TEST_SENSOR_INDEX1 = 123456
|
||||||
|
TEST_SENSOR_INDEX2 = 567890
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="api")
|
@pytest.fixture(name="api")
|
||||||
def api_fixture(check_api_key, get_nearby_sensors, get_sensors):
|
def api_fixture(get_sensors_response):
|
||||||
"""Define a fixture to return a mocked aiopurple API object."""
|
"""Define a fixture to return a mocked aiopurple API object."""
|
||||||
api = Mock(async_check_api_key=check_api_key)
|
return Mock(
|
||||||
api.sensors.async_get_nearby_sensors = get_nearby_sensors
|
async_check_api_key=AsyncMock(),
|
||||||
api.sensors.async_get_sensors = get_sensors
|
sensors=Mock(
|
||||||
return api
|
async_get_nearby_sensors=AsyncMock(
|
||||||
|
return_value=[
|
||||||
|
NearbySensorResult(sensor=sensor, distance=1.0)
|
||||||
@pytest.fixture(name="check_api_key")
|
for sensor in get_sensors_response.data.values()
|
||||||
def check_api_key_fixture():
|
]
|
||||||
"""Define a fixture to mock the method to check an API key's validity."""
|
),
|
||||||
return AsyncMock()
|
async_get_sensors=AsyncMock(return_value=get_sensors_response),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="config_entry")
|
@pytest.fixture(name="config_entry")
|
||||||
@ -32,7 +37,7 @@ def config_entry_fixture(hass, config_entry_data, config_entry_options):
|
|||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
title="abcde",
|
title="abcde",
|
||||||
unique_id="abcde12345",
|
unique_id=TEST_API_KEY,
|
||||||
data=config_entry_data,
|
data=config_entry_data,
|
||||||
options=config_entry_options,
|
options=config_entry_options,
|
||||||
)
|
)
|
||||||
@ -44,7 +49,7 @@ def config_entry_fixture(hass, config_entry_data, config_entry_options):
|
|||||||
def config_entry_data_fixture():
|
def config_entry_data_fixture():
|
||||||
"""Define a config entry data fixture."""
|
"""Define a config entry data fixture."""
|
||||||
return {
|
return {
|
||||||
"api_key": "abcde12345",
|
"api_key": TEST_API_KEY,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -52,27 +57,10 @@ def config_entry_data_fixture():
|
|||||||
def config_entry_options_fixture():
|
def config_entry_options_fixture():
|
||||||
"""Define a config entry options fixture."""
|
"""Define a config entry options fixture."""
|
||||||
return {
|
return {
|
||||||
"sensor_indices": [123456],
|
"sensor_indices": [TEST_SENSOR_INDEX1],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="get_nearby_sensors")
|
|
||||||
def get_nearby_sensors_fixture(get_sensors_response):
|
|
||||||
"""Define a mocked API.sensors.async_get_nearby_sensors."""
|
|
||||||
return AsyncMock(
|
|
||||||
return_value=[
|
|
||||||
NearbySensorResult(sensor=sensor, distance=1.0)
|
|
||||||
for sensor in get_sensors_response.data.values()
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="get_sensors")
|
|
||||||
def get_sensors_fixture(get_sensors_response):
|
|
||||||
"""Define a mocked API.sensors.async_get_sensors."""
|
|
||||||
return AsyncMock(return_value=get_sensors_response)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="get_sensors_response", scope="package")
|
@pytest.fixture(name="get_sensors_response", scope="package")
|
||||||
def get_sensors_response_fixture():
|
def get_sensors_response_fixture():
|
||||||
"""Define a fixture to mock an aiopurpleair GetSensorsResponse object."""
|
"""Define a fixture to mock an aiopurpleair GetSensorsResponse object."""
|
||||||
@ -81,12 +69,18 @@ def get_sensors_response_fixture():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="setup_purpleair")
|
@pytest.fixture(name="mock_aiopurpleair")
|
||||||
async def setup_purpleair_fixture(hass, api, config_entry_data):
|
async def mock_aiopurpleair_fixture(api):
|
||||||
"""Define a fixture to set up PurpleAir."""
|
"""Define a fixture to patch aiopurpleair."""
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.purpleair.config_flow.API", return_value=api
|
"homeassistant.components.purpleair.config_flow.API", return_value=api
|
||||||
), patch("homeassistant.components.purpleair.coordinator.API", return_value=api):
|
), patch("homeassistant.components.purpleair.coordinator.API", return_value=api):
|
||||||
assert await async_setup_component(hass, DOMAIN, config_entry_data)
|
|
||||||
await hass.async_block_till_done()
|
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(name="setup_config_entry")
|
||||||
|
async def setup_config_entry_fixture(hass, config_entry, mock_aiopurpleair):
|
||||||
|
"""Define a fixture to set up purpleair."""
|
||||||
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
yield
|
||||||
|
@ -9,14 +9,10 @@ from homeassistant.components.purpleair import DOMAIN
|
|||||||
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
|
|
||||||
|
from .conftest import TEST_API_KEY, TEST_SENSOR_INDEX1, TEST_SENSOR_INDEX2
|
||||||
|
|
||||||
async def test_duplicate_error(hass, config_entry, setup_purpleair):
|
TEST_LATITUDE = 51.5285582
|
||||||
"""Test that the proper error is shown when adding a duplicate config entry."""
|
TEST_LONGITUDE = -0.2416796
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": SOURCE_USER}, data={"api_key": "abcde12345"}
|
|
||||||
)
|
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
|
||||||
assert result["reason"] == "already_configured"
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@ -42,7 +38,7 @@ async def test_create_entry_by_coordinates(
|
|||||||
check_api_key_mock,
|
check_api_key_mock,
|
||||||
get_nearby_sensors_errors,
|
get_nearby_sensors_errors,
|
||||||
get_nearby_sensors_mock,
|
get_nearby_sensors_mock,
|
||||||
setup_purpleair,
|
mock_aiopurpleair,
|
||||||
):
|
):
|
||||||
"""Test creating an entry by entering a latitude/longitude (including errors)."""
|
"""Test creating an entry by entering a latitude/longitude (including errors)."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
@ -54,13 +50,13 @@ async def test_create_entry_by_coordinates(
|
|||||||
# Test errors that can arise when checking the API key:
|
# Test errors that can arise when checking the API key:
|
||||||
with patch.object(api, "async_check_api_key", check_api_key_mock):
|
with patch.object(api, "async_check_api_key", check_api_key_mock):
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], user_input={"api_key": "abcde12345"}
|
result["flow_id"], user_input={"api_key": TEST_API_KEY}
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||||
assert result["errors"] == check_api_key_errors
|
assert result["errors"] == check_api_key_errors
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], user_input={"api_key": "abcde12345"}
|
result["flow_id"], user_input={"api_key": TEST_API_KEY}
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||||
assert result["step_id"] == "by_coordinates"
|
assert result["step_id"] == "by_coordinates"
|
||||||
@ -70,8 +66,8 @@ async def test_create_entry_by_coordinates(
|
|||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
"latitude": 51.5285582,
|
"latitude": TEST_LATITUDE,
|
||||||
"longitude": -0.2416796,
|
"longitude": TEST_LONGITUDE,
|
||||||
"distance": 5,
|
"distance": 5,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -81,8 +77,8 @@ async def test_create_entry_by_coordinates(
|
|||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
"latitude": 51.5285582,
|
"latitude": TEST_LATITUDE,
|
||||||
"longitude": -0.2416796,
|
"longitude": TEST_LONGITUDE,
|
||||||
"distance": 5,
|
"distance": 5,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -92,19 +88,28 @@ async def test_create_entry_by_coordinates(
|
|||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
"sensor_index": "123456",
|
"sensor_index": str(TEST_SENSOR_INDEX1),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == "abcde"
|
assert result["title"] == "abcde"
|
||||||
assert result["data"] == {
|
assert result["data"] == {
|
||||||
"api_key": "abcde12345",
|
"api_key": TEST_API_KEY,
|
||||||
}
|
}
|
||||||
assert result["options"] == {
|
assert result["options"] == {
|
||||||
"sensor_indices": [123456],
|
"sensor_indices": [TEST_SENSOR_INDEX1],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async def test_duplicate_error(hass, config_entry, setup_config_entry):
|
||||||
|
"""Test that the proper error is shown when adding a duplicate config entry."""
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN, context={"source": SOURCE_USER}, data={"api_key": TEST_API_KEY}
|
||||||
|
)
|
||||||
|
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"check_api_key_mock,check_api_key_errors",
|
"check_api_key_mock,check_api_key_errors",
|
||||||
[
|
[
|
||||||
@ -114,7 +119,12 @@ async def test_create_entry_by_coordinates(
|
|||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_reauth(
|
async def test_reauth(
|
||||||
hass, api, check_api_key_errors, check_api_key_mock, config_entry, setup_purpleair
|
hass,
|
||||||
|
api,
|
||||||
|
check_api_key_errors,
|
||||||
|
check_api_key_mock,
|
||||||
|
config_entry,
|
||||||
|
setup_config_entry,
|
||||||
):
|
):
|
||||||
"""Test re-auth (including errors)."""
|
"""Test re-auth (including errors)."""
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
@ -124,7 +134,7 @@ async def test_reauth(
|
|||||||
"entry_id": config_entry.entry_id,
|
"entry_id": config_entry.entry_id,
|
||||||
"unique_id": config_entry.unique_id,
|
"unique_id": config_entry.unique_id,
|
||||||
},
|
},
|
||||||
data={"api_key": "abcde12345"},
|
data={"api_key": TEST_API_KEY},
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
||||||
assert result["step_id"] == "reauth_confirm"
|
assert result["step_id"] == "reauth_confirm"
|
||||||
@ -160,7 +170,7 @@ async def test_options_add_sensor(
|
|||||||
config_entry,
|
config_entry,
|
||||||
get_nearby_sensors_errors,
|
get_nearby_sensors_errors,
|
||||||
get_nearby_sensors_mock,
|
get_nearby_sensors_mock,
|
||||||
setup_purpleair,
|
setup_config_entry,
|
||||||
):
|
):
|
||||||
"""Test adding a sensor via the options flow (including errors)."""
|
"""Test adding a sensor via the options flow (including errors)."""
|
||||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||||
@ -178,8 +188,8 @@ async def test_options_add_sensor(
|
|||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
"latitude": 51.5285582,
|
"latitude": TEST_LATITUDE,
|
||||||
"longitude": -0.2416796,
|
"longitude": TEST_LONGITUDE,
|
||||||
"distance": 5,
|
"distance": 5,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -189,8 +199,8 @@ async def test_options_add_sensor(
|
|||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
"latitude": 51.5285582,
|
"latitude": TEST_LATITUDE,
|
||||||
"longitude": -0.2416796,
|
"longitude": TEST_LONGITUDE,
|
||||||
"distance": 5,
|
"distance": 5,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -200,19 +210,22 @@ async def test_options_add_sensor(
|
|||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
"sensor_index": "567890",
|
"sensor_index": str(TEST_SENSOR_INDEX2),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
||||||
assert result["data"] == {
|
assert result["data"] == {
|
||||||
"last_update_sensor_add": True,
|
"last_update_sensor_add": True,
|
||||||
"sensor_indices": [123456, 567890],
|
"sensor_indices": [TEST_SENSOR_INDEX1, TEST_SENSOR_INDEX2],
|
||||||
}
|
}
|
||||||
|
|
||||||
assert config_entry.options["sensor_indices"] == [123456, 567890]
|
assert config_entry.options["sensor_indices"] == [
|
||||||
|
TEST_SENSOR_INDEX1,
|
||||||
|
TEST_SENSOR_INDEX2,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
async def test_options_add_sensor_duplicate(hass, config_entry, setup_purpleair):
|
async def test_options_add_sensor_duplicate(hass, config_entry, setup_config_entry):
|
||||||
"""Test adding a duplicate sensor via the options flow."""
|
"""Test adding a duplicate sensor via the options flow."""
|
||||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.MENU
|
assert result["type"] == data_entry_flow.FlowResultType.MENU
|
||||||
@ -227,8 +240,8 @@ async def test_options_add_sensor_duplicate(hass, config_entry, setup_purpleair)
|
|||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
"latitude": 51.5285582,
|
"latitude": TEST_LATITUDE,
|
||||||
"longitude": -0.2416796,
|
"longitude": TEST_LONGITUDE,
|
||||||
"distance": 5,
|
"distance": 5,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -238,14 +251,14 @@ async def test_options_add_sensor_duplicate(hass, config_entry, setup_purpleair)
|
|||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
"sensor_index": "123456",
|
"sensor_index": str(TEST_SENSOR_INDEX1),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
async def test_options_remove_sensor(hass, config_entry, setup_purpleair):
|
async def test_options_remove_sensor(hass, config_entry, setup_config_entry):
|
||||||
"""Test removing a sensor via the options flow."""
|
"""Test removing a sensor via the options flow."""
|
||||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||||
assert result["type"] == data_entry_flow.FlowResultType.MENU
|
assert result["type"] == data_entry_flow.FlowResultType.MENU
|
||||||
@ -258,7 +271,7 @@ async def test_options_remove_sensor(hass, config_entry, setup_purpleair):
|
|||||||
assert result["step_id"] == "remove_sensor"
|
assert result["step_id"] == "remove_sensor"
|
||||||
|
|
||||||
device_registry = dr.async_get(hass)
|
device_registry = dr.async_get(hass)
|
||||||
device_entry = device_registry.async_get_device({(DOMAIN, "123456")})
|
device_entry = device_registry.async_get_device({(DOMAIN, str(TEST_SENSOR_INDEX1))})
|
||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input={"sensor_device_id": device_entry.id},
|
user_input={"sensor_device_id": device_entry.id},
|
||||||
|
@ -4,7 +4,7 @@ from homeassistant.components.diagnostics import REDACTED
|
|||||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||||
|
|
||||||
|
|
||||||
async def test_entry_diagnostics(hass, config_entry, hass_client, setup_purpleair):
|
async def test_entry_diagnostics(hass, config_entry, hass_client, setup_config_entry):
|
||||||
"""Test config entry diagnostics."""
|
"""Test config entry diagnostics."""
|
||||||
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
|
assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == {
|
||||||
"entry": {
|
"entry": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user