Skip to content

Commit fb20343

Browse files
committed
Branch P5.
1 parent b8e9f50 commit fb20343

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

custom_components/modbus/entity.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,20 @@ def async_hold(self) -> None:
177177
self._attr_available = False
178178
self.async_write_ha_state()
179179

180+
async def async_await_connection(self, _now: Any) -> None:
181+
"""Wait for first connect."""
182+
await self._hub.event_connected.wait()
183+
# await self._async_update_if_not_in_progress()
184+
# self._async_schedule_future_update(0.0)
185+
self.async_run()
186+
180187
async def async_base_added_to_hass(self) -> None:
181188
"""Handle entity which will be added."""
182-
self.async_run()
189+
async_call_later(
190+
self.hass,
191+
self._hub.config_delay + 0.1,
192+
self.async_await_connection,
193+
)
183194
self.async_on_remove(
184195
async_dispatcher_connect(self.hass, SIGNAL_STOP_ENTITY, self.async_hold)
185196
)

custom_components/modbus/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
"iot_class": "local_polling",
77
"loggers": ["pymodbus"],
88
"requirements": ["pymodbus==3.9.2"],
9-
"version": "1.4.0"
9+
"version": "1.5.0"
1010
}

custom_components/modbus/modbus.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import asyncio
66
from collections import namedtuple
7-
from collections.abc import Callable
87
from typing import Any
98

109
from pymodbus.client import (
@@ -28,11 +27,10 @@
2827
CONF_TYPE,
2928
EVENT_HOMEASSISTANT_STOP,
3029
)
31-
from homeassistant.core import Event, HomeAssistant, ServiceCall, callback
30+
from homeassistant.core import Event, HomeAssistant, ServiceCall
3231
from homeassistant.helpers import config_validation as cv
3332
from homeassistant.helpers.discovery import async_load_platform
3433
from homeassistant.helpers.dispatcher import async_dispatcher_send
35-
from homeassistant.helpers.event import async_call_later
3634
from homeassistant.helpers.typing import ConfigType
3735
from homeassistant.util.hass_dict import HassKey
3836

@@ -254,13 +252,13 @@ def __init__(self, hass: HomeAssistant, client_config: dict[str, Any]) -> None:
254252
self._client: (
255253
AsyncModbusSerialClient | AsyncModbusTcpClient | AsyncModbusUdpClient | None
256254
) = None
257-
self._async_cancel_listener: Callable[[], None] | None = None
258255
self._in_error = False
259256
self._lock = asyncio.Lock()
257+
self.event_connected = asyncio.Event()
260258
self.hass = hass
261259
self.name = client_config[CONF_NAME]
262260
self._config_type = client_config[CONF_TYPE]
263-
self._config_delay = client_config[CONF_DELAY]
261+
self.config_delay = client_config[CONF_DELAY]
264262
self._pb_request: dict[str, RunEntry] = {}
265263
self._connect_task: asyncio.Task
266264
self._last_log_error: str = ""
@@ -325,10 +323,10 @@ async def async_pb_connect(self) -> None:
325323
_LOGGER.info(message)
326324

327325
# 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()
332330

333331
async def async_setup(self) -> bool:
334332
"""Set up pymodbus client."""
@@ -349,12 +347,6 @@ async def async_setup(self) -> bool:
349347
)
350348
return True
351349

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-
358350
async def async_restart(self) -> None:
359351
"""Reconnect client."""
360352
if self._client:
@@ -364,9 +356,6 @@ async def async_restart(self) -> None:
364356

365357
async def async_close(self) -> None:
366358
"""Disconnect client."""
367-
if self._async_cancel_listener:
368-
self._async_cancel_listener()
369-
self._async_cancel_listener = None
370359
if not self._connect_task.done():
371360
self._connect_task.cancel()
372361

@@ -426,8 +415,6 @@ async def async_pb_call(
426415
use_call: str,
427416
) -> ModbusPDU | None:
428417
"""Convert async to sync pymodbus call."""
429-
if self._config_delay:
430-
return None
431418
async with self._lock:
432419
if not self._client:
433420
return None

0 commit comments

Comments
 (0)