Skip to content

Commit ce5af25

Browse files
Merge pull request #713 from WillCodeForCats/modbus-exceptions-constants
ModbusExceptions constant class
2 parents 0b1d839 + b3b3990 commit ce5af25

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

custom_components/solaredge_modbus_multi/const.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@
2828
)
2929

3030

31+
class ModbusExceptions:
32+
"""An enumeration of the valid modbus exceptions."""
33+
34+
"""
35+
Copied from pymodbus source:
36+
https://github.com/pymodbus-dev/pymodbus/blob/a1c14c7a8fbea52618ba1cbc9933c1dd24c3339d/pymodbus/pdu/pdu.py#L72
37+
"""
38+
39+
IllegalFunction = 0x01
40+
IllegalAddress = 0x02
41+
IllegalValue = 0x03
42+
SlaveFailure = 0x04
43+
Acknowledge = 0x05
44+
SlaveBusy = 0x06
45+
NegativeAcknowledge = 0x07
46+
MemoryParityError = 0x08
47+
GatewayPathUnavailable = 0x0A
48+
GatewayNoResponse = 0x0B
49+
50+
3151
class RetrySettings(IntEnum):
3252
"""Retry settings when opening a connection to the inverter fails."""
3353

custom_components/solaredge_modbus_multi/hub.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pymodbus.constants import Endian
1515
from pymodbus.exceptions import ConnectionException, ModbusIOException
1616
from pymodbus.payload import BinaryPayloadDecoder
17-
from pymodbus.pdu import ExceptionResponse, ModbusExceptions
17+
from pymodbus.pdu import ExceptionResponse
1818

1919
from .const import (
2020
BATTERY_REG_BASE,
@@ -25,6 +25,7 @@
2525
ConfDefaultStr,
2626
ConfName,
2727
ModbusDefaults,
28+
ModbusExceptions,
2829
RetrySettings,
2930
SolarEdgeTimeouts,
3031
SunSpecNotImpl,
@@ -506,19 +507,21 @@ async def modbus_read_holding_registers(self, unit, address, rcount):
506507
)
507508

508509
if result.isError():
509-
_LOGGER.debug(f"Unit {unit}: {result}")
510510

511511
if type(result) is ModbusIOException:
512512
raise ModbusIOError(result)
513513

514514
if type(result) is ExceptionResponse:
515515
if result.exception_code == ModbusExceptions.IllegalAddress:
516+
_LOGGER.debug(f"Unit {unit} Read IllegalAddress: {result}")
516517
raise ModbusIllegalAddress(result)
517518

518519
if result.exception_code == ModbusExceptions.IllegalFunction:
520+
_LOGGER.debug(f"Unit {unit} Read IllegalFunction: {result}")
519521
raise ModbusIllegalFunction(result)
520522

521523
if result.exception_code == ModbusExceptions.IllegalValue:
524+
_LOGGER.debug(f"Unit {unit} Read IllegalValue: {result}")
522525
raise ModbusIllegalValue(result)
523526

524527
raise ModbusReadError(result)
@@ -592,19 +595,25 @@ async def write_registers(self, unit: int, address: int, payload) -> None:
592595

593596
if type(result) is ExceptionResponse:
594597
if result.exception_code == ModbusExceptions.IllegalAddress:
595-
_LOGGER.debug(f"Write IllegalAddress: {result}")
598+
_LOGGER.debug(
599+
f"Unit {self._wr_unit} Write IllegalAddress: {result}"
600+
)
596601
raise HomeAssistantError(
597602
"Address not supported at device at ID {self._wr_unit}."
598603
)
599604

600605
if result.exception_code == ModbusExceptions.IllegalFunction:
601-
_LOGGER.debug(f"Write IllegalFunction: {result}")
606+
_LOGGER.debug(
607+
f"Unit {self._wr_unit} Write IllegalFunction: {result}"
608+
)
602609
raise HomeAssistantError(
603610
"Function not supported by device at ID {self._wr_unit}."
604611
)
605612

606613
if result.exception_code == ModbusExceptions.IllegalValue:
607-
_LOGGER.debug(f"Write IllegalValue: {result}")
614+
_LOGGER.debug(
615+
f"Unit {self._wr_unit} Write IllegalValue: {result}"
616+
)
608617
raise HomeAssistantError(
609618
"Value invalid for device at ID {self._wr_unit}."
610619
)

0 commit comments

Comments
 (0)