10000 Use monotonic time for query timing · python-kasa/python-kasa@1d29251 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d29251

Browse files
committed
Use monotonic time for query timing
1 parent a2b7daa commit 1d29251

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

kasa/httpclient.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async def post(
7272
# Once we know a device needs a wait between sequential queries always wait
7373
# first rather than keep erroring then waiting.
7474
if self._wait_between_requests:
75-
now = time.time()
75+
now = time.monotonic()
7676
gap = now - self._last_request_time
7777
if gap < self._wait_between_requests:
7878
sleep = self._wait_between_requests - gap
@@ -123,7 +123,7 @@ async def post(
123123
ex,
124124
)
125125
self._wait_between_requests = self.WAIT_BETWEEN_REQUESTS_ON_OSERROR
126-
self._last_request_time = time.time()
126+
self._last_request_time = time.monotonic()
127127
raise _ConnectionError(
128128
f"Device connection error: {self._config.host}: {ex}", ex
129129
) from ex
@@ -140,7 +140,7 @@ async def post(
140140

141141
# For performance only request system time if waiting is enabled
142142
if self._wait_between_requests:
143-
self._last_request_time = time.time()
143+
self._last_request_time = time.monotonic()
144144

145145
return resp.status, response_data
146146

kasa/klaptransport.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,9 @@ async def perform_handshake(self) -> Any:
300300
# There is a 24 hour timeout on the session cookie
301301
# but the clock on the device is not always accurate
302302
# so we set the expiry to 24 hours from now minus a buffer
303-
self._session_expire_at = time.time() + timeout - SESSION_EXPIRE_BUFFER_SECONDS
303+
self._session_expire_at = (
304+
time.monotonic() + timeout - SESSION_EXPIRE_BUFFER_SECONDS
305+
)
304306
self._encryption_session = await self.perform_handshake2(
305307
local_seed, remote_seed, auth_hash
306308
)
@@ -312,7 +314,7 @@ def _handshake_session_expired(self):
312314
"""Return true if session has expired."""
313315
return (
314316
self._session_expire_at is None
315-
or self._session_expire_at - time.time() <= 0
317+
or self._session_expire_at - time.monotonic() <= 0
316318
)
317319

318320
async def send(self, request: str):

kasa/smart/smartdevice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ async def update(self, update_children: bool = False):
159159
raise AuthenticationError("Tapo plug requires authentication.")
160160

161161
first_update = self._last_update_time is None
162-
now = time.time()
162+
now = time.monotonic()
163163
self._last_update_time = now
164164

165165
if first_update:

kasa/tests/test_smartdevice.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ async def test_update_module_update_delays(
224224

225225
new_dev = SmartDevice("127.0.0.1", protocol=dev.protocol)
226226
await new_dev.update()
227-
first_update_time = time.time()
227+
first_update_time = time.monotonic()
228228
assert new_dev._last_update_time == first_update_time
229229
for module in new_dev.modules.values():
230230
if module.query():
@@ -236,7 +236,7 @@ async def test_update_module_update_delays(
236236
seconds += tick
237237
freezer.tick(tick)
238238

239-
now = time.time()
239+
now = time.monotonic()
240240
await new_dev.update()
241241
for module in new_dev.modules.values():
242242
mod_delay = module.MINIMUM_UPDATE_INTERVAL_SECS

0 commit comments

Comments
 (0)
0