Add dynamic update interval to Airly integration (#47505)
* Add dynamic update interval * Update tests * Improve tests * Improve comments * Add MAX_UPDATE_INTERVAL * Suggested change Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Use async_fire_time_changed to test update interval * Fix test_update_interval * Patch dt_util in airly integration * Cleaning * Use total_seconds instead of seconds * Fix update interval test * Refactor update interval test * Don't create new context manager Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
9db6d0cee4
commit
513685bbea
3 changed files with 103 additions and 34 deletions
|
@ -1,6 +1,7 @@
|
|||
"""Test init of Airly integration."""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.airly import set_update_interval
|
||||
from homeassistant.components.airly.const import DOMAIN
|
||||
from homeassistant.config_entries import (
|
||||
ENTRY_STATE_LOADED,
|
||||
|
@ -8,10 +9,11 @@ from homeassistant.config_entries import (
|
|||
ENTRY_STATE_SETUP_RETRY,
|
||||
)
|
||||
from homeassistant.const import STATE_UNAVAILABLE
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from . import API_POINT_URL
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
|
||||
from tests.components.airly import init_integration
|
||||
|
||||
|
||||
|
@ -88,37 +90,83 @@ async def test_config_with_turned_off_station(hass, aioclient_mock):
|
|||
|
||||
async def test_update_interval(hass, aioclient_mock):
|
||||
"""Test correct update interval when the number of configured instances changes."""
|
||||
entry = await init_integration(hass, aioclient_mock)
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
for instance in hass.data[DOMAIN].values():
|
||||
assert instance.update_interval == timedelta(minutes=15)
|
||||
REMAINING_RQUESTS = 15
|
||||
HEADERS = {
|
||||
"X-RateLimit-Limit-day": "100",
|
||||
"X-RateLimit-Remaining-day": str(REMAINING_RQUESTS),
|
||||
}
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="Work",
|
||||
unique_id="66.66-111.11",
|
||||
title="Home",
|
||||
unique_id="123-456",
|
||||
data={
|
||||
"api_key": "foo",
|
||||
"latitude": 66.66,
|
||||
"longitude": 111.11,
|
||||
"name": "Work",
|
||||
"latitude": 123,
|
||||
"longitude": 456,
|
||||
"name": "Home",
|
||||
},
|
||||
)
|
||||
|
||||
aioclient_mock.get(
|
||||
"https://airapi.airly.eu/v2/measurements/point?lat=66.660000&lng=111.110000",
|
||||
API_POINT_URL,
|
||||
text=load_fixture("airly_valid_station.json"),
|
||||
headers=HEADERS,
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
instances = 1
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 2
|
||||
assert aioclient_mock.call_count == 1
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
for instance in hass.data[DOMAIN].values():
|
||||
assert instance.update_interval == timedelta(minutes=30)
|
||||
|
||||
update_interval = set_update_interval(instances, REMAINING_RQUESTS)
|
||||
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",
|
||||
text=load_fixture("airly_valid_station.json"),
|
||||
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
|
||||
assert entry.state == ENTRY_STATE_LOADED
|
||||
|
||||
update_interval = set_update_interval(instances, REMAINING_RQUESTS)
|
||||
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
|
||||
|
||||
|
||||
async def test_unload_entry(hass, aioclient_mock):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue