From 8a084599d886f7391806baa28139c055f409a338 Mon Sep 17 00:00:00 2001 From: Manu <4445816+tr4nt0r@users.noreply.github.com> Date: Tue, 22 Apr 2025 13:29:05 +0200 Subject: [PATCH] Refactor coordinator of ista EcoTrend integration (#143422) --- .../components/ista_ecotrend/__init__.py | 17 +------- .../components/ista_ecotrend/coordinator.py | 40 +++++++++++++------ .../ista_ecotrend/quality_scale.yaml | 4 +- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/ista_ecotrend/__init__.py b/homeassistant/components/ista_ecotrend/__init__.py index 4262b354acb..7650f0d5f18 100644 --- a/homeassistant/components/ista_ecotrend/__init__.py +++ b/homeassistant/components/ista_ecotrend/__init__.py @@ -4,13 +4,11 @@ from __future__ import annotations import logging -from pyecotrend_ista import KeycloakError, LoginError, PyEcotrendIsta, ServerError +from pyecotrend_ista import PyEcotrendIsta from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady -from .const import DOMAIN from .coordinator import IstaConfigEntry, IstaCoordinator _LOGGER = logging.getLogger(__name__) @@ -25,19 +23,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: IstaConfigEntry) -> bool entry.data[CONF_PASSWORD], _LOGGER, ) - try: - await hass.async_add_executor_job(ista.login) - except ServerError as e: - raise ConfigEntryNotReady( - translation_domain=DOMAIN, - translation_key="connection_exception", - ) from e - except (LoginError, KeycloakError) as e: - raise ConfigEntryAuthFailed( - translation_domain=DOMAIN, - translation_key="authentication_exception", - translation_placeholders={CONF_EMAIL: entry.data[CONF_EMAIL]}, - ) from e coordinator = IstaCoordinator(hass, entry, ista) await coordinator.async_config_entry_first_refresh() diff --git a/homeassistant/components/ista_ecotrend/coordinator.py b/homeassistant/components/ista_ecotrend/coordinator.py index 53ef4a46d20..13167b9d06c 100644 --- a/homeassistant/components/ista_ecotrend/coordinator.py +++ b/homeassistant/components/ista_ecotrend/coordinator.py @@ -11,7 +11,7 @@ from pyecotrend_ista import KeycloakError, LoginError, PyEcotrendIsta, ServerErr from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_EMAIL from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryAuthFailed +from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from .const import DOMAIN @@ -25,6 +25,7 @@ class IstaCoordinator(DataUpdateCoordinator[dict[str, Any]]): """Ista EcoTrend data update coordinator.""" config_entry: IstaConfigEntry + details: dict[str, Any] def __init__( self, hass: HomeAssistant, config_entry: IstaConfigEntry, ista: PyEcotrendIsta @@ -38,22 +39,35 @@ class IstaCoordinator(DataUpdateCoordinator[dict[str, Any]]): update_interval=timedelta(days=1), ) self.ista = ista - self.details: dict[str, Any] = {} + + async def _async_setup(self) -> None: + """Set up the ista EcoTrend coordinator.""" + + try: + self.details = await self.hass.async_add_executor_job(self.get_details) + except ServerError as e: + raise ConfigEntryNotReady( + translation_domain=DOMAIN, + translation_key="connection_exception", + ) from e + except (LoginError, KeycloakError) as e: + raise ConfigEntryAuthFailed( + translation_domain=DOMAIN, + translation_key="authentication_exception", + translation_placeholders={ + CONF_EMAIL: self.config_entry.data[CONF_EMAIL] + }, + ) from e async def _async_update_data(self): """Fetch ista EcoTrend data.""" try: - await self.hass.async_add_executor_job(self.ista.login) - - if not self.details: - self.details = await self.async_get_details() - return await self.hass.async_add_executor_job(self.get_consumption_data) - except ServerError as e: raise UpdateFailed( - "Unable to connect and retrieve data from ista EcoTrend, try again later" + translation_domain=DOMAIN, + translation_key="connection_exception", ) from e except (LoginError, KeycloakError) as e: raise ConfigEntryAuthFailed( @@ -67,17 +81,17 @@ class IstaCoordinator(DataUpdateCoordinator[dict[str, Any]]): def get_consumption_data(self) -> dict[str, Any]: """Get raw json data for all consumption units.""" + self.ista.login() return { consumption_unit: self.ista.get_consumption_data(consumption_unit) for consumption_unit in self.ista.get_uuids() } - async def async_get_details(self) -> dict[str, Any]: + def get_details(self) -> dict[str, Any]: """Retrieve details of consumption units.""" - result = await self.hass.async_add_executor_job( - self.ista.get_consumption_unit_details - ) + self.ista.login() + result = self.ista.get_consumption_unit_details() return { consumption_unit: next( diff --git a/homeassistant/components/ista_ecotrend/quality_scale.yaml b/homeassistant/components/ista_ecotrend/quality_scale.yaml index b942ecba487..ed7f170eadc 100644 --- a/homeassistant/components/ista_ecotrend/quality_scale.yaml +++ b/homeassistant/components/ista_ecotrend/quality_scale.yaml @@ -5,9 +5,7 @@ rules: comment: The integration registers no actions. appropriate-polling: done brands: done - common-modules: - status: todo - comment: Group the 3 different executor jobs as one executor job + common-modules: done config-flow-test-coverage: status: todo comment: test_form/docstrings outdated, test already_configuret, test abort conditions in reauth,