|
42 | 42 |
|
43 | 43 | # To ease Docker packaging of your application, you can setup your authentication parameters through env variables
|
44 | 44 |
|
45 |
| -# Authentication use : |
46 |
| -# 1 - Values hard coded in the library |
47 |
| -# 2 - The .netatmo.credentials file in JSON format in your home directory |
48 |
| -# 3 - Values defined in environment variables : CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN |
49 |
| - |
50 |
| -# Each level override values defined in the previous level. You could define CLIENT_ID and CLIENT_SECRET hard coded in the library |
51 |
| -# and REFRESH_TOKEN in .netatmo.credentials or environment variables |
52 |
| - |
53 |
| -# 1 : Embedded credentials |
54 |
| -cred = { # You can hard code authentication information in the following lines |
55 |
| - "CLIENT_ID" : "", # Your client ID from Netatmo app registration at http://dev.netatmo.com |
56 |
| - "CLIENT_SECRET" : "", # Your client app secret ' ' |
57 |
| - "REFRESH_TOKEN" : "" # Your scoped refresh token (generated with app credentials) |
58 |
| - } |
| 45 | +# Authentication: |
| 46 | +# 1 - The .netatmo.credentials file in JSON format in your home directory (now mandatory for regular use) |
| 47 | +# 2 - Values defined in environment variables : CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN |
59 | 48 |
|
60 |
| -# Other authentication setup management (optionals) |
| 49 | +# Note that the refresh token being short lived, using envvar will be restricted to speific testing use case |
61 | 50 |
|
| 51 | +# Note: this file will be rewritten by the library to record refresh_token change |
| 52 | +# If you run your application in container, remember to persist this file |
62 | 53 | CREDENTIALS = expanduser("~/.netatmo.credentials")
|
| 54 | +with open(CREDENTIALS, "r") as f: |
| 55 | + cred = {k.upper():v for k,v in json.loads(f.read()).items()} |
63 | 56 |
|
64 | 57 | def getParameter(key, default):
|
65 |
| - return getenv(key, default[key]) |
66 |
| - |
67 |
| -# 2 : Override hard coded values with credentials file if any |
68 |
| -if exists(CREDENTIALS) : |
69 |
| - with open(CREDENTIALS, "r") as f: |
70 |
| - cred.update({k.upper():v for k,v in json.loads(f.read()).items()}) |
| 58 | + return getenv(key, default.get(key, None)) |
71 | 59 |
|
72 |
| -# 3 : Override final value with content of env variables if defined |
| 60 | +# Override values with content of env variables if defined |
73 | 61 | _CLIENT_ID = getParameter("CLIENT_ID", cred)
|
74 | 62 | _CLIENT_SECRET = getParameter("CLIENT_SECRET", cred)
|
75 | 63 | _REFRESH_TOKEN = getParameter("REFRESH_TOKEN", cred)
|
@@ -261,9 +249,11 @@ def renew_token(self):
|
261 | 249 | }
|
262 | 250 | resp = postRequest("authentication", _AUTH_REQ, postParams)
|
263 | 251 | if self.refreshToken != resp['refresh_token']:
|
264 |
| - print("New refresh token:", resp['refresh_token']) |
| 252 | + self.refreshToken = resp['refresh_token'] |
| 253 | + cred["REFRESH_TOKEN"] = self.refreshToken |
| 254 | + with open(CREDENTIALS, "w") as f: |
| 255 | + f.write(json.dumps(cred, indent=True)) |
265 | 256 | self._accessToken = resp['access_token']
|
266 |
| - self.refreshToken = resp['refresh_token'] |
267 | 257 | self.expiration = int(resp['expire_in'] + time.time())
|
268 | 258 |
|
269 | 259 |
|
|
0 commit comments