Skip to content

Commit

Permalink
refactor: Use of discovery as a ping only during main loop
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 26, 2024
1 parent 2734277 commit ee49a1f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions custom_components/solarman/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def load(self):
self.device_info = await self.profile.resolve(self.get)
_LOGGER.debug(self.device_info)
except TimeoutError as e:
raise TimeoutError(f"[{self.config.serial}] Device setup timed out.") from e
raise TimeoutError(f"[{self.config.serial}] Device setup timed out") from e
except BaseException as e:
raise Exception(f"[{self.config.serial}] Device setup failed. [{format_exception(e)}]") from e

Expand Down Expand Up @@ -107,7 +107,7 @@ async def try_read_write(self, code, start, arg, message: str, incremental_wait:
except Exception as e:
_LOGGER.debug(f"[{self.config.serial}] {message} failed, attempts left: {attempts_left}{'' if attempts_left > 0 else ', aborting.'} [{format_exception(e)}]")

await self.endpoint.discover()
await self.endpoint.discover(True)

if not self.modbus.auto_reconnect:
await self.modbus.disconnect()
Expand Down
4 changes: 3 additions & 1 deletion custom_components/solarman/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async def _discover_all(self) -> dict:
async for item in self._discover([str(net.broadcast_address) for net in nets], True):
yield item

async def discover(self):
async def discover(self, ping_only = False):
_LOGGER.debug(f"discover")

self._devices = {}
Expand All @@ -76,6 +76,8 @@ async def discover(self):
self._devices = {item[0]: item[1] async for item in self._discover(self._ip)}
if len(self._devices) > 0 and not self._serial in self._devices:
self._devices = {}
if ping_only:
return self._devices

attempts_left = ACTION_ATTEMPTS
while len(self._devices) == 0 and attempts_left > 0:
Expand Down

0 comments on commit ee49a1f

Please sign in to comment.