Skip to content

Commit

Permalink
fix: Processing of empty requests list
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 13, 2024
1 parent 05cacfd commit cb83da4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
9 changes: 6 additions & 3 deletions custom_components/solarman/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ async def read_write(self, code, start, arg):
case _:
raise Exception(f"[{self.serial}] Used incorrect modbus function code {code}")

async def try_read_write(self, code, start, arg, message, incremental_wait):
async def try_read_write(self, code, start, arg, message: str, incremental_wait: bool = True):
_LOGGER.debug(f"[{self.config.serial}] {message} ...")

response = None
Expand Down Expand Up @@ -224,13 +224,16 @@ async def get(self, runtime = 0, requests = None):

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

if scheduled_count == 0:
return result

try:
async with asyncio.timeout(TIMINGS_UPDATE_TIMEOUT):
async with self._semaphore:
for request in scheduled:
code, start, end = get_request_code(request), get_request_start(request), get_request_end(request)
quantity = end - start + 1
responses[(code, start)] = await self.try_read_write(code, start, quantity, f"Querying {code:02X} ~ {start:04} - {end:04} | 0x{start:04X} - 0x{end:04X} #{quantity:03}", True)
responses[(code, start)] = await self.try_read_write(code, start, quantity, f"Querying {code:02X} ~ {start:04} - {end:04} | 0x{start:04X} - 0x{end:04X} #{quantity:03}")

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

Expand All @@ -248,7 +251,7 @@ async def get(self, runtime = 0, requests = None):
return result

async def call(self, code, start, arg):
_LOGGER.debug(f"[{self.config.serial}] Scheduling call request.")
_LOGGER.debug(f"[{self.config.serial}] Scheduling request.")

async with asyncio.timeout(TIMINGS_UPDATE_TIMEOUT):
async with self._semaphore:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/solarman/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def ensure_list(value):
return value if isinstance(value, list) else [value]

def ensure_list_safe_len(value: list):
return ensure_list(value), len(value) if value is not None and isinstance(value, list) else 0
return ensure_list(value), len(value) if value is not None and isinstance(value, list) else (1 if isinstance(value, dict) and value else 0)

def set_request(code, start, end):
return { REQUEST_CODE: code, REQUEST_START: start, REQUEST_END: end }
Expand Down
8 changes: 4 additions & 4 deletions custom_components/solarman/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ async def _async_update_data(self) -> dict[str, Any]:
except:
self._counter = 0
raise
except TimeoutError:
await self.inverter.endpoint.discover()
raise
except Exception as e:
except (TimeoutError, Exception) as e:
if isinstance(e, TimeoutError):
await self.inverter.endpoint.discover()
raise
raise UpdateFailed(e) from e

#async def _reload(self):
Expand Down

0 comments on commit cb83da4

Please sign in to comment.