Skip to content

Commit 1446502

Browse files
committed
schedule clock synchronization every 60 seconds
1 parent 40185ed commit 1446502

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

plugwise_usb/nodes/circle.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from asyncio import Task, create_task, gather
5+
from asyncio import Task, create_task, gather, sleep
66
from collections.abc import Awaitable, Callable
77
from dataclasses import replace
88
from datetime import UTC, datetime, timedelta
@@ -141,6 +141,8 @@ def __init__(
141141
"""Initialize base class for Sleeping End Device."""
142142
super().__init__(mac, node_type, controller, loaded_callback)
143143

144+
# Clock
145+
self._clock_synchronize_task: Task[None] | None = None
144146
# Relay
145147
self._relay_lock: RelayLock = RelayLock()
146148
self._relay_state: RelayState = RelayState()
@@ -852,6 +854,12 @@ async def _relay_update_lock(
852854
)
853855
await self.save_cache()
854856

857+
async def _clock_synchronize_scheduler(self) -> bool:
858+
"""Synchronize clock scheduler."""
859+
while True:
860+
await sleep(60)
861+
await self.clock_synchronize()
862+
855863
async def clock_synchronize(self) -> bool:
856864
"""Synchronize clock. Returns true if successful."""
857865
get_clock_request = CircleClockGetRequest(self._send, self._mac_in_bytes)
@@ -992,6 +1000,10 @@ async def initialize(self) -> bool:
9921000
)
9931001
self._initialized = False
9941002
return False
1003+
if self._clock_synchronize_task is None or self._clock_synchronize_task.done():
1004+
self._clock_synchronize_task = create_task(
1005+
self._clock_synchronize_scheduler()
1006+
)
9951007

9961008
if not self._calibration and not await self.calibration_update():
9971009
_LOGGER.debug(
@@ -1082,6 +1094,13 @@ async def unload(self) -> None:
10821094
if self._cache_enabled:
10831095
await self._energy_log_records_save_to_cache()
10841096

1097+
if (
1098+
hasattr(self, "_clock_synchronize_task")
1099+
and self._clock_synchronize_task
1100+
and not self._clock_synchronize_task.done()
1101+
):
1102+
self._clock_synchronize_task.cancel()
1103+
10851104
await super().unload()
10861105

10871106
@raise_not_loaded

0 commit comments

Comments
 (0)