Skip to content

Commit

Permalink
add faster connection failure as well as more verbose logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sfstar committed Feb 28, 2025
1 parent 8dac4bb commit 02f443e
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions custom_components/victron/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ def __init__(self, host: str, port: int) -> None:
self.host = host
self.port = port
# Fail more quickly and only retry once before executing retry logic
self._client = ModbusTcpClient(host=self.host, port=self.port, timeout=10, retries=1)
self._client = ModbusTcpClient(
host=self.host, port=self.port, timeout=10, retries=1
)
self._lock = threading.Lock()

def is_still_connected(self):
Expand All @@ -47,21 +49,38 @@ def disconnect(self):
def write_register(self, unit, address, value):
"""Write a register."""
try:
slave = int(unit) if unit else 1
slave = int(unit)
if not unit:
_LOGGER.error(
"Unit for this device (%s) isn't set correctly. Cannot write (%s) to register (%s). Ensure that the config was migrated to latest state by forcing a rescan",
unit,
value,
address,
)
return
self._client.write_register(address=address, value=value, slave=slave)
except BrokenPipeError:
self.__handle_broken_pipe_error()

def read_holding_registers(self, unit, address, count):
"""Read holding registers."""
try:
slave = int(unit) if unit else 1
if not unit:
return None

slave = int(unit)
return self._client.read_holding_registers(
address=address, count=count, slave=slave
)
except BrokenPipeError:
self.__handle_broken_pipe_error()
return None
except ValueError as e:

Check failure on line 78 in custom_components/victron/hub.py

View workflow job for this annotation

GitHub Actions / ruff

custom_components/victron/hub.py:78:30: F841 Local variable `e` is assigned to but never used
_LOGGER.error(
"Unit for this device (%s) isn't set correctly. Cannot read register (%s). Ensure that the config was migrated to latest state by forcing a rescan",
unit,
address,
)

def __handle_broken_pipe_error(self):
_LOGGER.warning(
Expand Down

0 comments on commit 02f443e

Please sign in to comment.