Skip to content

Commit 952d29f

Browse files
authored
fix: Coordinator update fail fallback (#217)
1 parent efd6b97 commit 952d29f

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

custom_components/iec/coordinator.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from homeassistant.core import HomeAssistant, callback
2222
from homeassistant.exceptions import ConfigEntryAuthFailed
2323
from homeassistant.helpers import aiohttp_client
24-
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
24+
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
2525
from iec_api.iec_client import IecClient
2626
from iec_api.models.contract import Contract
2727
from iec_api.models.device import Device, Devices
@@ -341,32 +341,9 @@ async def _verify_daily_readings_exist(
341341
f' is present: {daily_reading.value}'
342342
)
343343

344-
async def _async_update_data(
344+
async def _update_data(
345345
self,
346346
) -> dict[str, dict[str, Any]]:
347-
"""Fetch data from API endpoint."""
348-
if self._first_load:
349-
_LOGGER.debug("Loading API token from config entry")
350-
await self.api.load_jwt_token(
351-
JWT.from_dict(self._entry_data[CONF_API_TOKEN])
352-
)
353-
354-
self._first_load = False
355-
try:
356-
_LOGGER.debug("Checking if API token needs to be refreshed")
357-
# First thing first, check the token and refresh if needed.
358-
old_token = self.api.get_token()
359-
await self.api.check_token()
360-
new_token = self.api.get_token()
361-
if old_token != new_token:
362-
_LOGGER.debug("Token refreshed")
363-
new_data = {**self._entry_data, CONF_API_TOKEN: new_token.to_dict()}
364-
self.hass.config_entries.async_update_entry(
365-
entry=self._config_entry, data=new_data
366-
)
367-
except IECError as err:
368-
raise ConfigEntryAuthFailed from err
369-
370347
if not self._bp_number:
371348
customer = await self.api.get_customer()
372349
self._bp_number = customer.bp_number
@@ -633,6 +610,37 @@ async def _async_update_data(
633610

634611
return data
635612

613+
async def _async_update_data(
614+
self,
615+
) -> dict[str, dict[str, Any]]:
616+
"""Fetch data from API endpoint."""
617+
if self._first_load:
618+
_LOGGER.debug("Loading API token from config entry")
619+
await self.api.load_jwt_token(
620+
JWT.from_dict(self._entry_data[CONF_API_TOKEN])
621+
)
622+
623+
self._first_load = False
624+
try:
625+
_LOGGER.debug("Checking if API token needs to be refreshed")
626+
# First thing first, check the token and refresh if needed.
627+
old_token = self.api.get_token()
628+
await self.api.check_token()
629+
new_token = self.api.get_token()
630+
if old_token != new_token:
631+
_LOGGER.debug("Token refreshed")
632+
new_data = {**self._entry_data, CONF_API_TOKEN: new_token.to_dict()}
633+
self.hass.config_entries.async_update_entry(
634+
entry=self._config_entry, data=new_data
635+
)
636+
except IECError as err:
637+
raise ConfigEntryAuthFailed from err
638+
639+
try:
640+
return await self._update_data()
641+
except Exception as err:
642+
raise UpdateFailed("Failed Updating IEC data") from err
643+
636644
async def _insert_statistics(self, contract_id: int, is_smart_meter: bool) -> None:
637645
if not is_smart_meter:
638646
_LOGGER.info(

0 commit comments

Comments
 (0)