Skip to content

Commit b9880a3

Browse files
committed
Move generic initialize code to base class
1 parent 0d0a9cb commit b9880a3

7 files changed

Lines changed: 17 additions & 47 deletions

File tree

plugwise_usb/nodes/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,10 @@ async def _load_from_cache(self) -> bool:
410410

411411
async def initialize(self) -> bool:
412412
"""Initialize node."""
413-
raise NotImplementedError()
413+
if self._initialized:
414+
return True
415+
self._initialized = True
416+
return True
414417

415418
async def _available_update_state(self, available: bool) -> None:
416419
"""Update the node availability state."""

plugwise_usb/nodes/circle.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,14 @@ async def initialize(self) -> bool:
786786
if self._initialized:
787787
_LOGGER.debug("Already initialized node %s", self._mac_in_str)
788788
return True
789-
self._initialized = True
790789

790+
if isinstance(self, PlugwiseCircle) and not await self.clock_synchronize():
791+
_LOGGER.debug(
792+
"Failed to initialized node %s, failed clock sync",
793+
self._mac_in_str
794+
)
795+
self._initialized = False
796+
return False
791797
if not self._calibration and not await self.calibration_update():
792798
_LOGGER.debug(
793799
"Failed to initialized node %s, no calibration",
@@ -800,13 +806,6 @@ async def initialize(self) -> bool:
800806
"Failed to retrieve node info for %s",
801807
self._mac_in_str
802808
)
803-
if not await self.clock_synchronize():
804-
_LOGGER.debug(
805-
"Failed to initialized node %s, failed clock sync",
806-
self._mac_in_str
807-
)
808-
self._initialized = False
809-
return False
810809
if (
811810
NodeFeature.RELAY_INIT in self._features and
812811
self._relay_init_state is None
@@ -820,7 +819,7 @@ async def initialize(self) -> bool:
820819
)
821820
self._initialized = False
822821
return False
823-
return True
822+
return await super().initialize()
824823

825824
async def node_info_update(
826825
self, node_info: NodeInfoResponse | None = None

plugwise_usb/nodes/circle_plus.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,31 +90,10 @@ async def initialize(self) -> bool:
9090
"""Initialize node."""
9191
if self._initialized:
9292
return True
93-
self._initialized = True
94-
if not self._available:
95-
self._initialized = False
96-
return False
97-
if not self._calibration and not await self.calibration_update():
98-
self._initialized = False
99-
return False
10093
if not await self.realtime_clock_synchronize():
10194
self._initialized = False
10295
return False
103-
if (
104-
NodeFeature.RELAY_INIT in self._features and
105-
self._relay_init_state is None
106-
):
107-
if (state := await self._relay_init_get()) is not None:
108-
self._relay_init_state = state
109-
else:
110-
_LOGGER.debug(
111-
"Failed to initialized node %s, relay init",
112-
self.mac
113-
)
114-
return False
115-
self._initialized = True
116-
await self._loaded_callback(NodeEvent.LOADED, self.mac)
117-
return True
96+
return await super().initialize()
11897

11998
async def realtime_clock_synchronize(self) -> bool:
12099
"""Synchronize realtime clock."""

plugwise_usb/nodes/scan.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,12 @@ async def initialize(self) -> bool:
6666
"""Initialize Scan node."""
6767
if self._initialized:
6868
return True
69-
self._initialized = True
70-
if not await super().initialize():
71-
self._initialized = False
72-
return False
7369
self._scan_subscription = self._message_subscribe(
7470
self._switch_group,
7571
self._mac_in_bytes,
7672
(NODE_SWITCH_GROUP_ID,),
7773
)
78-
self._initialized = True
79-
return True
74+
return await super().initialize()
8075

8176
async def unload(self) -> None:
8277
"""Unload node."""

plugwise_usb/nodes/sed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async def initialize(self) -> bool:
9696
self._mac_in_bytes,
9797
NODE_AWAKE_RESPONSE_ID,
9898
)
99-
return True
99+
return await super().initialize()
100100

101101
@property
102102
def maintenance_interval(self) -> int | None:

plugwise_usb/nodes/sense.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,12 @@ async def initialize(self) -> bool:
6363
"""Initialize Sense node."""
6464
if self._initialized:
6565
return True
66-
if not await super().initialize():
67-
return False
6866
self._sense_subscription = self._message_subscribe(
6967
self._sense_report,
7068
self._mac_in_bytes,
7169
SENSE_REPORT_ID,
7270
)
73-
self._initialized = True
74-
return True
71+
return await super().initialize()
7572

7673
async def unload(self) -> None:
7774
"""Unload node."""

plugwise_usb/nodes/switch.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,13 @@ async def initialize(self) -> bool:
4747
"""Initialize Switch node."""
4848
if self._initialized:
4949
return True
50-
if not await super().initialize():
51-
return False
5250
self._switch_subscription = self._message_subscribe(
5351
b"0056",
5452
self._switch_group,
5553
self._mac_in_bytes,
5654
NODE_SWITCH_GROUP_ID,
5755
)
58-
self._initialized = True
59-
return True
56+
return await super().initialize()
6057

6158
async def unload(self) -> None:
6259
"""Unload node."""

0 commit comments

Comments
 (0)