From 477bdbf5350df291dbdd48df36bc6441d0caa9e7 Mon Sep 17 00:00:00 2001 From: jan Iversen Date: Sat, 7 Mar 2020 16:19:53 +0100 Subject: [PATCH] modbus: make delay configurable Add optional parameter "delay" to modbus configuration Default is 0, which means do not execute time.sleep --- homeassistant/components/modbus/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/modbus/__init__.py b/homeassistant/components/modbus/__init__.py index 2a51966da1fc31..e974cd98e1c7ee 100644 --- a/homeassistant/components/modbus/__init__.py +++ b/homeassistant/components/modbus/__init__.py @@ -9,6 +9,7 @@ from homeassistant.const import ( ATTR_STATE, + CONF_DELAY, CONF_HOST, CONF_METHOD, CONF_NAME, @@ -60,6 +61,7 @@ vol.Required(CONF_PORT): cv.port, vol.Required(CONF_TYPE): vol.Any("tcp", "udp", "rtuovertcp"), vol.Optional(CONF_TIMEOUT, default=3): cv.socket_timeout, + vol.Optional(CONF_DELAY, default=0): cv.positive_int, } ) @@ -132,7 +134,8 @@ def setup(hass, config): for client_config in config[DOMAIN]: client = setup_client(client_config) name = client_config[CONF_NAME] - hub_collect[name] = ModbusHub(client, name) + delay = client_config[CONF_DELAY] + hub_collect[name] = ModbusHub(client, name, delay) _LOGGER.debug("Setting up hub: %s", client_config) def stop_modbus(event): @@ -187,11 +190,12 @@ def write_coil(service): class ModbusHub: """Thread safe wrapper class for pymodbus.""" - def __init__(self, modbus_client, name): + def __init__(self, modbus_client, name, delay): """Initialize the Modbus hub.""" self._client = modbus_client self._lock = threading.Lock() self._name = name + self._delay = delay @property def name(self): @@ -207,7 +211,8 @@ def connect(self): """Connect client.""" with self._lock: self._client.connect() - time.sleep(2) + if self._delay > 0: + time.sleep(self._delay) def read_coils(self, unit, address, count): """Read coils."""