Move template check into fritzbox entry setup (#83863)

* move template check into entry setup

* use else in try-except block
This commit is contained in:
Michael 2022-12-12 22:11:57 +01:00 committed by GitHub
parent 22e5d86324
commit 41041cb673
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 9 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from xml.etree.ElementTree import ParseError
from pyfritzhome import Fritzhome, FritzhomeDevice, LoginError from pyfritzhome import Fritzhome, FritzhomeDevice, LoginError
from pyfritzhome.devicetypes.fritzhomeentitybase import FritzhomeEntityBase from pyfritzhome.devicetypes.fritzhomeentitybase import FritzhomeEntityBase
@ -43,7 +44,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
CONF_CONNECTIONS: fritz, CONF_CONNECTIONS: fritz,
} }
coordinator = FritzboxDataUpdateCoordinator(hass, entry) try:
await hass.async_add_executor_job(fritz.update_templates)
except ParseError:
LOGGER.debug("Disable smarthome templates")
has_templates = False
else:
LOGGER.debug("Enable smarthome templates")
has_templates = True
coordinator = FritzboxDataUpdateCoordinator(hass, entry, has_templates)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()

View File

@ -3,7 +3,6 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
from xml.etree.ElementTree import ParseError
from pyfritzhome import Fritzhome, FritzhomeDevice, LoginError from pyfritzhome import Fritzhome, FritzhomeDevice, LoginError
from pyfritzhome.devicetypes import FritzhomeTemplate from pyfritzhome.devicetypes import FritzhomeTemplate
@ -30,17 +29,14 @@ class FritzboxDataUpdateCoordinator(DataUpdateCoordinator[FritzboxCoordinatorDat
configuration_url: str configuration_url: str
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None: def __init__(
self, hass: HomeAssistant, entry: ConfigEntry, has_templates: bool
) -> None:
"""Initialize the Fritzbox Smarthome device coordinator.""" """Initialize the Fritzbox Smarthome device coordinator."""
self.entry = entry self.entry = entry
self.fritz: Fritzhome = hass.data[DOMAIN][self.entry.entry_id][CONF_CONNECTIONS] self.fritz: Fritzhome = hass.data[DOMAIN][self.entry.entry_id][CONF_CONNECTIONS]
self.configuration_url = self.fritz.get_prefixed_host() self.configuration_url = self.fritz.get_prefixed_host()
self.has_templates = True self.has_templates = has_templates
try:
hass.async_add_executor_job(self.fritz.update_templates)
except ParseError:
LOGGER.info("Disable smarthome templates")
self.has_templates = False
super().__init__( super().__init__(
hass, hass,