|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | | -from asyncio import Task, create_task, gather |
| 5 | +from asyncio import Task, create_task, gather, sleep |
6 | 6 | from collections.abc import Awaitable, Callable |
7 | 7 | from dataclasses import replace |
8 | 8 | from datetime import UTC, datetime, timedelta |
@@ -141,6 +141,8 @@ def __init__( |
141 | 141 | """Initialize base class for Sleeping End Device.""" |
142 | 142 | super().__init__(mac, node_type, controller, loaded_callback) |
143 | 143 |
|
| 144 | + # Clock |
| 145 | + self._clock_synchronize_task: Task[None] | None = None |
144 | 146 | # Relay |
145 | 147 | self._relay_lock: RelayLock = RelayLock() |
146 | 148 | self._relay_state: RelayState = RelayState() |
@@ -852,6 +854,12 @@ async def _relay_update_lock( |
852 | 854 | ) |
853 | 855 | await self.save_cache() |
854 | 856 |
|
| 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 | + |
855 | 863 | async def clock_synchronize(self) -> bool: |
856 | 864 | """Synchronize clock. Returns true if successful.""" |
857 | 865 | get_clock_request = CircleClockGetRequest(self._send, self._mac_in_bytes) |
@@ -992,6 +1000,10 @@ async def initialize(self) -> bool: |
992 | 1000 | ) |
993 | 1001 | self._initialized = False |
994 | 1002 | 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 | + ) |
995 | 1007 |
|
996 | 1008 | if not self._calibration and not await self.calibration_update(): |
997 | 1009 | _LOGGER.debug( |
@@ -1082,6 +1094,13 @@ async def unload(self) -> None: |
1082 | 1094 | if self._cache_enabled: |
1083 | 1095 | await self._energy_log_records_save_to_cache() |
1084 | 1096 |
|
| 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 | + |
1085 | 1104 | await super().unload() |
1086 | 1105 |
|
1087 | 1106 | @raise_not_loaded |
|
0 commit comments