Skip to content

Commit 9ba84fb

Browse files
committed
Delay start after connection is made.
1 parent 3942df7 commit 9ba84fb

File tree

2 files changed

+8
-19
lines changed

2 files changed

+8
-19
lines changed

custom_components/modbus/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"issue_tracker": "https://github.com/pymodbus-dev/homeassistant_modbus/issues",
88
"loggers": ["pymodbus"],
99
"requirements": ["pymodbus==3.11.1"],
10-
"version": "1.0.2"
10+
"version": "1.0.3"
1111
}

custom_components/modbus/modbus.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828
CONF_TYPE,
2929
EVENT_HOMEASSISTANT_STOP,
3030
)
31-
from homeassistant.core import Event, HomeAssistant, ServiceCall, callback
31+
from homeassistant.core import Event, HomeAssistant, ServiceCall
3232
from homeassistant.helpers import config_validation as cv
3333
from homeassistant.helpers.discovery import async_load_platform
3434
from homeassistant.helpers.dispatcher import async_dispatcher_send
35-
from homeassistant.helpers.event import async_call_later
3635
from homeassistant.helpers.typing import ConfigType
3736
from homeassistant.util.hass_dict import HassKey
3837

@@ -317,12 +316,16 @@ async def async_pb_connect(self) -> None:
317316
try:
318317
await self._client.connect() # type: ignore[union-attr]
319318
except ModbusException as exception_error:
320-
err = f"{self.name} connect failed, retry in pymodbus ({exception_error!s})"
321-
self._log_error(err)
319+
self._log_error(
320+
f"{self.name} connect failed, please check your configuration ({exception_error!s})"
321+
)
322322
return
323323
message = f"modbus {self.name} communication open"
324324
_LOGGER.info(message)
325325

326+
if self._config_delay:
327+
await asyncio.sleep(self._config_delay)
328+
326329
async def async_setup(self) -> bool:
327330
"""Set up pymodbus client."""
328331
try:
@@ -340,20 +343,8 @@ async def async_setup(self) -> bool:
340343
self._connect_task = self.hass.async_create_background_task(
341344
self.async_pb_connect(), "modbus-connect"
342345
)
343-
344-
# Start counting down to allow modbus requests.
345-
if self._config_delay:
346-
self._async_cancel_listener = async_call_later(
347-
self.hass, self._config_delay, self.async_end_delay
348-
)
349346
return True
350347

351-
@callback
352-
def async_end_delay(self, args: Any) -> None:
353-
"""End startup delay."""
354-
self._async_cancel_listener = None
355-
self._config_delay = 0
356-
357348
async def async_restart(self) -> None:
358349
"""Reconnect client."""
359350
if self._client:
@@ -425,8 +416,6 @@ async def async_pb_call(
425416
use_call: str,
426417
) -> ModbusPDU | None:
427418
"""Convert async to sync pymodbus call."""
428-
if self._config_delay:
429-
return None
430419
async with self._lock:
431420
if not self._client:
432421
return None

0 commit comments

Comments
 (0)