Skip to content

Commit

Permalink
refactor: Request exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 10, 2024
1 parent ae0da32 commit 7b897ab
Showing 1 changed file with 6 additions and 15 deletions.
21 changes: 6 additions & 15 deletions custom_components/solarman/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,6 @@ async def try_read_write(self, code, start, arg, message, incremental_wait):

return response

async def get_failed(self):
_LOGGER.debug(f"[{self.config.serial}] Fetching failed. [Previous State: {self.get_connection_state} ({self.state})]")
self.state = 0 if self.state == 1 else -1

await self.modbus.disconnect()

return self.state == -1

async def get(self, runtime = 0, requests = None):
scheduled = self.profile.parser.schedule_requests(runtime) if not requests else requests
scheduled_count = len(scheduled) if scheduled else 0
Expand All @@ -232,14 +224,13 @@ async def get(self, runtime = 0, requests = None):
self.state_updated = now
self.state = 1

except TimeoutError as e:
if await self.get_failed():
raise
_LOGGER.debug(f"[{self.config.serial}] Timeout fetching {self.config.name} data: {e}")
except Exception as e:
if await self.get_failed():
except (TimeoutError, Exception) as e:
_LOGGER.debug(f"[{self.config.serial}] Fetching failed. [Previous State: {self.get_connection_state} ({self.state})]")
self.state = 0 if self.state == 1 else -1
if self.state == -1:
await self.modbus.disconnect()
raise
_LOGGER.debug(f"[{self.config.serial}] Error fetching {self.config.name} data: {e}")
_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

0 comments on commit 7b897ab

Please sign in to comment.