2020-07-30 18:41:18 +00:00
|
|
|
"""Test init of Airly integration."""
|
2023-02-09 15:09:13 +00:00
|
|
|
from typing import Any
|
2021-04-27 21:34:53 +00:00
|
|
|
from unittest.mock import patch
|
2020-07-30 18:41:18 +00:00
|
|
|
|
2021-05-10 10:13:45 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-06-28 08:15:56 +00:00
|
|
|
from homeassistant.components.air_quality import DOMAIN as AIR_QUALITY_PLATFORM
|
2021-04-27 21:34:53 +00:00
|
|
|
from homeassistant.components.airly import set_update_interval
|
2020-07-30 18:41:18 +00:00
|
|
|
from homeassistant.components.airly.const import DOMAIN
|
2021-05-20 17:19:20 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
2020-07-30 18:41:18 +00:00
|
|
|
from homeassistant.const import STATE_UNAVAILABLE
|
2023-02-08 11:16:23 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-02-09 15:09:13 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
2021-04-27 21:34:53 +00:00
|
|
|
from homeassistant.util.dt import utcnow
|
2020-07-30 18:41:18 +00:00
|
|
|
|
2022-09-19 07:48:25 +00:00
|
|
|
from . import API_POINT_URL, init_integration
|
2020-12-17 15:44:46 +00:00
|
|
|
|
2023-02-09 09:43:45 +00:00
|
|
|
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
|
2023-02-08 11:16:23 +00:00
|
|
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
|
2023-02-08 11:16:23 +00:00
|
|
|
async def test_async_setup_entry(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2020-07-30 18:41:18 +00:00
|
|
|
"""Test a successful setup entry."""
|
2020-12-17 15:44:46 +00:00
|
|
|
await init_integration(hass, aioclient_mock)
|
2020-07-30 18:41:18 +00:00
|
|
|
|
2023-04-14 07:43:15 +00:00
|
|
|
state = hass.states.get("sensor.home_pm2_5")
|
2020-07-30 18:41:18 +00:00
|
|
|
assert state is not None
|
|
|
|
assert state.state != STATE_UNAVAILABLE
|
2023-02-04 19:54:36 +00:00
|
|
|
assert state.state == "4.37"
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
|
2023-02-08 11:16:23 +00:00
|
|
|
async def test_config_not_ready(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2020-07-30 18:41:18 +00:00
|
|
|
"""Test for setup failure if connection to Airly is missing."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="Home",
|
2020-12-17 15:44:46 +00:00
|
|
|
unique_id="123-456",
|
2020-07-30 18:41:18 +00:00
|
|
|
data={
|
|
|
|
"api_key": "foo",
|
2020-12-17 15:44:46 +00:00
|
|
|
"latitude": 123,
|
|
|
|
"longitude": 456,
|
2020-07-30 18:41:18 +00:00
|
|
|
"name": "Home",
|
2021-01-04 22:14:45 +00:00
|
|
|
"use_nearest": True,
|
2020-07-30 18:41:18 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-12-17 15:44:46 +00:00
|
|
|
aioclient_mock.get(API_POINT_URL, exc=ConnectionError())
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.SETUP_RETRY
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
|
2023-02-08 11:16:23 +00:00
|
|
|
async def test_config_without_unique_id(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2020-07-30 18:41:18 +00:00
|
|
|
"""Test for setup entry without unique_id."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="Home",
|
|
|
|
data={
|
|
|
|
"api_key": "foo",
|
2020-12-17 15:44:46 +00:00
|
|
|
"latitude": 123,
|
|
|
|
"longitude": 456,
|
2020-07-30 18:41:18 +00:00
|
|
|
"name": "Home",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-11-02 03:47:05 +00:00
|
|
|
aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
|
2020-12-17 15:44:46 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.LOADED
|
2020-12-17 15:44:46 +00:00
|
|
|
assert entry.unique_id == "123-456"
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
|
2023-02-08 11:16:23 +00:00
|
|
|
async def test_config_with_turned_off_station(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2020-07-30 18:41:18 +00:00
|
|
|
"""Test for setup entry for a turned off measuring station."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="Home",
|
2020-12-17 15:44:46 +00:00
|
|
|
unique_id="123-456",
|
2020-07-30 18:41:18 +00:00
|
|
|
data={
|
|
|
|
"api_key": "foo",
|
2020-12-17 15:44:46 +00:00
|
|
|
"latitude": 123,
|
|
|
|
"longitude": 456,
|
2020-07-30 18:41:18 +00:00
|
|
|
"name": "Home",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-11-02 03:47:05 +00:00
|
|
|
aioclient_mock.get(API_POINT_URL, text=load_fixture("no_station.json", "airly"))
|
2020-12-17 15:44:46 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.SETUP_RETRY
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
|
2023-02-08 11:16:23 +00:00
|
|
|
async def test_update_interval(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2020-07-30 18:41:18 +00:00
|
|
|
"""Test correct update interval when the number of configured instances changes."""
|
2022-01-18 03:22:11 +00:00
|
|
|
REMAINING_REQUESTS = 15
|
2021-04-27 21:34:53 +00:00
|
|
|
HEADERS = {
|
|
|
|
"X-RateLimit-Limit-day": "100",
|
2022-01-18 03:22:11 +00:00
|
|
|
"X-RateLimit-Remaining-day": str(REMAINING_REQUESTS),
|
2021-04-27 21:34:53 +00:00
|
|
|
}
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
2021-04-27 21:34:53 +00:00
|
|
|
title="Home",
|
|
|
|
unique_id="123-456",
|
2020-07-30 18:41:18 +00:00
|
|
|
data={
|
|
|
|
"api_key": "foo",
|
2021-04-27 21:34:53 +00:00
|
|
|
"latitude": 123,
|
|
|
|
"longitude": 456,
|
|
|
|
"name": "Home",
|
2020-07-30 18:41:18 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-12-17 15:44:46 +00:00
|
|
|
aioclient_mock.get(
|
2021-04-27 21:34:53 +00:00
|
|
|
API_POINT_URL,
|
2021-11-02 03:47:05 +00:00
|
|
|
text=load_fixture("valid_station.json", "airly"),
|
2021-04-27 21:34:53 +00:00
|
|
|
headers=HEADERS,
|
2020-12-17 15:44:46 +00:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
2021-04-27 21:34:53 +00:00
|
|
|
instances = 1
|
2020-07-30 18:41:18 +00:00
|
|
|
|
2021-04-27 21:34:53 +00:00
|
|
|
assert aioclient_mock.call_count == 1
|
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.LOADED
|
2021-04-27 21:34:53 +00:00
|
|
|
|
2022-01-18 03:22:11 +00:00
|
|
|
update_interval = set_update_interval(instances, REMAINING_REQUESTS)
|
2021-04-27 21:34:53 +00:00
|
|
|
future = utcnow() + update_interval
|
|
|
|
with patch("homeassistant.util.dt.utcnow") as mock_utcnow:
|
|
|
|
mock_utcnow.return_value = future
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# call_count should increase by one because we have one instance configured
|
|
|
|
assert aioclient_mock.call_count == 2
|
|
|
|
|
|
|
|
# Now we add the second Airly instance
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="Work",
|
|
|
|
unique_id="66.66-111.11",
|
|
|
|
data={
|
|
|
|
"api_key": "foo",
|
|
|
|
"latitude": 66.66,
|
|
|
|
"longitude": 111.11,
|
|
|
|
"name": "Work",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
aioclient_mock.get(
|
|
|
|
"https://airapi.airly.eu/v2/measurements/point?lat=66.660000&lng=111.110000",
|
2021-11-02 03:47:05 +00:00
|
|
|
text=load_fixture("valid_station.json", "airly"),
|
2021-04-27 21:34:53 +00:00
|
|
|
headers=HEADERS,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
instances = 2
|
|
|
|
|
|
|
|
assert aioclient_mock.call_count == 3
|
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 2
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.LOADED
|
2021-04-27 21:34:53 +00:00
|
|
|
|
2022-01-18 03:22:11 +00:00
|
|
|
update_interval = set_update_interval(instances, REMAINING_REQUESTS)
|
2021-04-27 21:34:53 +00:00
|
|
|
future = utcnow() + update_interval
|
|
|
|
mock_utcnow.return_value = future
|
|
|
|
async_fire_time_changed(hass, future)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# call_count should increase by two because we have two instances configured
|
|
|
|
assert aioclient_mock.call_count == 5
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
|
2023-02-08 11:16:23 +00:00
|
|
|
async def test_unload_entry(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2020-07-30 18:41:18 +00:00
|
|
|
"""Test successful unload of entry."""
|
2020-12-17 15:44:46 +00:00
|
|
|
entry = await init_integration(hass, aioclient_mock)
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.LOADED
|
2020-07-30 18:41:18 +00:00
|
|
|
|
|
|
|
assert await hass.config_entries.async_unload(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2021-05-20 17:19:20 +00:00
|
|
|
assert entry.state is ConfigEntryState.NOT_LOADED
|
2020-07-30 18:41:18 +00:00
|
|
|
assert not hass.data.get(DOMAIN)
|
2021-05-07 14:47:52 +00:00
|
|
|
|
|
|
|
|
2021-05-10 10:13:45 +00:00
|
|
|
@pytest.mark.parametrize("old_identifier", ((DOMAIN, 123, 456), (DOMAIN, "123", "456")))
|
2023-02-09 09:43:45 +00:00
|
|
|
async def test_migrate_device_entry(
|
2023-02-09 15:09:13 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
aioclient_mock: AiohttpClientMocker,
|
|
|
|
old_identifier: tuple[str, Any, Any],
|
|
|
|
device_registry: dr.DeviceRegistry,
|
|
|
|
) -> None:
|
2021-05-07 14:47:52 +00:00
|
|
|
"""Test device_info identifiers migration."""
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
title="Home",
|
|
|
|
unique_id="123-456",
|
|
|
|
data={
|
|
|
|
"api_key": "foo",
|
|
|
|
"latitude": 123,
|
|
|
|
"longitude": 456,
|
|
|
|
"name": "Home",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-11-02 03:47:05 +00:00
|
|
|
aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly"))
|
2021-05-07 14:47:52 +00:00
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
2023-02-09 09:43:45 +00:00
|
|
|
device_entry = device_registry.async_get_or_create(
|
2021-05-10 10:13:45 +00:00
|
|
|
config_entry_id=config_entry.entry_id, identifiers={old_identifier}
|
2021-05-07 14:47:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2023-02-09 09:43:45 +00:00
|
|
|
migrated_device_entry = device_registry.async_get_or_create(
|
2021-05-10 10:13:45 +00:00
|
|
|
config_entry_id=config_entry.entry_id, identifiers={(DOMAIN, "123-456")}
|
2021-05-07 14:47:52 +00:00
|
|
|
)
|
|
|
|
assert device_entry.id == migrated_device_entry.id
|
2021-06-28 08:15:56 +00:00
|
|
|
|
|
|
|
|
2023-02-08 11:16:23 +00:00
|
|
|
async def test_remove_air_quality_entities(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2021-06-28 08:15:56 +00:00
|
|
|
"""Test remove air_quality entities from registry."""
|
|
|
|
registry = er.async_get(hass)
|
|
|
|
|
|
|
|
registry.async_get_or_create(
|
|
|
|
AIR_QUALITY_PLATFORM,
|
|
|
|
DOMAIN,
|
|
|
|
"123-456",
|
|
|
|
suggested_object_id="home",
|
|
|
|
disabled_by=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
await init_integration(hass, aioclient_mock)
|
|
|
|
|
|
|
|
entry = registry.async_get("air_quality.home")
|
|
|
|
assert entry is None
|