From 50001684aa0cf9bc062874b1f247e84fc8a9b1ae Mon Sep 17 00:00:00 2001 From: jan iversen Date: Sun, 6 Jun 2021 09:13:50 +0200 Subject: [PATCH] Add retries/retry_on_empty configuration parameters to Modbus (#51412) * Add retries/retry_on_empty configuration parameters. * Please review comment. --- homeassistant/components/modbus/__init__.py | 4 ++++ homeassistant/components/modbus/const.py | 2 ++ homeassistant/components/modbus/modbus.py | 13 ++++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/modbus/__init__.py b/homeassistant/components/modbus/__init__.py index 8d00a1ba7c5..242b5e7a0ad 100644 --- a/homeassistant/components/modbus/__init__.py +++ b/homeassistant/components/modbus/__init__.py @@ -66,6 +66,8 @@ from .const import ( CONF_MIN_TEMP, CONF_PARITY, CONF_PRECISION, + CONF_RETRIES, + CONF_RETRY_ON_EMPTY, CONF_REVERSE_ORDER, CONF_SCALE, CONF_STATE_CLOSED, @@ -292,6 +294,8 @@ MODBUS_SCHEMA = vol.Schema( 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_RETRIES, default=3): cv.positive_int, + vol.Optional(CONF_RETRY_ON_EMPTY, default=False): cv.boolean, 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 cfda4a3863a..7074431b0a9 100644 --- a/homeassistant/components/modbus/const.py +++ b/homeassistant/components/modbus/const.py @@ -35,6 +35,8 @@ CONF_PARITY = "parity" CONF_REGISTER = "register" CONF_REGISTER_TYPE = "register_type" CONF_REGISTERS = "registers" +CONF_RETRIES = "retries" +CONF_RETRY_ON_EMPTY = "retry_on_empty" CONF_REVERSE_ORDER = "reverse_order" CONF_PRECISION = "precision" CONF_SCALE = "scale" diff --git a/homeassistant/components/modbus/modbus.py b/homeassistant/components/modbus/modbus.py index f75c30b363b..ce4ea8235b4 100644 --- a/homeassistant/components/modbus/modbus.py +++ b/homeassistant/components/modbus/modbus.py @@ -39,6 +39,8 @@ from .const import ( CONF_BYTESIZE, CONF_CLOSE_COMM_ON_ERROR, CONF_PARITY, + CONF_RETRIES, + CONF_RETRY_ON_EMPTY, CONF_STOPBITS, DEFAULT_HUB, MODBUS_DOMAIN as DOMAIN, @@ -149,6 +151,8 @@ class ModbusHub: 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] + self._config_retries = client_config[CONF_RETRIES] + self._config_retry_on_empty = client_config[CONF_RETRY_ON_EMPTY] Defaults.Timeout = client_config[CONF_TIMEOUT] if self._config_type == "serial": # serial configuration @@ -216,7 +220,8 @@ class ModbusHub: bytesize=self._config_bytesize, parity=self._config_parity, timeout=self._config_timeout, - retry_on_empty=True, + retries=self._config_retries, + retry_on_empty=self._config_retry_on_empty, reset_socket=self._config_reset_socket, ) elif self._config_type == "rtuovertcp": @@ -225,6 +230,8 @@ class ModbusHub: port=self._config_port, framer=ModbusRtuFramer, timeout=self._config_timeout, + retries=self._config_retries, + retry_on_empty=self._config_retry_on_empty, reset_socket=self._config_reset_socket, ) elif self._config_type == "tcp": @@ -232,6 +239,8 @@ class ModbusHub: host=self._config_host, port=self._config_port, timeout=self._config_timeout, + retries=self._config_retries, + retry_on_empty=self._config_retry_on_empty, reset_socket=self._config_reset_socket, ) elif self._config_type == "udp": @@ -239,6 +248,8 @@ class ModbusHub: host=self._config_host, port=self._config_port, timeout=self._config_timeout, + retries=self._config_retries, + retry_on_empty=self._config_retry_on_empty, reset_socket=self._config_reset_socket, ) except ModbusException as exception_error: