|
36 | 36 | import json
|
37 | 37 | import os
|
38 | 38 | import sys
|
39 |
| -from datetime import timedelta, datetime |
| 39 | +from datetime import timedelta, datetime, timezone |
40 | 40 | from pathlib import Path
|
41 | 41 | from urllib.parse import urlencode
|
42 | 42 |
|
@@ -69,7 +69,7 @@ class Tado:
|
69 | 69 | device_verification_url = None
|
70 | 70 | device_verification_url_expires_at = None
|
71 | 71 | id = None
|
72 |
| - refresh_at = datetime.now() + timedelta(minutes=10) |
| 72 | + refresh_at = datetime.now(timezone.utc) + timedelta(minutes=10) |
73 | 73 | refresh_token = None
|
74 | 74 | timeout = 15
|
75 | 75 | user_code = None
|
@@ -98,7 +98,7 @@ def set_oauth_token(self, response) -> str:
|
98 | 98 | self.refresh_token = refresh_token
|
99 | 99 | # We subtract 30 seconds from the correct refresh time.
|
100 | 100 | # Then we have a 30 seconds timespan to get a new refresh_token
|
101 |
| - self.refresh_at = datetime.now() + timedelta(seconds=expires_in) - timedelta(seconds=30) |
| 101 | + self.refresh_at = datetime.now(timezone.utc) + timedelta(seconds=expires_in) - timedelta(seconds=30) |
102 | 102 |
|
103 | 103 | self.access_headers = {
|
104 | 104 | 'Authorization': f'Bearer {access_token}',
|
@@ -126,7 +126,7 @@ def load_token(self) -> bool:
|
126 | 126 |
|
127 | 127 |
|
128 | 128 | def refresh_auth(self, refresh_token: str = None, force_refresh = False) -> bool:
|
129 |
| - if self.refresh_at >= datetime.now() and not force_refresh: |
| 129 | + if self.refresh_at >= datetime.now(timezone.utc) and not force_refresh: |
130 | 130 | return True
|
131 | 131 |
|
132 | 132 | url='https://login.tado.com/oauth2/token'
|
@@ -182,17 +182,19 @@ def login_device_flow(self) -> DeviceActivationStatus:
|
182 | 182 | print("Please visit the following URL in your Web browser to log in to your Tado account:", visit_url)
|
183 | 183 |
|
184 | 184 | expires_in_seconds = response["expires_in"]
|
185 |
| - self.device_verification_url_expires_at = datetime.now() + timedelta(seconds=expires_in_seconds) |
| 185 | + self.device_verification_url_expires_at = datetime.now(timezone.utc) + timedelta(seconds=expires_in_seconds) |
| 186 | + # print the expiry time in the user's local timezone. |
| 187 | + device_verification_url_expires_at_local_tz = datetime.now() + timedelta(seconds=expires_in_seconds) |
186 | 188 |
|
187 | 189 | print(
|
188 | 190 | "Waiting for you to complete logging in. You have until",
|
189 |
| - self.device_verification_url_expires_at.strftime("%Y-%m-%d %H:%M:%S"), |
| 191 | + device_verification_url_expires_at_local_tz.strftime("%Y-%m-%d %H:%M:%S"), |
190 | 192 | )
|
191 | 193 |
|
192 | 194 | return DeviceActivationStatus.PENDING
|
193 | 195 |
|
194 | 196 | def check_device_activation(self) -> bool:
|
195 |
| - if self.device_verification_url_expires_at is not None and datetime.timestamp(datetime.now()) > datetime.timestamp(self.device_verification_url_expires_at): |
| 197 | + if self.device_verification_url_expires_at is not None and datetime.timestamp(datetime.now(timezone.utc)) > datetime.timestamp(self.device_verification_url_expires_at): |
196 | 198 | raise Exception("User took too long to enter key")
|
197 | 199 |
|
198 | 200 | # Await the desired interval, before polling the API again
|
|
0 commit comments