Skip to content

Commit

Permalink
Modbus: make delay configurable
Browse files Browse the repository at this point in the history
Add optional parameter "delay" to Modbus configuration

Default is 0, which means do not execute time.sleep
  • Loading branch information
janiversen committed Mar 11, 2020
1 parent e36b304 commit fbfc845
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions homeassistant/components/modbus/__init__.py
Expand Up @@ -9,6 +9,7 @@

from homeassistant.const import (
ATTR_STATE,
CONF_DELAY,
CONF_HOST,
CONF_METHOD,
CONF_NAME,
Expand Down Expand Up @@ -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,
}
)

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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."""
Expand Down

0 comments on commit fbfc845

Please sign in to comment.