Increase update interval in AccuWeather integration (#44984)

This commit is contained in:
Maciej Bieniek 2021-01-20 21:17:53 +01:00 committed by GitHub
parent e46f1c0a10
commit 14f5eb7305
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 8 deletions

View file

@ -100,13 +100,13 @@ class AccuWeatherDataUpdateCoordinator(DataUpdateCoordinator):
self.accuweather = AccuWeather(api_key, session, location_key=self.location_key)
# Enabling the forecast download increases the number of requests per data
# update, we use 32 minutes for current condition only and 64 minutes for
# update, we use 40 minutes for current condition only and 80 minutes for
# current condition and forecast as update interval to not exceed allowed number
# of requests. We have 50 requests allowed per day, so we use 45 and leave 5 as
# of requests. We have 50 requests allowed per day, so we use 36 and leave 14 as
# a reserve for restarting HA.
update_interval = (
timedelta(minutes=64) if self.forecast else timedelta(minutes=32)
)
update_interval = timedelta(minutes=40)
if self.forecast:
update_interval *= 2
_LOGGER.debug("Data will be update every %s", update_interval)
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=update_interval)

View file

@ -25,7 +25,7 @@
"step": {
"user": {
"title": "AccuWeather Options",
"description": "Due to the limitations of the free version of the AccuWeather API key, when you enable weather forecast, data updates will be performed every 64 minutes instead of every 32 minutes.",
"description": "Due to the limitations of the free version of the AccuWeather API key, when you enable weather forecast, data updates will be performed every 80 minutes instead of every 40 minutes.",
"data": {
"forecast": "Weather forecast"
}

View file

@ -1,7 +1,10 @@
"""Test init of AccuWeather integration."""
from datetime import timedelta
from unittest.mock import patch
from homeassistant.components.accuweather.const import DOMAIN
from accuweather import ApiError
from homeassistant.components.accuweather.const import COORDINATOR, DOMAIN
from homeassistant.config_entries import (
ENTRY_STATE_LOADED,
ENTRY_STATE_NOT_LOADED,
@ -39,7 +42,7 @@ async def test_config_not_ready(hass):
with patch(
"homeassistant.components.accuweather.AccuWeather._async_get_data",
side_effect=ConnectionError(),
side_effect=ApiError("API Error"),
):
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
@ -58,3 +61,23 @@ async def test_unload_entry(hass):
assert entry.state == ENTRY_STATE_NOT_LOADED
assert not hass.data.get(DOMAIN)
async def test_update_interval(hass):
"""Test correct update interval."""
entry = await init_integration(hass)
assert entry.state == ENTRY_STATE_LOADED
assert hass.data[DOMAIN][entry.entry_id][COORDINATOR].update_interval == timedelta(
minutes=40
)
async def test_update_interval_forecast(hass):
"""Test correct update interval when forecast is True."""
entry = await init_integration(hass, forecast=True)
assert entry.state == ENTRY_STATE_LOADED
assert hass.data[DOMAIN][entry.entry_id][COORDINATOR].update_interval == timedelta(
minutes=80
)