diff --git a/homeassistant/components/modbus/__init__.py b/homeassistant/components/modbus/__init__.py index 8e0d9220718..8b9e37aa1e4 100644 --- a/homeassistant/components/modbus/__init__.py +++ b/homeassistant/components/modbus/__init__.py @@ -57,6 +57,7 @@ from .const import ( CONF_BAUDRATE, CONF_BYTESIZE, CONF_CLIMATES, + CONF_CLOSE_COMM_ON_ERROR, CONF_CURRENT_TEMP, CONF_CURRENT_TEMP_REGISTER_TYPE, CONF_DATA_COUNT, @@ -281,6 +282,7 @@ MODBUS_SCHEMA = vol.Schema( { vol.Optional(CONF_NAME, default=DEFAULT_HUB): cv.string, vol.Optional(CONF_TIMEOUT, default=3): cv.socket_timeout, + vol.Optional(CONF_CLOSE_COMM_ON_ERROR, default=True): cv.boolean, vol.Optional(CONF_DELAY, default=0): cv.positive_int, vol.Optional(CONF_BINARY_SENSORS): vol.All( cv.ensure_list, [BINARY_SENSOR_SCHEMA] diff --git a/homeassistant/components/modbus/const.py b/homeassistant/components/modbus/const.py index d817108b4d3..15de884d5b9 100644 --- a/homeassistant/components/modbus/const.py +++ b/homeassistant/components/modbus/const.py @@ -16,6 +16,7 @@ from homeassistant.const import ( CONF_BAUDRATE = "baudrate" CONF_BYTESIZE = "bytesize" CONF_CLIMATES = "climates" +CONF_CLOSE_COMM_ON_ERROR = "close_comm_on_error" CONF_COILS = "coils" CONF_CURRENT_TEMP = "current_temp_register" CONF_CURRENT_TEMP_REGISTER_TYPE = "current_temp_register_type" diff --git a/homeassistant/components/modbus/modbus.py b/homeassistant/components/modbus/modbus.py index d9db6cf980c..8610c6a855c 100644 --- a/homeassistant/components/modbus/modbus.py +++ b/homeassistant/components/modbus/modbus.py @@ -28,6 +28,7 @@ from .const import ( ATTR_VALUE, CONF_BAUDRATE, CONF_BYTESIZE, + CONF_CLOSE_COMM_ON_ERROR, CONF_PARITY, CONF_STOPBITS, DEFAULT_HUB, @@ -125,6 +126,7 @@ class ModbusHub: self._config_port = client_config[CONF_PORT] self._config_timeout = client_config[CONF_TIMEOUT] self._config_delay = client_config[CONF_DELAY] + self._config_reset_socket = client_config[CONF_CLOSE_COMM_ON_ERROR] Defaults.Timeout = client_config[CONF_TIMEOUT] if self._config_type == "serial": # serial configuration @@ -163,6 +165,7 @@ class ModbusHub: parity=self._config_parity, timeout=self._config_timeout, retry_on_empty=True, + reset_socket=self._config_reset_socket, ) elif self._config_type == "rtuovertcp": self._client = ModbusTcpClient( @@ -170,18 +173,21 @@ class ModbusHub: port=self._config_port, framer=ModbusRtuFramer, timeout=self._config_timeout, + reset_socket=self._config_reset_socket, ) elif self._config_type == "tcp": self._client = ModbusTcpClient( host=self._config_host, port=self._config_port, timeout=self._config_timeout, + reset_socket=self._config_reset_socket, ) elif self._config_type == "udp": self._client = ModbusUdpClient( host=self._config_host, port=self._config_port, timeout=self._config_timeout, + reset_socket=self._config_reset_socket, ) except ModbusException as exception_error: self._log_error(exception_error, error_state=False)