Skip to content

Commit d8f3a1c

Browse files
committed
Rewrite and standardize clock synchronization methods
1 parent ad056c3 commit d8f3a1c

2 files changed

Lines changed: 28 additions & 24 deletions

File tree

plugwise_usb/nodes/circle.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -670,31 +670,31 @@ async def clock_synchronize(self) -> bool:
670670
clock_offset = (
671671
clock_response.timestamp.replace(microsecond=0) - _dt_of_circle
672672
)
673-
if (clock_offset.seconds > MAX_TIME_DRIFT) or (
674-
clock_offset.seconds < -(MAX_TIME_DRIFT)
673+
if (clock_offset.seconds < MAX_TIME_DRIFT) or (
674+
clock_offset.seconds > -(MAX_TIME_DRIFT)
675675
):
676-
_LOGGER.info(
677-
"Reset clock of node %s because time has drifted %s sec",
678-
self._mac_in_str,
679-
str(clock_offset.seconds),
676+
return True
677+
_LOGGER.info(
678+
"Reset clock of node %s because time has drifted %s sec",
679+
self._mac_in_str,
680+
str(clock_offset.seconds),
681+
)
682+
node_response: NodeResponse | None = await self._send(
683+
CircleClockSetRequest(
684+
self._mac_in_bytes,
685+
datetime.now(tz=UTC),
686+
self._node_protocols.max
680687
)
681-
node_response: NodeResponse | None = await self._send(
682-
CircleClockSetRequest(
683-
self._mac_in_bytes,
684-
datetime.now(tz=UTC),
685-
self._node_protocols.max
686-
)
688+
)
689+
if node_response is None:
690+
_LOGGER.warning(
691+
"Failed to (re)set the internal clock of %s",
692+
self.name,
687693
)
688-
if (
689-
node_response is None
690-
or node_response.ack_id != NodeResponseType.CLOCK_ACCEPTED
691-
):
692-
_LOGGER.warning(
693-
"Failed to (re)set the internal clock of node %s",
694-
self._mac_in_str,
695-
)
696-
return False
697-
return True
694+
return False
695+
if node_response.ack_id == NodeResponseType.CLOCK_ACCEPTED:
696+
return True
697+
return False
698698

699699
async def load(self) -> bool:
700700
"""Load and activate Circle node features."""

plugwise_usb/nodes/circle_plus.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ async def initialize(self) -> bool:
9595
return False
9696
return await super().initialize()
9797

98-
async def realtime_clock_synchronize(self) -> bool:
99-
"""Synchronize realtime clock."""
98+
async def clock_synchronize(self) -> bool:
99+
"""Synchronize realtime clock. Returns true if successful."""
100100
clock_response: CirclePlusRealTimeClockResponse | None = (
101101
await self._send(
102102
CirclePlusRealTimeClockGetRequest(self._mac_in_bytes)
@@ -138,6 +138,10 @@ async def realtime_clock_synchronize(self) -> bool:
138138
),
139139
)
140140
if node_response is None:
141+
_LOGGER.warning(
142+
"Failed to (re)set the internal realtime clock of %s",
143+
self.name,
144+
)
141145
return False
142146
if node_response.ack_id == NodeResponseType.CLOCK_ACCEPTED:
143147
return True

0 commit comments

Comments
 (0)