Skip to content

Commit 58046c2

Browse files
committed
update node loading strategy, maybe this can be improved to load instantly after discover happens
1 parent 5c48e02 commit 58046c2

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

plugwise_usb/network/__init__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,10 @@ async def _load_discovered_nodes(self) -> bool:
482482
)
483483
result_index += 1
484484
_LOGGER.debug("_load_discovered_nodes | END")
485+
if not all(load_result) and (
486+
self._load_stragglers_task is None or self._load_stragglers_task.done()
487+
):
488+
self._load_stragglers_task = create_task(self._load_stragglers())
485489
return all(load_result)
486490

487491
async def _unload_discovered_nodes(self) -> None:
@@ -491,9 +495,11 @@ async def _unload_discovered_nodes(self) -> None:
491495
# endregion
492496

493497
# region - Network instance
494-
async def start(self) -> None:
498+
async def start(self, load: bool = True) -> None:
495499
"""Start and activate network."""
496500
self._register.start_node_discover(self._discover_node)
501+
if load:
502+
self._register.scan_completed_callback(self._load_discovered_nodes)
497503
await self._register.start()
498504
self._subscribe_to_protocol_events()
499505
await self._subscribe_to_node_events()
@@ -508,10 +514,8 @@ async def discover_nodes(self, load: bool = True) -> bool:
508514
"""Discover nodes."""
509515
await self.discover_network_coordinator(load=load)
510516
if not self._is_running:
511-
await self.start()
512-
if load and not await self._load_discovered_nodes():
513-
self._load_stragglers_task = create_task(self._load_stragglers())
514-
return False
517+
await self.start(load=load)
518+
return True
515519

516520
return True
517521

plugwise_usb/network/registry.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def __init__(
5050
self._full_scan_finished: Callable[[], Awaitable[None]] | None = None
5151
self._registration_scan_delay: float = CIRCLEPLUS_SCANREQUEST_MAINTENANCE
5252
self._scan_completed = False
53+
self._scan_completed_callback: Callable[[], Awaitable[None]] | None = None
5354

5455
# region Properties
5556

@@ -102,6 +103,10 @@ def start_node_discover(self, callback: Callable[[], Awaitable[None]]) -> None:
102103
"""Register method to be called when a node is found."""
103104
self._start_node_discover = callback
104105

106+
def scan_completed_callback(self, callback: Callable[[], Awaitable[None]]) -> None:
107+
"""Register method to be called when a node is found."""
108+
self._scan_completed_callback = callback
109+
105110
def full_scan_finished(self, callback: Callable[[], Awaitable[None]]) -> None:
106111
"""Register method to be called when full scan is finished."""
107112
self._full_scan_finished = callback
@@ -200,6 +205,8 @@ async def update_missing_registrations_circleplus(self) -> None:
200205
self._scan_completed = True
201206
if self._network_cache is not None:
202207
await self._network_cache.prune_cache(_maintenance_registry)
208+
if self._scan_completed_callback is not None:
209+
await self._scan_completed_callback()
203210

204211
async def load_registrations_from_cache(self) -> None:
205212
"""Quick retrieval of all unknown network registrations from cache."""
@@ -216,6 +223,8 @@ async def load_registrations_from_cache(self) -> None:
216223
await self._start_node_discover(mac, nodetype, True)
217224
await sleep(0.1)
218225
_LOGGER.info("Cache network registration discovery finished")
226+
if self._scan_completed_callback is not None:
227+
await self._scan_completed_callback()
219228

220229
async def update_node_registration(self, mac: str) -> bool:
221230
"""Register (re)joined node to Plugwise network and return network address."""

0 commit comments

Comments
 (0)