mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Handle errors in the periodic Freebox update (#184578)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""Support for Freebox devices (Freebox v6 and Freebox mini 4K)."""
|
||||
|
||||
from datetime import timedelta
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
|
||||
from aiohttp import ClientError
|
||||
@@ -92,8 +92,25 @@ async def async_setup_entry(hass: HomeAssistant, entry: FreeboxConfigEntry) -> b
|
||||
except (HttpRequestError, ClientError, TimeoutError) as err:
|
||||
raise ConfigEntryNotReady from err
|
||||
|
||||
update_failed = False
|
||||
|
||||
async def _async_update_all(now: datetime) -> None:
|
||||
"""Update the router, logging a failure once until it recovers."""
|
||||
nonlocal update_failed
|
||||
try:
|
||||
await router.update_all()
|
||||
except (HttpRequestError, ClientError, TimeoutError) as err:
|
||||
if not update_failed:
|
||||
_LOGGER.warning("Error updating the Freebox: %r", err)
|
||||
update_failed = True
|
||||
return
|
||||
|
||||
if update_failed:
|
||||
_LOGGER.info("Updating the Freebox works again")
|
||||
update_failed = False
|
||||
|
||||
entry.async_on_unload(
|
||||
async_track_time_interval(hass, router.update_all, SCAN_INTERVAL)
|
||||
async_track_time_interval(hass, _async_update_all, SCAN_INTERVAL)
|
||||
)
|
||||
|
||||
entry.runtime_data = router
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
from unittest.mock import Mock
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER_DOMAIN
|
||||
from homeassistant.components.freebox import SCAN_INTERVAL
|
||||
@@ -48,3 +49,29 @@ async def test_bridge_mode(
|
||||
|
||||
# If get_interfaces failed, not called again
|
||||
assert router_bridge_mode().lan.get_interfaces.call_count == 1
|
||||
|
||||
|
||||
async def test_update_timeout(
|
||||
hass: HomeAssistant,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
router: Mock,
|
||||
) -> None:
|
||||
"""Test a failing periodic update is logged once and recovers."""
|
||||
await setup_platform(hass, DEVICE_TRACKER_DOMAIN)
|
||||
|
||||
router().lan.get_hosts_list.side_effect = TimeoutError
|
||||
for _ in range(2):
|
||||
freezer.tick(SCAN_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Task exception was never retrieved" not in caplog.text
|
||||
assert caplog.text.count("Error updating the Freebox") == 1
|
||||
|
||||
router().lan.get_hosts_list.side_effect = None
|
||||
freezer.tick(SCAN_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Updating the Freebox works again" in caplog.text
|
||||
|
||||
Reference in New Issue
Block a user