Cache data and update faster after failed updates in NWS (#35722)
* add last_update_success_time and a failed update interval * add failed update interval annd valid times to nws * Revert "add last_update_success_time and a failed update interval" This reverts commit 09428c96861aa4601832e6f6392ac3b18591159d. * extend DataUpdateCoordinator
This commit is contained in:
1 parent
8cbee76929
commit
3a97d96dc0
3 files changed
+150
-34
No files matched your search
@@ -10,6 +10,7 @@ from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
from tests.components.nws.const import (
|
||||
EXPECTED_FORECAST_IMPERIAL,
|
||||
@@ -154,39 +155,79 @@ async def test_entity_refresh(hass, mock_simple_nws):
|
||||
|
||||
async def test_error_observation(hass, mock_simple_nws):
|
||||
"""Test error during update observation."""
|
||||
instance = mock_simple_nws.return_value
|
||||
instance.update_observation.side_effect = aiohttp.ClientError
|
||||
utc_time = dt_util.utcnow()
|
||||
with patch("homeassistant.components.nws.utcnow") as mock_utc, patch(
|
||||
"homeassistant.components.nws.weather.utcnow"
|
||||
) as mock_utc_weather:
|
||||
|
||||
entry = MockConfigEntry(domain=nws.DOMAIN, data=NWS_CONFIG,)
|
||||
entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
def increment_time(time):
|
||||
mock_utc.return_value += time
|
||||
mock_utc_weather.return_value += time
|
||||
async_fire_time_changed(hass, mock_utc.return_value)
|
||||
|
||||
instance.update_observation.assert_called_once()
|
||||
mock_utc.return_value = utc_time
|
||||
mock_utc_weather.return_value = utc_time
|
||||
instance = mock_simple_nws.return_value
|
||||
# first update fails
|
||||
instance.update_observation.side_effect = aiohttp.ClientError
|
||||
|
||||
state = hass.states.get("weather.abc_daynight")
|
||||
assert state
|
||||
assert state.state == "unavailable"
|
||||
entry = MockConfigEntry(domain=nws.DOMAIN, data=NWS_CONFIG,)
|
||||
entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("weather.abc_hourly")
|
||||
assert state
|
||||
assert state.state == "unavailable"
|
||||
instance.update_observation.assert_called_once()
|
||||
|
||||
instance.update_observation.side_effect = None
|
||||
state = hass.states.get("weather.abc_daynight")
|
||||
assert state
|
||||
assert state.state == "unavailable"
|
||||
|
||||
future_time = dt_util.utcnow() + timedelta(minutes=15)
|
||||
async_fire_time_changed(hass, future_time)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("weather.abc_hourly")
|
||||
assert state
|
||||
assert state.state == "unavailable"
|
||||
|
||||
assert instance.update_observation.call_count == 2
|
||||
# second update happens faster and succeeds
|
||||
instance.update_observation.side_effect = None
|
||||
increment_time(timedelta(minutes=1))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("weather.abc_daynight")
|
||||
assert state
|
||||
assert state.state == "sunny"
|
||||
assert instance.update_observation.call_count == 2
|
||||
|
||||
state = hass.states.get("weather.abc_hourly")
|
||||
assert state
|
||||
assert state.state == "sunny"
|
||||
state = hass.states.get("weather.abc_daynight")
|
||||
assert state
|
||||
assert state.state == "sunny"
|
||||
|
||||
state = hass.states.get("weather.abc_hourly")
|
||||
assert state
|
||||
assert state.state == "sunny"
|
||||
|
||||
# third udate fails, but data is cached
|
||||
instance.update_observation.side_effect = aiohttp.ClientError
|
||||
|
||||
increment_time(timedelta(minutes=10))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert instance.update_observation.call_count == 3
|
||||
|
||||
state = hass.states.get("weather.abc_daynight")
|
||||
assert state
|
||||
assert state.state == "sunny"
|
||||
|
||||
state = hass.states.get("weather.abc_hourly")
|
||||
assert state
|
||||
assert state.state == "sunny"
|
||||
|
||||
# after 20 minutes data caching expires, data is no longer shown
|
||||
increment_time(timedelta(minutes=10))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("weather.abc_daynight")
|
||||
assert state
|
||||
assert state.state == "unavailable"
|
||||
|
||||
state = hass.states.get("weather.abc_hourly")
|
||||
assert state
|
||||
assert state.state == "unavailable"
|
||||
|
||||
|
||||
async def test_error_forecast(hass, mock_simple_nws):
|
||||
@@ -207,8 +248,7 @@ async def test_error_forecast(hass, mock_simple_nws):
|
||||
|
||||
instance.update_forecast.side_effect = None
|
||||
|
||||
future_time = dt_util.utcnow() + timedelta(minutes=15)
|
||||
async_fire_time_changed(hass, future_time)
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=1))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert instance.update_forecast.call_count == 2
|
||||
@@ -236,8 +276,7 @@ async def test_error_forecast_hourly(hass, mock_simple_nws):
|
||||
|
||||
instance.update_forecast_hourly.side_effect = None
|
||||
|
||||
future_time = dt_util.utcnow() + timedelta(minutes=15)
|
||||
async_fire_time_changed(hass, future_time)
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=1))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert instance.update_forecast_hourly.call_count == 2
|
||||
|
||||
Reference in new issue
Block a user