Skip to content

Commit

Permalink
Fix reauth error and exception in ista EcoTrend integration (#121482)
Browse files Browse the repository at this point in the history
  • Loading branch information
tr4nt0r authored and frenck committed Jul 19, 2024
1 parent 7137075 commit ef7d68b
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions homeassistant/components/ista_ecotrend/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pyecotrend_ista import KeycloakError, LoginError, PyEcotrendIsta, ServerError

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EMAIL
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
Expand All @@ -21,6 +22,8 @@
class IstaCoordinator(DataUpdateCoordinator[dict[str, Any]]):
"""Ista EcoTrend data update coordinator."""

config_entry: ConfigEntry

def __init__(self, hass: HomeAssistant, ista: PyEcotrendIsta) -> None:
"""Initialize ista EcoTrend data update coordinator."""
super().__init__(
Expand All @@ -35,11 +38,14 @@ def __init__(self, hass: HomeAssistant, ista: PyEcotrendIsta) -> None:
async def _async_update_data(self):
"""Fetch ista EcoTrend data."""

if not self.details:
self.details = await self.async_get_details()

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"
Expand All @@ -48,7 +54,9 @@ async def _async_update_data(self):
raise ConfigEntryAuthFailed(
translation_domain=DOMAIN,
translation_key="authentication_exception",
translation_placeholders={CONF_EMAIL: self.ista._email}, # noqa: SLF001
translation_placeholders={
CONF_EMAIL: self.config_entry.data[CONF_EMAIL]
},
) from e

def get_consumption_data(self) -> dict[str, Any]:
Expand All @@ -61,26 +69,16 @@ def get_consumption_data(self) -> dict[str, Any]:

async def async_get_details(self) -> dict[str, Any]:
"""Retrieve details of consumption units."""
try:
result = await self.hass.async_add_executor_job(
self.ista.get_consumption_unit_details

result = await self.hass.async_add_executor_job(
self.ista.get_consumption_unit_details
)

return {
consumption_unit: next(
details
for details in result["consumptionUnits"]
if details["id"] == consumption_unit
)
except ServerError as e:
raise UpdateFailed(
"Unable to connect and retrieve data from ista EcoTrend, try again later"
) from e
except (LoginError, KeycloakError) as e:
raise ConfigEntryAuthFailed(
translation_domain=DOMAIN,
translation_key="authentication_exception",
translation_placeholders={CONF_EMAIL: self.ista._email}, # noqa: SLF001
) from e
else:
return {
consumption_unit: next(
details
for details in result["consumptionUnits"]
if details["id"] == consumption_unit
)
for consumption_unit in self.ista.get_uuids()
}
for consumption_unit in self.ista.get_uuids()
}

0 comments on commit ef7d68b

Please sign in to comment.