Skip to content

Commit

Permalink
refactor: Enhance coordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan authored Feb 10, 2025
1 parent 2018683 commit 2309899
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions custom_components/solarman/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ def __init__(self, hass: HomeAssistant, device: Device):
super().__init__(hass, _LOGGER, name = device.config.name, update_interval = TIMINGS_UPDATE_INTERVAL, always_update = False)
self.device = device
self._counter = 0
self._last_successful_data: dict[str, Any] | None = None
self._is_offline_logged = False

async def _async_setup(self) -> None:
try:
Expand All @@ -31,22 +29,17 @@ async def _async_setup(self) -> None:
async def _async_update_data(self) -> dict[str, Any]:
try:
try:
data = await self.device.get(int(self._counter * self._update_interval_seconds))
self._last_successful_data = data
self._is_offline_logged = False
return data
#return await self.device.get(int(self._counter * self._update_interval_seconds))
return await self.device.get(int(self._counter * self._update_interval_seconds))
finally:
self._counter += 1
except Exception as e:
self._counter = 0

if not self._is_offline_logged:
if self.last_update_success:
_LOGGER.warning("Error retrieving data. Use last known data.")
self._is_offline_logged = True

if self._last_successful_data is not None:
return self._last_successful_data
if self.data is not None:
return self.data

if isinstance(e, TimeoutError):
raise
Expand Down

0 comments on commit 2309899

Please sign in to comment.