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: diff --git a/custom_components/solarman/provider.py b/custom_components/solarman/provider.py index 9f9c50b..7c2fb13 100644 --- a/custom_components/solarman/provider.py +++ b/custom_components/solarman/provider.py @@ -88,8 +88,8 @@ def ipaddress(self): except AddressValueError: return IPv4Address(socket.gethostbyname(self.host)) - async def discover(self): - if self.ipaddress.is_private and (discover := await InverterDiscovery(self.hass, self.host, self.serial).discover()): + async def discover(self, ping_only = False): + if self.ipaddress.is_private and (discover := await InverterDiscovery(self.hass, self.host, self.serial).discover(ping_only)): if (device := discover.get(self.serial)) is not None: self.host = device["ip"] self.mac = device["mac"] @@ -112,7 +112,7 @@ def auto(self) -> bool: @cached_property def attributes(self) -> str: - return {ATTR_[k]: int(self._additional_options.get(k, DEFAULT_[k])) for k in ATTR_} + return {ATTR_[k]: int(self._additional_options.get(k, DEFAULT_[k])) for k in ATTR_} | {kv[0]: kv[1] for v in value.split(';') if (kv := v.split('=', 1)) and len(kv) > 1} if (value := self._additional_options.get(CONF_CUSTOM, DEFAULT_[CONF_CUSTOM])) else {} async def resolve(self, request: Callable[[], Awaitable[None]] | None = None): _LOGGER.debug(f"Device autodetection is {"enabled" if self.auto and request else f"disabled. Selected profile: {self.filename}"}")