Skip to content

Commit

Permalink
add reconnection logic to write registers as well
Browse files Browse the repository at this point in the history
  • Loading branch information
sfstar committed Feb 28, 2025
1 parent e431274 commit 8c82c44
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions custom_components/victron/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ def disconnect(self):

def write_register(self, unit, address, value):
"""Write a register."""
slave = int(unit) if unit else 1
return self._client.write_register(address=address, value=value, slave=slave)
try:
slave = int(unit) if unit else 1
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."""
Expand All @@ -60,10 +63,15 @@ def read_holding_registers(self, unit, address, count):
)
return result

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

View workflow job for this annotation

GitHub Actions / ruff

custom_components/victron/hub.py:64:20: RET504 Unnecessary assignment to `result` before `return` statement

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

View workflow job for this annotation

GitHub Actions / ruff

custom_components/victron/hub.py:64:13: TRY300 Consider moving this statement to an `else` block
except BrokenPipeError:
_LOGGER.warning("connection to the remote gx device is broken, trying to reconnect")
if not self.is_still_connected():
self.connect()
_LOGGER.info("connection to GX device re-established")
self.handle_broken_pipe_error()
return None


def handle_broken_pipe_error(self):

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

View workflow job for this annotation

GitHub Actions / ruff

custom_components/victron/hub.py:70:9: D102 Missing docstring in public method
_LOGGER.warning("connection to the remote gx device is broken, trying to reconnect")
if not self.is_still_connected():
self.connect()
_LOGGER.info("connection to GX device re-established")

def calculate_register_count(self, registerInfoDict: OrderedDict):
"""Calculate the number of registers to read."""
Expand Down Expand Up @@ -97,27 +105,19 @@ def determine_present_devices(self):
if unit == 100 and not key.startswith(("settings", "system")):
continue

try:
address = self.get_first_register_id(register_definition)
count = self.calculate_register_count(register_definition)
result = self.read_holding_registers(unit, address, count)
if result.isError():
_LOGGER.debug(
"result is error for unit: %s address: %s count: %s and result: %s",
unit,
address,
count,
result,
)
else:
working_registers.append(key)

except BrokenPipeError:
print("The pipe is broken. Handling the error.")
# Handle the error, e.g., clean up or retry the operation

except HomeAssistantError as e:
_LOGGER.error(e)
address = self.get_first_register_id(register_definition)
count = self.calculate_register_count(register_definition)
result = self.read_holding_registers(unit, address, count)
if result is None:
_LOGGER.debug(
"result is error for unit: %s address: %s count: %s and result: %s",
unit,
address,
count,
result,
)
else:
working_registers.append(key)

if len(working_registers) > 0:
valid_devices[unit] = working_registers
Expand Down

0 comments on commit 8c82c44

Please sign in to comment.