Skip to content

Commit 2efa143

Browse files
committed
Change load() to return None in all cases
1 parent 076ff5b commit 2efa143

File tree

7 files changed

+65
-73
lines changed

7 files changed

+65
-73
lines changed

plugwise_usb/network/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,9 @@ async def _load_node(self, mac: str) -> bool:
481481
return False
482482
if self._nodes[mac].is_loaded:
483483
return True
484-
if await self._nodes[mac].load():
485-
await self._notify_node_event_subscribers(NodeEvent.LOADED, mac)
486-
return True
487-
return False
484+
await self._nodes[mac].load()
485+
await self._notify_node_event_subscribers(NodeEvent.LOADED, mac)
486+
return True
488487

489488
async def _load_stragglers(self) -> None:
490489
"""Retry failed load operation."""

plugwise_usb/nodes/circle.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from datetime import UTC, datetime
99
from functools import wraps
1010
import logging
11-
from typing import Any, TypeVar, cast
11+
from typing import Any, Final, TypeVar, cast
1212

1313
from ..api import (
1414
EnergyStatistics,
@@ -59,6 +59,19 @@
5959
CACHE_RELAY_INIT = "relay_init"
6060
CACHE_RELAY_LOCK = "relay_lock"
6161

62+
CIRCLE_FEATURES: Final = (
63+
NodeFeature.CIRCLE,
64+
NodeFeature.RELAY,
65+
NodeFeature.RELAY_INIT,
66+
NodeFeature.RELAY_LOCK,
67+
NodeFeature.ENERGY,
68+
NodeFeature.POWER,
69+
)
70+
71+
72+
# Default firmware if not known
73+
DEFAULT_FIRMWARE: Final = datetime(2008, 8, 26, 15, 46, tzinfo=UTC)
74+
6275
FuncT = TypeVar("FuncT", bound=Callable[..., Any])
6376
_LOGGER = logging.getLogger(__name__)
6477

@@ -884,10 +897,10 @@ async def clock_synchronize(self) -> bool:
884897
return True
885898
return False
886899

887-
async def load(self) -> bool:
900+
async def load(self) -> None:
888901
"""Load and activate Circle node features."""
889902
if self._loaded:
890-
return True
903+
return
891904

892905
if self._cache_enabled:
893906
_LOGGER.debug("Loading Circle node %s from cache", self._mac_in_str)
@@ -897,34 +910,18 @@ async def load(self) -> bool:
897910
_LOGGER.debug("Retrieving Info For Circle node %s", self._mac_in_str)
898911

899912
# Check if node is online
900-
if not self._available and not await self.is_online():
901-
_LOGGER.debug(
902-
"Failed to load Circle node %s because it is not online",
903-
self._mac_in_str,
904-
)
905-
return False
906-
907-
# Get node info
908-
if await self.node_info_update() is None:
913+
if (
914+
not self._available and not await self.is_online()
915+
) or await self.node_info_update() is None:
909916
_LOGGER.debug(
910-
"Failed to load Circle node %s because it is not responding to information request",
917+
"Failed to retrieve NodeInfo for %s, loading defaults",
911918
self._mac_in_str,
912919
)
913-
return False
920+
await self._load_defaults()
914921

915-
self._loaded = True
922+
self._loaded = True
916923

917-
self._setup_protocol(
918-
CIRCLE_FIRMWARE_SUPPORT,
919-
(
920-
NodeFeature.CIRCLE,
921-
NodeFeature.RELAY,
922-
NodeFeature.RELAY_INIT,
923-
NodeFeature.RELAY_LOCK,
924-
NodeFeature.ENERGY,
925-
NodeFeature.POWER,
926-
),
927-
)
924+
self._setup_protocol(CIRCLE_FIRMWARE_SUPPORT, CIRCLE_FEATURES)
928925
await self._loaded_callback(NodeEvent.LOADED, self.mac)
929926
await self.initialize()
930927
return True
@@ -974,6 +971,15 @@ async def _load_from_cache(self) -> bool:
974971

975972
return result
976973

974+
async def _load_defaults(self) -> None:
975+
"""Load default configuration settings."""
976+
if self._node_info.model is None:
977+
self._node_info.model = "Circle"
978+
if self._node_info.name is None:
979+
self._node_info.name = f"Circle {self._node_info.mac[-5:]}"
980+
if self._node_info.firmware is None:
981+
self._node_info.firmware = DEFAULT_FIRMWARE
982+
977983
@raise_not_loaded
978984
async def initialize(self) -> bool:
979985
"""Initialize node."""

plugwise_usb/nodes/scan.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,34 +102,30 @@ def __init__(
102102

103103
# region Load & Initialize
104104

105-
async def load(self) -> bool:
105+
async def load(self) -> None:
106106
"""Load and activate Scan node features."""
107107
if self._loaded:
108-
return True
108+
return
109109

110110
_LOGGER.debug("Loading Scan node %s", self._node_info.mac)
111111
await super().load()
112112

113113
self._setup_protocol(SCAN_FIRMWARE_SUPPORT, SCAN_FEATURES)
114-
if await self.initialize():
115-
await self._loaded_callback(NodeEvent.LOADED, self.mac)
116-
return True
117-
_LOGGER.warning("Load Scan node %s failed", self._node_info.mac)
118-
return False
114+
await self.initialize()
115+
await self._loaded_callback(NodeEvent.LOADED, self.mac)
119116

120117
@raise_not_loaded
121-
async def initialize(self) -> bool:
118+
async def initialize(self) -> None:
122119
"""Initialize Scan node."""
123120
if self._initialized:
124-
return True
121+
return
125122

126123
self._unsubscribe_switch_group = await self._message_subscribe(
127124
self._switch_group,
128125
self._mac_in_bytes,
129126
(NODE_SWITCH_GROUP_ID,),
130127
)
131128
await super().initialize()
132-
return True
133129

134130
async def unload(self) -> None:
135131
"""Unload node."""

plugwise_usb/nodes/sed.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ def __init__(
114114
self._maintenance_last_awake: datetime | None = None
115115
self._maintenance_interval_restored_from_cache = False
116116

117-
async def load(self) -> None:
117+
async def load(self) -> bool:
118118
"""Load and activate SED node features."""
119119
if self._loaded:
120-
return
120+
return True
121+
121122
_LOGGER.debug("Load SED node %s from cache", self._node_info.mac)
122123
if await self._load_from_cache():
123124
self._loaded = True
@@ -126,6 +127,7 @@ async def load(self) -> None:
126127
await self._load_defaults()
127128
self._loaded = True
128129
self._features += SED_FEATURES
130+
return self._loaded
129131

130132
async def unload(self) -> None:
131133
"""Deactivate and unload node features."""
@@ -146,18 +148,17 @@ async def unload(self) -> None:
146148
await super().unload()
147149

148150
@raise_not_loaded
149-
async def initialize(self) -> bool:
151+
async def initialize(self) -> None:
150152
"""Initialize SED node."""
151153
if self._initialized:
152-
return True
154+
return
153155

154156
self._awake_subscription = await self._message_subscribe(
155157
self._awake_response,
156158
self._mac_in_bytes,
157159
(NODE_AWAKE_RESPONSE_ID,),
158160
)
159161
await super().initialize()
160-
return True
161162

162163
async def _load_defaults(self) -> None:
163164
"""Load default configuration settings."""

plugwise_usb/nodes/sense.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,30 @@ def __init__(
5353

5454
self._sense_subscription: Callable[[], None] | None = None
5555

56-
async def load(self) -> bool:
56+
async def load(self) -> None:
5757
"""Load and activate Sense node features."""
5858
if self._loaded:
59-
return True
59+
return
6060

6161
_LOGGER.debug("Loading Sense node %s", self._node_info.mac)
6262
await super().load()
6363

6464
self._setup_protocol(SENSE_FIRMWARE_SUPPORT, SENSE_FEATURES)
65-
if await self.initialize():
66-
await self._loaded_callback(NodeEvent.LOADED, self.mac)
67-
return True
68-
69-
_LOGGER.warning("Load Sense node %s failed", self._node_info.mac)
70-
return False
65+
await self.initialize()
66+
await self._loaded_callback(NodeEvent.LOADED, self.mac)
7167

7268
@raise_not_loaded
73-
async def initialize(self) -> bool:
69+
async def initialize(self) -> None:
7470
"""Initialize Sense node."""
7571
if self._initialized:
76-
return True
72+
return
7773

7874
self._sense_subscription = await self._message_subscribe(
7975
self._sense_report,
8076
self._mac_in_bytes,
8177
(SENSE_REPORT_ID,),
8278
)
8379
await super().initialize()
84-
return True
8580

8681
async def unload(self) -> None:
8782
"""Unload node."""

plugwise_usb/nodes/switch.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,35 +46,30 @@ def __init__(
4646
self._switch_subscription: Callable[[], None] | None = None
4747
self._switch = SwitchGroup()
4848

49-
async def load(self) -> bool:
49+
async def load(self) -> None:
5050
"""Load and activate Switch node features."""
5151
if self._loaded:
52-
return True
52+
return
5353

5454
_LOGGER.debug("Loading Switch node %s", self._node_info.mac)
5555
await super().load()
5656

5757
self._setup_protocol(SWITCH_FIRMWARE_SUPPORT, SWITCH_FEATURES)
58-
if await self.initialize():
59-
await self._loaded_callback(NodeEvent.LOADED, self.mac)
60-
return True
61-
62-
_LOGGER.warning("Load Switch node %s failed", self._node_info.mac)
63-
return False
58+
await self.initialize()
59+
await self._loaded_callback(NodeEvent.LOADED, self.mac)
6460

6561
@raise_not_loaded
66-
async def initialize(self) -> bool:
62+
async def initialize(self) -> None:
6763
"""Initialize Switch node."""
6864
if self._initialized:
69-
return True
65+
return
7066

7167
self._switch_subscription = await self._message_subscribe(
7268
self._switch_response,
7369
self._mac_in_bytes,
7470
(NODE_SWITCH_GROUP_ID,),
7571
)
7672
await super().initialize()
77-
return True
7873

7974
async def unload(self) -> None:
8075
"""Unload node."""

tests/test_usb.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ async def test_stick_node_discovered_subscription(
575575
assert mac_awake_node == "5555555555555555"
576576
unsub_awake()
577577

578-
assert await stick.nodes["5555555555555555"].load()
578+
assert await stick.nodes["5555555555555555"].load() is None
579579
assert stick.nodes["5555555555555555"].node_info.firmware == dt(
580580
2011, 6, 27, 8, 55, 44, tzinfo=UTC
581581
)
@@ -1951,7 +1951,7 @@ async def load_callback(event: pw_api.NodeEvent, mac: str) -> None: # type: ign
19511951

19521952
assert test_sed.node_info.is_battery_powered
19531953
assert test_sed.is_battery_powered
1954-
assert await test_sed.load() is None
1954+
assert await test_sed.load()
19551955
assert sorted(test_sed.features) == sorted(
19561956
(
19571957
pw_api.NodeFeature.AVAILABLE,
@@ -2168,7 +2168,7 @@ async def load_callback(event: pw_api.NodeEvent, mac: str) -> None: # type: ign
21682168
relay_state=None,
21692169
)
21702170
await test_scan.update_node_details(node_info)
2171-
assert await test_scan.load()
2171+
assert await test_scan.load() is None
21722172

21732173
# test motion reset timer
21742174
assert test_scan.reset_timer == 10
@@ -2277,7 +2277,7 @@ async def load_callback(event: pw_api.NodeEvent, mac: str) -> None: # type: ign
22772277
)
22782278
await test_scan.update_node_details(node_info)
22792279
test_scan.cache_enabled = True
2280-
assert await test_scan.load()
2280+
await test_scan.load()
22812281
assert sorted(test_scan.features) == sorted(
22822282
(
22832283
pw_api.NodeFeature.AVAILABLE,
@@ -2356,7 +2356,7 @@ async def load_callback(event: pw_api.NodeEvent, mac: str) -> None: # type: ign
23562356
relay_state=None,
23572357
)
23582358
await test_switch.update_node_details(node_info)
2359-
assert await test_switch.load()
2359+
await test_switch.load()
23602360

23612361
# Switch specific defaults
23622362
assert test_switch.switch is False
@@ -2379,7 +2379,7 @@ async def load_callback(event: pw_api.NodeEvent, mac: str) -> None: # type: ign
23792379
await test_switch.update_node_details(node_info)
23802380
test_switch.cache_enabled = True
23812381
assert test_switch.cache_enabled is True
2382-
assert await test_switch.load()
2382+
await test_switch.load()
23832383
assert sorted(test_switch.features) == sorted(
23842384
(
23852385
pw_api.NodeFeature.AVAILABLE,

0 commit comments

Comments
 (0)