diff --git a/.coveragerc b/.coveragerc index 181c551e02a..6ad158066df 100644 --- a/.coveragerc +++ b/.coveragerc @@ -620,6 +620,7 @@ omit = homeassistant/components/mochad/* homeassistant/components/modbus/climate.py homeassistant/components/modbus/cover.py + homeassistant/components/modbus/modbus.py homeassistant/components/modbus/switch.py homeassistant/components/modem_callerid/sensor.py homeassistant/components/motion_blinds/__init__.py diff --git a/homeassistant/components/modbus/modbus.py b/homeassistant/components/modbus/modbus.py index ad53bd2aa53..f04c019e6a6 100644 --- a/homeassistant/components/modbus/modbus.py +++ b/homeassistant/components/modbus/modbus.py @@ -5,6 +5,7 @@ import threading from pymodbus.client.sync import ModbusSerialClient, ModbusTcpClient, ModbusUdpClient from pymodbus.constants import Defaults from pymodbus.exceptions import ModbusException +from pymodbus.pdu import ExceptionResponse, IllegalFunctionRequest from pymodbus.transaction import ModbusRtuFramer from homeassistant.const import ( @@ -161,10 +162,11 @@ class ModbusHub: return self._config_name def _log_error(self, exception_error: ModbusException, error_state=True): + log_text = "Pymodbus: " + str(exception_error) if self._in_error: - _LOGGER.debug(str(exception_error)) + _LOGGER.debug(log_text) else: - _LOGGER.error(str(exception_error)) + _LOGGER.error(log_text) self._in_error = error_state def setup(self): @@ -236,6 +238,9 @@ class ModbusHub: except ModbusException as exception_error: self._log_error(exception_error) return None + if isinstance(result, (ExceptionResponse, IllegalFunctionRequest)): + self._log_error(result) + return None self._in_error = False return result @@ -248,6 +253,9 @@ class ModbusHub: except ModbusException as exception_error: self._log_error(exception_error) return None + if isinstance(result, (ExceptionResponse, IllegalFunctionRequest)): + self._log_error(result) + return None self._in_error = False return result @@ -260,6 +268,9 @@ class ModbusHub: except ModbusException as exception_error: self._log_error(exception_error) return None + if isinstance(result, (ExceptionResponse, IllegalFunctionRequest)): + self._log_error(result) + return None self._in_error = False return result @@ -272,6 +283,9 @@ class ModbusHub: except ModbusException as exception_error: self._log_error(exception_error) return None + if isinstance(result, (ExceptionResponse, IllegalFunctionRequest)): + self._log_error(result) + return None self._in_error = False return result @@ -280,10 +294,13 @@ class ModbusHub: with self._lock: kwargs = {"unit": unit} if unit else {} try: - self._client.write_coil(address, value, **kwargs) + result = self._client.write_coil(address, value, **kwargs) except ModbusException as exception_error: self._log_error(exception_error) return False + if isinstance(result, (ExceptionResponse, IllegalFunctionRequest)): + self._log_error(result) + return False self._in_error = False return True @@ -292,10 +309,13 @@ class ModbusHub: with self._lock: kwargs = {"unit": unit} if unit else {} try: - self._client.write_coils(address, values, **kwargs) + result = self._client.write_coils(address, values, **kwargs) except ModbusException as exception_error: self._log_error(exception_error) return False + if isinstance(result, (ExceptionResponse, IllegalFunctionRequest)): + self._log_error(result) + return False self._in_error = False return True @@ -304,10 +324,13 @@ class ModbusHub: with self._lock: kwargs = {"unit": unit} if unit else {} try: - self._client.write_register(address, value, **kwargs) + result = self._client.write_register(address, value, **kwargs) except ModbusException as exception_error: self._log_error(exception_error) return False + if isinstance(result, (ExceptionResponse, IllegalFunctionRequest)): + self._log_error(result) + return False self._in_error = False return True @@ -316,9 +339,12 @@ class ModbusHub: with self._lock: kwargs = {"unit": unit} if unit else {} try: - self._client.write_registers(address, values, **kwargs) + result = self._client.write_registers(address, values, **kwargs) except ModbusException as exception_error: self._log_error(exception_error) return False + if isinstance(result, (ExceptionResponse, IllegalFunctionRequest)): + self._log_error(result) + return False self._in_error = False return True