Skip to content

Commit f196c99

Browse files
committed
address comments from @CoMPaTech
1 parent 1ffb28c commit f196c99

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ fixtures/*
1919
tmp
2020
.cache
2121
appdata_folder
22+
mock_folder_that_exists/*

plugwise_usb/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
NODE_TIME_OUT: Final = 15
4242

4343
# Retry delay discover nodes
44-
NODE_DISCOVER_INTERVAL = 60
44+
NODE_RETRY_DISCOVER_INTERVAL = 60
45+
NODE_RETRY_LOAD_INTERVAL = 60
4546

4647
MAX_RETRIES: Final = 3
4748
SUPPRESS_INITIALIZATION_WARNINGS: Final = 10 # Minutes to suppress (expected) communication warning messages after initialization

plugwise_usb/network/__init__.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212

1313
from ..api import NodeEvent, NodeType, PlugwiseNode, StickEvent
1414
from ..connection import StickController
15-
from ..constants import ENERGY_NODE_TYPES, UTF8
15+
from ..constants import (
16+
ENERGY_NODE_TYPES,
17+
NODE_RETRY_DISCOVER_INTERVAL,
18+
NODE_RETRY_LOAD_INTERVAL,
19+
UTF8,
20+
)
1621
from ..exceptions import CacheError, MessageError, NodeError, StickError, StickTimeout
1722
from ..helpers.util import validate_mac
1823
from ..messages.requests import CircleMeasureIntervalRequest, NodePingRequest
@@ -445,14 +450,11 @@ async def _discover_registered_nodes(self) -> None:
445450
self._registry_stragglers[address] = mac
446451
registered_counter += 1
447452
await sleep(0)
448-
if len(self._registry_stragglers) > 0:
449-
if (
450-
self._discover_stragglers_task is None
451-
or self._discover_stragglers_task.done()
452-
):
453-
self._discover_stragglers_task = create_task(
454-
self._discover_stragglers()
455-
)
453+
if len(self._registry_stragglers) > 0 and (
454+
self._discover_stragglers_task is None
455+
or self._discover_stragglers_task.done()
456+
):
457+
self._discover_stragglers_task = create_task(self._discover_stragglers())
456458
_LOGGER.debug(
457459
"Total %s online of %s registered node(s)",
458460
str(len(self._nodes)),
@@ -462,7 +464,7 @@ async def _discover_registered_nodes(self) -> None:
462464
async def _discover_stragglers(self) -> None:
463465
"""Repeat Discovery of Nodes with unknown NodeType."""
464466
while len(self._registry_stragglers) > 0:
465-
await sleep(60)
467+
await sleep(NODE_RETRY_DISCOVER_INTERVAL)
466468
stragglers: dict[int, str] = {}
467469
for address, mac in self._registry_stragglers.items():
468470
if not await self._discover_node(address, mac, None):
@@ -486,9 +488,9 @@ async def _load_node(self, mac: str) -> bool:
486488

487489
async def _load_stragglers(self) -> None:
488490
"""Retry failed load operation."""
489-
await sleep(60)
491+
await sleep(NODE_RETRY_LOAD_INTERVAL)
490492
while not self._load_discovered_nodes():
491-
await sleep(60)
493+
await sleep(NODE_RETRY_LOAD_INTERVAL)
492494

493495
async def _load_discovered_nodes(self) -> bool:
494496
"""Load all nodes currently discovered."""

plugwise_usb/network/registry.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,8 @@ async def update_network_registration(
163163
node_type = self._network_cache.get_nodetype(mac)
164164

165165
self._registry[address] = (mac, node_type)
166-
if node_type is not None:
167-
if self._network_cache is not None:
168-
await self._network_cache.update_nodetypes(mac, node_type)
166+
if node_type is not None and self._network_cache is not None:
167+
await self._network_cache.update_nodetypes(mac, node_type)
169168

170169
async def update_missing_registrations_full(self) -> None:
171170
"""Full retrieval of all unknown network registrations from network controller."""

plugwise_usb/nodes/node.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,9 @@ async def is_online(self) -> bool:
588588
if await self.ping_update() is None:
589589
_LOGGER.debug("No response to ping for %s", self.mac)
590590
return False
591-
if not self._initialized:
592-
if not await self.initialize():
593-
_LOGGER.debug("Node %s failed to initialize after ping", self.mac)
594-
return False
591+
if not self._initialized and self._loaded and not await self.initialize():
592+
_LOGGER.debug("Node %s failed to initialize after ping", self.mac)
593+
return False
595594
return True
596595

597596
async def ping_update(

tests/test_usb.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,15 @@ class MockOsPath:
207207

208208
async def exists(self, file_or_path: str) -> bool: # noqa: PLR0911
209209
"""Exists folder."""
210-
if file_or_path == "mock_folder_that_exists":
211-
return True
212-
if file_or_path == "mock_folder_that_exists/nodetype.cache":
213-
return True
214-
if file_or_path == "mock_folder_that_exists\\nodetype.cache":
215-
return True
216-
if file_or_path == "mock_folder_that_exists/0123456789ABCDEF.cache":
217-
return True
218-
if file_or_path == "mock_folder_that_exists\\0123456789ABCDEF.cache":
219-
return True
220-
if file_or_path == "mock_folder_that_exists\\file_that_exists.ext":
210+
test_exists = [
211+
"mock_folder_that_exists",
212+
"mock_folder_that_exists/nodetype.cache",
213+
"mock_folder_that_exists\\nodetype.cache",
214+
"mock_folder_that_exists/0123456789ABCDEF.cache",
215+
"mock_folder_that_exists\\0123456789ABCDEF.cache",
216+
"mock_folder_that_exists\\file_that_exists.ext",
217+
]
218+
if file_or_path in test_exists:
221219
return True
222220
return file_or_path == "mock_folder_that_exists/file_that_exists.ext"
223221

0 commit comments

Comments
 (0)