From dd7fbef948b6496a2662ddfbdb6c9c509e34aa27 Mon Sep 17 00:00:00 2001 From: jan iversen Date: Thu, 9 Apr 2020 13:53:23 +0200 Subject: [PATCH] Fix modbus default delay (#33877) * solve modbus issue #33872 CONF_DELAY was used in a serial connection, which is not permitted. Sometimes async_update is called after async_setup is completed, but before event EVENT_HOMEASSISTANT_START is issued, leading to a missing object. * resolve review comment. Do not wait for start event, but activate pymodbus directly in async setup. * review 2 Remark, this does not work, async_setup hangs. clean start_modbus() from async calls, leaving only the pymodbus setup. * review 2a Moved listen_once back to start_modbus, since it is sync. --- homeassistant/components/modbus/__init__.py | 33 ++++++++++----------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/modbus/__init__.py b/homeassistant/components/modbus/__init__.py index 6bb6c7fb1a2..3c488bd3245 100644 --- a/homeassistant/components/modbus/__init__.py +++ b/homeassistant/components/modbus/__init__.py @@ -21,7 +21,6 @@ from homeassistant.const import ( CONF_PORT, CONF_TIMEOUT, CONF_TYPE, - EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, ) import homeassistant.helpers.config_validation as cv @@ -106,27 +105,13 @@ async def async_setup(hass, config): for client in hub_collect.values(): del client - def start_modbus(event): + def start_modbus(): """Start Modbus service.""" for client in hub_collect.values(): client.setup() hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_modbus) - # Register services for modbus - hass.services.async_register( - MODBUS_DOMAIN, - SERVICE_WRITE_REGISTER, - write_register, - schema=SERVICE_WRITE_REGISTER_SCHEMA, - ) - hass.services.async_register( - MODBUS_DOMAIN, - SERVICE_WRITE_COIL, - write_coil, - schema=SERVICE_WRITE_COIL_SCHEMA, - ) - async def write_register(service): """Write Modbus registers.""" unit = int(float(service.data[ATTR_UNIT])) @@ -150,8 +135,19 @@ async def async_setup(hass, config): client_name = service.data[ATTR_HUB] await hub_collect[client_name].write_coil(unit, address, state) - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, start_modbus) + # do not wait for EVENT_HOMEASSISTANT_START, activate pymodbus now + await hass.async_add_executor_job(start_modbus) + # Register services for modbus + hass.services.async_register( + MODBUS_DOMAIN, + SERVICE_WRITE_REGISTER, + write_register, + schema=SERVICE_WRITE_REGISTER_SCHEMA, + ) + hass.services.async_register( + MODBUS_DOMAIN, SERVICE_WRITE_COIL, write_coil, schema=SERVICE_WRITE_COIL_SCHEMA, + ) return True @@ -169,7 +165,7 @@ class ModbusHub: self._config_type = client_config[CONF_TYPE] self._config_port = client_config[CONF_PORT] self._config_timeout = client_config[CONF_TIMEOUT] - self._config_delay = client_config[CONF_DELAY] + self._config_delay = 0 if self._config_type == "serial": # serial configuration @@ -181,6 +177,7 @@ class ModbusHub: else: # network configuration self._config_host = client_config[CONF_HOST] + self._config_delay = client_config[CONF_DELAY] @property def name(self):