4
4
5
5
import asyncio
6
6
from collections import namedtuple
7
- from collections .abc import Callable
8
7
from typing import Any
9
8
10
9
from pymodbus .client import (
28
27
CONF_TYPE ,
29
28
EVENT_HOMEASSISTANT_STOP ,
30
29
)
31
- from homeassistant .core import Event , HomeAssistant , ServiceCall , callback
30
+ from homeassistant .core import Event , HomeAssistant , ServiceCall
32
31
from homeassistant .helpers import config_validation as cv
33
32
from homeassistant .helpers .discovery import async_load_platform
34
33
from homeassistant .helpers .dispatcher import async_dispatcher_send
35
- from homeassistant .helpers .event import async_call_later
36
34
from homeassistant .helpers .typing import ConfigType
37
35
from homeassistant .util .hass_dict import HassKey
38
36
@@ -254,13 +252,13 @@ def __init__(self, hass: HomeAssistant, client_config: dict[str, Any]) -> None:
254
252
self ._client : (
255
253
AsyncModbusSerialClient | AsyncModbusTcpClient | AsyncModbusUdpClient | None
256
254
) = None
257
- self ._async_cancel_listener : Callable [[], None ] | None = None
258
255
self ._in_error = False
259
256
self ._lock = asyncio .Lock ()
257
+ self .event_connected = asyncio .Event ()
260
258
self .hass = hass
261
259
self .name = client_config [CONF_NAME ]
262
260
self ._config_type = client_config [CONF_TYPE ]
263
- self ._config_delay = client_config [CONF_DELAY ]
261
+ self .config_delay = client_config [CONF_DELAY ]
264
262
self ._pb_request : dict [str , RunEntry ] = {}
265
263
self ._connect_task : asyncio .Task
266
264
self ._last_log_error : str = ""
@@ -325,10 +323,10 @@ async def async_pb_connect(self) -> None:
325
323
_LOGGER .info (message )
326
324
327
325
# Start counting down to allow modbus requests.
328
- if self ._config_delay :
329
- self . _async_cancel_listener = async_call_later (
330
- self .hass , self . _config_delay , self . async_end_delay
331
- )
326
+ if self .config_delay :
327
+ await asyncio . sleep ( self . config_delay )
328
+ self .config_delay = 0
329
+ self . event_connected . set ( )
332
330
333
331
async def async_setup (self ) -> bool :
334
332
"""Set up pymodbus client."""
@@ -349,12 +347,6 @@ async def async_setup(self) -> bool:
349
347
)
350
348
return True
351
349
352
- @callback
353
- def async_end_delay (self , args : Any ) -> None :
354
- """End startup delay."""
355
- self ._async_cancel_listener = None
356
- self ._config_delay = 0
357
-
358
350
async def async_restart (self ) -> None :
359
351
"""Reconnect client."""
360
352
if self ._client :
@@ -364,9 +356,6 @@ async def async_restart(self) -> None:
364
356
365
357
async def async_close (self ) -> None :
366
358
"""Disconnect client."""
367
- if self ._async_cancel_listener :
368
- self ._async_cancel_listener ()
369
- self ._async_cancel_listener = None
370
359
if not self ._connect_task .done ():
371
360
self ._connect_task .cancel ()
372
361
@@ -426,8 +415,6 @@ async def async_pb_call(
426
415
use_call : str ,
427
416
) -> ModbusPDU | None :
428
417
"""Convert async to sync pymodbus call."""
429
- if self ._config_delay :
430
- return None
431
418
async with self ._lock :
432
419
if not self ._client :
433
420
return None
0 commit comments