mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 16:17:20 +00:00
Refactor coordinator of ista EcoTrend integration (#143422)
This commit is contained in:
parent
24b51e0582
commit
8a084599d8
@ -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()
|
||||
|
@ -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(
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user