Perform re-login after Fritzbox has rebooted (#64709)

This commit is contained in:
Michael 2022-01-24 12:25:29 +01:00 committed by GitHub
parent 2a00c096db
commit 6874b49a39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 26 deletions

View file

@ -8,8 +8,8 @@ import requests
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import CONF_CONNECTIONS, DOMAIN, LOGGER
@ -34,19 +34,19 @@ class FritzboxDataUpdateCoordinator(DataUpdateCoordinator):
def _update_fritz_devices(self) -> dict[str, FritzhomeDevice]:
"""Update all fritzbox device data."""
try:
devices = self.fritz.get_devices()
self.fritz.update_devices()
except requests.exceptions.ConnectionError as ex:
raise ConfigEntryNotReady from ex
raise UpdateFailed from ex
except requests.exceptions.HTTPError:
# If the device rebooted, login again
try:
self.fritz.login()
except LoginError as ex:
raise ConfigEntryAuthFailed from ex
devices = self.fritz.get_devices()
data = {}
self.fritz.update_devices()
devices = self.fritz.get_devices()
data = {}
for device in devices:
# assume device as unavailable, see #55799
if (

View file

@ -222,15 +222,15 @@ async def test_update_error(hass: HomeAssistant, fritz: Mock):
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
)
assert fritz().update_devices.call_count == 1
assert fritz().login.call_count == 1
assert fritz().update_devices.call_count == 2
assert fritz().login.call_count == 2
next_update = dt_util.utcnow() + timedelta(seconds=200)
async_fire_time_changed(hass, next_update)
await hass.async_block_till_done()
assert fritz().update_devices.call_count == 2
assert fritz().login.call_count == 2
assert fritz().update_devices.call_count == 4
assert fritz().login.call_count == 4
async def test_set_temperature_temperature(hass: HomeAssistant, fritz: Mock):

View file

@ -163,10 +163,11 @@ async def test_coordinator_update_after_reboot(hass: HomeAssistant, fritz: Mock)
unique_id="any",
)
entry.add_to_hass(hass)
fritz().get_devices.side_effect = [HTTPError(), ""]
fritz().update_devices.side_effect = [HTTPError(), ""]
assert await hass.config_entries.async_setup(entry.entry_id)
assert fritz().get_devices.call_count == 2
assert fritz().update_devices.call_count == 2
assert fritz().get_devices.call_count == 1
assert fritz().login.call_count == 2
@ -180,11 +181,12 @@ async def test_coordinator_update_after_password_change(
unique_id="any",
)
entry.add_to_hass(hass)
fritz().get_devices.side_effect = HTTPError()
fritz().update_devices.side_effect = HTTPError()
fritz().login.side_effect = ["", LoginError("some_user")]
assert not await hass.config_entries.async_setup(entry.entry_id)
assert fritz().get_devices.call_count == 1
assert fritz().update_devices.call_count == 1
assert fritz().get_devices.call_count == 0
assert fritz().login.call_count == 2

View file

@ -174,12 +174,12 @@ async def test_update_error(hass: HomeAssistant, fritz: Mock):
assert not await setup_config_entry(
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
)
assert fritz().update_devices.call_count == 1
assert fritz().login.call_count == 1
assert fritz().update_devices.call_count == 2
assert fritz().login.call_count == 2
next_update = dt_util.utcnow() + timedelta(seconds=200)
async_fire_time_changed(hass, next_update)
await hass.async_block_till_done()
assert fritz().update_devices.call_count == 2
assert fritz().login.call_count == 2
assert fritz().update_devices.call_count == 4
assert fritz().login.call_count == 4

View file

@ -78,12 +78,12 @@ async def test_update_error(hass: HomeAssistant, fritz: Mock):
assert not await setup_config_entry(
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
)
assert fritz().update_devices.call_count == 1
assert fritz().login.call_count == 1
assert fritz().update_devices.call_count == 2
assert fritz().login.call_count == 2
next_update = dt_util.utcnow() + timedelta(seconds=200)
async_fire_time_changed(hass, next_update)
await hass.async_block_till_done()
assert fritz().update_devices.call_count == 2
assert fritz().login.call_count == 2
assert fritz().update_devices.call_count == 4
assert fritz().login.call_count == 4

View file

@ -123,15 +123,15 @@ async def test_update_error(hass: HomeAssistant, fritz: Mock):
assert not await setup_config_entry(
hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz
)
assert fritz().update_devices.call_count == 1
assert fritz().login.call_count == 1
assert fritz().update_devices.call_count == 2
assert fritz().login.call_count == 2
next_update = dt_util.utcnow() + timedelta(seconds=200)
async_fire_time_changed(hass, next_update)
await hass.async_block_till_done()
assert fritz().update_devices.call_count == 2
assert fritz().login.call_count == 2
assert fritz().update_devices.call_count == 4
assert fritz().login.call_count == 4
async def test_assume_device_unavailable(hass: HomeAssistant, fritz: Mock):