Skip to content

Commit

Permalink
fix: Coordinator update fail fallback (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuyKh authored Nov 7, 2024
1 parent efd6b97 commit 952d29f
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions custom_components/iec/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from iec_api.iec_client import IecClient
from iec_api.models.contract import Contract
from iec_api.models.device import Device, Devices
Expand Down Expand Up @@ -341,32 +341,9 @@ async def _verify_daily_readings_exist(
f' is present: {daily_reading.value}'
)

async def _async_update_data(
async def _update_data(
self,
) -> dict[str, dict[str, Any]]:
"""Fetch data from API endpoint."""
if self._first_load:
_LOGGER.debug("Loading API token from config entry")
await self.api.load_jwt_token(
JWT.from_dict(self._entry_data[CONF_API_TOKEN])
)

self._first_load = False
try:
_LOGGER.debug("Checking if API token needs to be refreshed")
# First thing first, check the token and refresh if needed.
old_token = self.api.get_token()
await self.api.check_token()
new_token = self.api.get_token()
if old_token != new_token:
_LOGGER.debug("Token refreshed")
new_data = {**self._entry_data, CONF_API_TOKEN: new_token.to_dict()}
self.hass.config_entries.async_update_entry(
entry=self._config_entry, data=new_data
)
except IECError as err:
raise ConfigEntryAuthFailed from err

if not self._bp_number:
customer = await self.api.get_customer()
self._bp_number = customer.bp_number
Expand Down Expand Up @@ -633,6 +610,37 @@ async def _async_update_data(

return data

async def _async_update_data(
self,
) -> dict[str, dict[str, Any]]:
"""Fetch data from API endpoint."""
if self._first_load:
_LOGGER.debug("Loading API token from config entry")
await self.api.load_jwt_token(
JWT.from_dict(self._entry_data[CONF_API_TOKEN])
)

self._first_load = False
try:
_LOGGER.debug("Checking if API token needs to be refreshed")
# First thing first, check the token and refresh if needed.
old_token = self.api.get_token()
await self.api.check_token()
new_token = self.api.get_token()
if old_token != new_token:
_LOGGER.debug("Token refreshed")
new_data = {**self._entry_data, CONF_API_TOKEN: new_token.to_dict()}
self.hass.config_entries.async_update_entry(
entry=self._config_entry, data=new_data
)
except IECError as err:
raise ConfigEntryAuthFailed from err

try:
return await self._update_data()
except Exception as err:
raise UpdateFailed("Failed Updating IEC data") from err

async def _insert_statistics(self, contract_id: int, is_smart_meter: bool) -> None:
if not is_smart_meter:
_LOGGER.info(
Expand Down

0 comments on commit 952d29f

Please sign in to comment.