Skip to content

Commit

Permalink
refactor: Move rediscovery to api.py
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 16, 2024
1 parent b523ca8 commit d77eedd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
14 changes: 9 additions & 5 deletions custom_components/solarman/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ async def try_read_write(self, code, start, arg, message: str, incremental_wait:
return response

async def get(self, runtime = 0, requests = None):
scheduled, scheduled_count = ensure_list_safe_len(self.profile.parser.schedule_requests(runtime) if requests is None else requests)
scheduled, scount = ensure_list_safe_len(self.profile.parser.schedule_requests(runtime) if requests is None else requests)
responses, result = {}, {}

_LOGGER.debug(f"[{self.config.serial}] Scheduling {scheduled_count} query request{'' if scheduled_count == 1 else 's'}. ^{runtime}")
_LOGGER.debug(f"[{self.config.serial}] Scheduling {scount} query request{'s' if scount > 1 else ''}. ^{runtime}")

if scheduled_count == 0:
if scount == 0:
return result

try:
Expand All @@ -230,15 +230,19 @@ async def get(self, runtime = 0, requests = None):

result = self.profile.parser.process(responses) if requests is None else responses

if (rc := len(result) if result else 0) > 0:
_LOGGER.debug(f"[{self.config.serial}] Returning {rc} new values to the Coordinator. [Previous State: {self.state.print} ({self.state.value})]")
if (rcount := len(result) if result else 0) > 0:
_LOGGER.debug(f"[{self.config.serial}] Returning {rcount} new value{'s' if rcount > 1 else ''}. [Previous State: {self.state.print} ({self.state.value})]")
self.state.update()

except (TimeoutError, Exception) as e:
_LOGGER.debug(f"[{self.config.serial}] Fetching failed. [Previous State: {self.state.print} ({self.state.value})]")

await self.endpoint.discover()

if self.state.reevaluate():
await self.modbus.disconnect()
raise

_LOGGER.debug(f"[{self.config.serial}] {"Timeout" if isinstance(e, TimeoutError) else "Error"} fetching {self.config.name} data: {format_exception(e)}")

return result
Expand Down
10 changes: 3 additions & 7 deletions custom_components/solarman/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ def _accounting(self) -> int:

async def _async_update_data(self) -> dict[str, Any]:
try:
try:
return await self.inverter.get(self._accounting())
except:
self._counter = 0
raise
except (TimeoutError, Exception) as e:
return await self.inverter.get(self._accounting())
except Exception as e:
self._counter = 0
if isinstance(e, TimeoutError):
await self.inverter.endpoint.discover()
raise
raise UpdateFailed(e) from e

Expand Down

0 comments on commit d77eedd

Please sign in to comment.