From 0a2fa9984d9be7606bdf4c19fea203119c86e705 Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Mon, 12 Dec 2022 22:11:57 +0100 Subject: [PATCH] Move template check into fritzbox entry setup (#83863) * move template check into entry setup * use else in try-except block --- homeassistant/components/fritzbox/__init__.py | 12 +++++++++++- homeassistant/components/fritzbox/coordinator.py | 12 ++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/fritzbox/__init__.py b/homeassistant/components/fritzbox/__init__.py index 40d170db3b3..43bd0bfeeb0 100644 --- a/homeassistant/components/fritzbox/__init__.py +++ b/homeassistant/components/fritzbox/__init__.py @@ -2,6 +2,7 @@ from __future__ import annotations from abc import ABC, abstractmethod +from xml.etree.ElementTree import ParseError from pyfritzhome import Fritzhome, FritzhomeDevice, LoginError from pyfritzhome.devicetypes.fritzhomeentitybase import FritzhomeEntityBase @@ -43,7 +44,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: 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() diff --git a/homeassistant/components/fritzbox/coordinator.py b/homeassistant/components/fritzbox/coordinator.py index 6bc3bac623f..80087adf9ac 100644 --- a/homeassistant/components/fritzbox/coordinator.py +++ b/homeassistant/components/fritzbox/coordinator.py @@ -3,7 +3,6 @@ from __future__ import annotations 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 @@ -30,17 +29,14 @@ class FritzboxDataUpdateCoordinator(DataUpdateCoordinator[FritzboxCoordinatorDat 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.""" 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 + self.has_templates = has_templates super().__init__( hass,