|
21 | 21 | from homeassistant.core import HomeAssistant, callback
|
22 | 22 | from homeassistant.exceptions import ConfigEntryAuthFailed
|
23 | 23 | from homeassistant.helpers import aiohttp_client
|
24 |
| -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator |
| 24 | +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed |
25 | 25 | from iec_api.iec_client import IecClient
|
26 | 26 | from iec_api.models.contract import Contract
|
27 | 27 | from iec_api.models.device import Device, Devices
|
@@ -341,32 +341,9 @@ async def _verify_daily_readings_exist(
|
341 | 341 | f' is present: {daily_reading.value}'
|
342 | 342 | )
|
343 | 343 |
|
344 |
| - async def _async_update_data( |
| 344 | + async def _update_data( |
345 | 345 | self,
|
346 | 346 | ) -> 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 |
| - |
370 | 347 | if not self._bp_number:
|
371 | 348 | customer = await self.api.get_customer()
|
372 | 349 | self._bp_number = customer.bp_number
|
@@ -633,6 +610,37 @@ async def _async_update_data(
|
633 | 610 |
|
634 | 611 | return data
|
635 | 612 |
|
| 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 | + |
636 | 644 | async def _insert_statistics(self, contract_id: int, is_smart_meter: bool) -> None:
|
637 | 645 | if not is_smart_meter:
|
638 | 646 | _LOGGER.info(
|
|
0 commit comments