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 a358216
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 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
6 changes: 3 additions & 3 deletions custom_components/solarman/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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}"}")
Expand Down

0 comments on commit a358216

Please sign in to comment.