From ee49a1fe76ff6be69a3e8d0091b14c46e9321721 Mon Sep 17 00:00:00 2001 From: David Rapan Date: Thu, 26 Dec 2024 15:48:34 +0100 Subject: [PATCH] refactor: Use of discovery as a ping only during main loop --- custom_components/solarman/api.py | 4 ++-- custom_components/solarman/discovery.py | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/custom_components/solarman/api.py b/custom_components/solarman/api.py index caa8cef..22aa31c 100644 --- a/custom_components/solarman/api.py +++ b/custom_components/solarman/api.py @@ -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 @@ -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() diff --git a/custom_components/solarman/discovery.py b/custom_components/solarman/discovery.py index de1837f..307451b 100644 --- a/custom_components/solarman/discovery.py +++ b/custom_components/solarman/discovery.py @@ -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 = {} @@ -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: