Skip to content

Commit

Permalink
Fix loading of Fritz!Smarthome issues on older Fritz!Box (#83688)
Browse files Browse the repository at this point in the history
* check if templates are available

* add test
  • Loading branch information
mib1185 authored and balloob committed Dec 11, 2022
1 parent 3a2fb40 commit e150533
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
21 changes: 16 additions & 5 deletions homeassistant/components/fritzbox/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from dataclasses import dataclass
from datetime import timedelta
from xml.etree.ElementTree import ParseError

from pyfritzhome import Fritzhome, FritzhomeDevice, LoginError
from pyfritzhome.devicetypes import FritzhomeTemplate
Expand Down Expand Up @@ -34,6 +35,13 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
self.entry = entry
self.fritz: Fritzhome = hass.data[DOMAIN][self.entry.entry_id][CONF_CONNECTIONS]
self.configuration_url = self.fritz.get_prefixed_host()
self.has_templates = True
try:
hass.async_add_executor_job(self.fritz.update_templates)
except ParseError:
LOGGER.info("Disable smarthome templates")
self.has_templates = False

super().__init__(
hass,
LOGGER,
Expand All @@ -45,7 +53,8 @@ def _update_fritz_devices(self) -> FritzboxCoordinatorData:
"""Update all fritzbox device data."""
try:
self.fritz.update_devices()
self.fritz.update_templates()
if self.has_templates:
self.fritz.update_templates()
except requests.exceptions.ConnectionError as ex:
raise UpdateFailed from ex
except requests.exceptions.HTTPError:
Expand All @@ -55,7 +64,8 @@ def _update_fritz_devices(self) -> FritzboxCoordinatorData:
except LoginError as ex:
raise ConfigEntryAuthFailed from ex
self.fritz.update_devices()
self.fritz.update_templates()
if self.has_templates:
self.fritz.update_templates()

devices = self.fritz.get_devices()
device_data = {}
Expand All @@ -75,10 +85,11 @@ def _update_fritz_devices(self) -> FritzboxCoordinatorData:

device_data[device.ain] = device

templates = self.fritz.get_templates()
template_data = {}
for template in templates:
template_data[template.ain] = template
if self.has_templates:
templates = self.fritz.get_templates()
for template in templates:
template_data[template.ain] = template

return FritzboxCoordinatorData(devices=device_data, templates=template_data)

Expand Down
17 changes: 16 additions & 1 deletion tests/components/fritzbox/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from unittest.mock import Mock, call, patch
from xml.etree.ElementTree import ParseError

from pyfritzhome import LoginError
import pytest
Expand Down Expand Up @@ -167,7 +168,7 @@ async def test_coordinator_update_after_reboot(hass: HomeAssistant, fritz: Mock)

assert await hass.config_entries.async_setup(entry.entry_id)
assert fritz().update_devices.call_count == 2
assert fritz().update_templates.call_count == 1
assert fritz().update_templates.call_count == 2
assert fritz().get_devices.call_count == 1
assert fritz().get_templates.call_count == 1
assert fritz().login.call_count == 2
Expand Down Expand Up @@ -265,3 +266,17 @@ async def test_raise_config_entry_not_ready_when_offline(hass: HomeAssistant):
entries = hass.config_entries.async_entries()
config_entry = entries[0]
assert config_entry.state is ConfigEntryState.SETUP_ERROR


async def test_disable_smarthome_templates(hass: HomeAssistant, fritz: Mock):
"""Test smarthome templates are disabled."""
entry = MockConfigEntry(
domain=FB_DOMAIN,
data=MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0],
unique_id="any",
)
entry.add_to_hass(hass)
fritz().update_templates.side_effect = [ParseError(), ""]

assert await hass.config_entries.async_setup(entry.entry_id)
assert fritz().update_templates.call_count == 1

0 comments on commit e150533

Please sign in to comment.