Skip to content

Commit 3bdb00b

Browse files
committed
Make sure a node loads from cache even if it is offline.
If loading from cache fails while node is offline, retry loading from network_init.
1 parent 0581596 commit 3bdb00b

File tree

3 files changed

+38
-42
lines changed

3 files changed

+38
-42
lines changed

plugwise_usb/network/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def __init__(
7474
self._discover_sed_tasks: dict[str, Task[bool]] = {}
7575
self._registry_stragglers: dict[int, str] = {}
7676
self._discover_stragglers_task: Task[None] | None = None
77+
self._load_stragglers_task: Task[None] | None = None
7778

7879
# region - Properties
7980

@@ -483,6 +484,12 @@ async def _load_node(self, mac: str) -> bool:
483484
return True
484485
return False
485486

487+
async def _discover_stragglers(self) -> None:
488+
"""Retry failed load operation."""
489+
await sleep(60)
490+
while not self._load_discovered_nodes():
491+
await sleep(60)
492+
486493
async def _load_discovered_nodes(self) -> bool:
487494
"""Load all nodes currently discovered."""
488495
_LOGGER.debug("_load_discovered_nodes | START | %s", len(self._nodes))
@@ -532,7 +539,9 @@ async def discover_nodes(self, load: bool = True) -> bool:
532539
await self.start()
533540
await self._discover_registered_nodes()
534541
if load:
535-
return await self._load_discovered_nodes()
542+
if not await self._load_discovered_nodes():
543+
self._load_stragglers_task = create_task(self._load_stragglers())
544+
return False
536545

537546
return True
538547

plugwise_usb/nodes/circle.py

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -884,48 +884,30 @@ async def load(self) -> bool:
884884
_LOGGER.debug("Loading Circle node %s from cache", self._mac_in_str)
885885
if await self._load_from_cache():
886886
self._loaded = True
887-
self._setup_protocol(
888-
CIRCLE_FIRMWARE_SUPPORT,
889-
(
890-
NodeFeature.CIRCLE,
891-
NodeFeature.RELAY,
892-
NodeFeature.RELAY_INIT,
893-
NodeFeature.RELAY_LOCK,
894-
NodeFeature.ENERGY,
895-
NodeFeature.POWER,
896-
),
897-
)
898-
if await self.initialize():
899-
await self._loaded_callback(NodeEvent.LOADED, self.mac)
900-
return True
887+
if not self._loaded:
888+
_LOGGER.debug("Retrieving Info For Circle node %s", self._mac_in_str)
901889

902-
_LOGGER.debug(
903-
"Loading Circle node %s from cache failed",
904-
self._mac_in_str,
905-
)
906-
else:
907-
_LOGGER.debug("Loading Circle node %s", self._mac_in_str)
890+
# Check if node is online
891+
if not self._available and not await self.is_online():
892+
_LOGGER.debug(
893+
"Failed to load Circle node %s because it is not online",
894+
self._mac_in_str,
895+
)
896+
return False
908897

909-
# Check if node is online
910-
if not self._available and not await self.is_online():
911-
_LOGGER.debug(
912-
"Failed to load Circle node %s because it is not online",
913-
self._mac_in_str,
914-
)
915-
return False
898+
# Get node info
899+
if (
900+
self.skip_update(self._node_info, 30)
901+
and await self.node_info_update() is None
902+
):
903+
_LOGGER.debug(
904+
"Failed to load Circle node %s because it is not responding to information request",
905+
self._mac_in_str,
906+
)
907+
return False
916908

917-
# Get node info
918-
if (
919-
self.skip_update(self._node_info, 30)
920-
and await self.node_info_update() is None
921-
):
922-
_LOGGER.debug(
923-
"Failed to load Circle node %s because it is not responding to information request",
924-
self._mac_in_str,
925-
)
926-
return False
909+
self._loaded = True
927910

928-
self._loaded = True
929911
self._setup_protocol(
930912
CIRCLE_FIRMWARE_SUPPORT,
931913
(
@@ -937,10 +919,8 @@ async def load(self) -> bool:
937919
NodeFeature.POWER,
938920
),
939921
)
940-
if not await self.initialize():
941-
return False
942-
943922
await self._loaded_callback(NodeEvent.LOADED, self.mac)
923+
await self.initialize()
944924
return True
945925

946926
async def _load_from_cache(self) -> bool:

plugwise_usb/nodes/node.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ def node_info(self) -> NodeInfo:
228228
"""Node information."""
229229
return self._node_info
230230

231+
@property
232+
def initialized(self) -> bool:
233+
"""Node is Initialized."""
234+
return self._initialized
235+
231236
@property
232237
def mac(self) -> str:
233238
"""Zigbee mac address of node."""
@@ -583,6 +588,8 @@ async def is_online(self) -> bool:
583588
if await self.ping_update() is None:
584589
_LOGGER.debug("No response to ping for %s", self.mac)
585590
return False
591+
if not self._initialized:
592+
await self.initialize()
586593
return True
587594

588595
async def ping_update(

0 commit comments

Comments
 (0)