From 14f5eb73057bc1be4c4f5e85d893188665fc26b7 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Wed, 20 Jan 2021 21:17:53 +0100 Subject: [PATCH] Increase update interval in AccuWeather integration (#44984) --- .../components/accuweather/__init__.py | 10 +++---- .../components/accuweather/strings.json | 2 +- tests/components/accuweather/test_init.py | 27 +++++++++++++++++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/accuweather/__init__.py b/homeassistant/components/accuweather/__init__.py index c8ae14678d5..27dbae7a41f 100644 --- a/homeassistant/components/accuweather/__init__.py +++ b/homeassistant/components/accuweather/__init__.py @@ -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) diff --git a/homeassistant/components/accuweather/strings.json b/homeassistant/components/accuweather/strings.json index 65aa2a9ed91..c4305a0a7a5 100644 --- a/homeassistant/components/accuweather/strings.json +++ b/homeassistant/components/accuweather/strings.json @@ -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" } diff --git a/tests/components/accuweather/test_init.py b/tests/components/accuweather/test_init.py index 3e480d57278..ac331197082 100644 --- a/tests/components/accuweather/test_init.py +++ b/tests/components/accuweather/test_init.py @@ -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 + )