Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use monotonic time for query timing #1070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions kasa/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def post(
# Once we know a device needs a wait between sequential queries always wait
# first rather than keep erroring then waiting.
if self._wait_between_requests:
now = time.time()
now = time.monotonic()
gap = now - self._last_request_time
if gap < self._wait_between_requests:
sleep = self._wait_between_requests - gap
Expand Down Expand Up @@ -123,7 +123,7 @@ async def post(
ex,
)
self._wait_between_requests = self.WAIT_BETWEEN_REQUESTS_ON_OSERROR
self._last_request_time = time.time()
self._last_request_time = time.monotonic()
raise _ConnectionError(
f"Device connection error: {self._config.host}: {ex}", ex
) from ex
Expand All @@ -140,7 +140,7 @@ async def post(

# For performance only request system time if waiting is enabled
if self._wait_between_requests:
self._last_request_time = time.time()
self._last_request_time = time.monotonic()

return resp.status, response_data

Expand Down
6 changes: 4 additions & 2 deletions kasa/klaptransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ async def perform_handshake(self) -> Any:
# There is a 24 hour timeout on the session cookie
# but the clock on the device is not always accurate
# so we set the expiry to 24 hours from now minus a buffer
self._session_expire_at = time.time() + timeout - SESSION_EXPIRE_BUFFER_SECONDS
self._session_expire_at = (
time.monotonic() + timeout - SESSION_EXPIRE_BUFFER_SECONDS
)
self._encryption_session = await self.perform_handshake2(
local_seed, remote_seed, auth_hash
)
Expand All @@ -312,7 +314,7 @@ def _handshake_session_expired(self):
"""Return true if session has expired."""
return (
self._session_expire_at is None
or self._session_expire_at - time.time() <= 0
or self._session_expire_at - time.monotonic() <= 0
)

async def send(self, request: str):
Expand Down
2 changes: 1 addition & 1 deletion kasa/smart/smartdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async def update(self, update_children: bool = False):
raise AuthenticationError("Tapo plug requires authentication.")

first_update = self._last_update_time is None
now = time.time()
now = time.monotonic()
self._last_update_time = now

if first_update:
Expand Down
2 changes: 1 addition & 1 deletion kasa/smartprotocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ def generate_id(self):
)

def _current_millis(self):
return round(time.time() * 1000)
return round(time.monotonic() * 1000)

def _wait_next_millis(self, last_timestamp):
timestamp = self._current_millis()
Expand Down
4 changes: 2 additions & 2 deletions kasa/tests/test_smartdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ async def test_update_module_update_delays(

new_dev = SmartDevice("127.0.0.1", protocol=dev.protocol)
await new_dev.update()
first_update_time = time.time()
first_update_time = time.monotonic()
assert new_dev._last_update_time == first_update_time
for module in new_dev.modules.values():
if module.query():
Expand All @@ -236,7 +236,7 @@ async def test_update_module_update_delays(
seconds += tick
freezer.tick(tick)

now = time.time()
now = time.monotonic()
await new_dev.update()
for module in new_dev.modules.values():
mod_delay = module.MINIMUM_UPDATE_INTERVAL_SECS
Expand Down
Loading