|
1 | 1 | # Published Jan 2013 |
2 | 2 | # Revised Jan 2014 (to add new modules data) |
3 | | -# Author : Philippe Larduinat, philippelt@users.sourceforge.net |
4 | | -# Public domain source code |
| 3 | +# Revised 2016 (to add camera support) |
| 4 | +# Author : Philippe Larduinat, ph.larduinat@wanadoo.fr |
| 5 | +# Multiple contributors : see https://github.com/philippelt/netatmo-api-python |
| 6 | +# License : GPL V3 |
5 | 7 | """ |
6 | 8 | This API provides access to the Netatmo weather station or/and the Welcome camera |
7 | 9 | This package can be used with Python2 or Python3 applications and do not |
|
11 | 13 | coding=utf-8 |
12 | 14 | """ |
13 | 15 | from sys import version_info |
| 16 | +from os import getenv |
| 17 | +from os.path import expanduser, exists |
14 | 18 | import json, time |
15 | 19 | import imghdr |
16 | 20 | import warnings |
|
22 | 26 | from urllib import urlencode |
23 | 27 | import urllib2 |
24 | 28 |
|
25 | | -######################## USER SPECIFIC INFORMATION ###################### |
| 29 | +######################## AUTHENTICATION INFORMATION ###################### |
26 | 30 |
|
27 | 31 | # To be able to have a program accessing your netatmo data, you have to register your program as |
28 | 32 | # a Netatmo app in your Netatmo account. All you have to do is to give it a name (whatever) and you will be |
29 | 33 | # returned a client_id and secret that your app has to supply to access netatmo servers. |
30 | 34 |
|
31 | | -_CLIENT_ID = "" # Your client ID from Netatmo app registration at http://dev.netatmo.com/dev/listapps |
32 | | -_CLIENT_SECRET = "" # Your client app secret ' ' |
33 | | -_USERNAME = "" # Your netatmo account username |
34 | | -_PASSWORD = "" # Your netatmo account password |
| 35 | +# To ease Docker packaging of your application, you can setup your authentication parameters through env variables |
| 36 | + |
| 37 | +# Authentication use : |
| 38 | +# 1 - Values hard coded in the library |
| 39 | +# 2 - The .netatmo.credentials file in JSON format in your home directory |
| 40 | +# 3 - Values defined in environment variables : CLIENT_ID, CLIENT_SECRET, USERNAME, PASSWORD |
| 41 | + |
| 42 | +# Each level override values defined in the previous level. You could define CLIENT_ID and CLIENT_SECRET hard coded in the library |
| 43 | +# and username/password in .netatmo.credentials or environment variables |
| 44 | + |
| 45 | +cred = { # You can hard code authentication information in the following lines |
| 46 | + "CLIENT_ID" : "", # Your client ID from Netatmo app registration at http://dev.netatmo.com/dev/listapps |
| 47 | + "CLIENT_SECRET" : "", # Your client app secret ' ' |
| 48 | + "USERNAME" : "", # Your netatmo account username |
| 49 | + "PASSWORD" : "" # Your netatmo account password |
| 50 | + } |
| 51 | + |
| 52 | +# Other authentication setup management (optionals) |
| 53 | + |
| 54 | +CREDENTIALS = expanduser("~/.netatmo.credentials") |
| 55 | + |
| 56 | +def getParameter(key, default): |
| 57 | + return getenv(key, default[key]) |
| 58 | + |
| 59 | +# 2 : Override hard coded values with credentials file if any |
| 60 | +if exists(CREDENTIALS) : |
| 61 | + with open(CREDENTIALS, "r") as f: cred = json.loads(f.read()) |
| 62 | + |
| 63 | +# 3 : Override final value with content of env variables if defined |
| 64 | +_CLIENT_ID = getParameter("CLIENT_ID", cred) |
| 65 | +_CLIENT_SECRET = getParameter("CLIENT_SECRET", cred) |
| 66 | +_USERNAME = getParameter("USERNAME", cred) |
| 67 | +_PASSWORD = getParameter("PASSWORD", cred) |
35 | 68 |
|
36 | 69 | ######################################################################### |
37 | 70 |
|
38 | 71 |
|
39 | 72 | # Common definitions |
40 | 73 |
|
41 | | -_BASE_URL = "https://api.netatmo.com/" |
42 | | -_AUTH_REQ = _BASE_URL + "oauth2/token" |
43 | | -_GETMEASURE_REQ = _BASE_URL + "api/getmeasure" |
44 | | -_GETSTATIONDATA_REQ = _BASE_URL + "api/getstationsdata" |
45 | | -_GETHOMEDATA_REQ = _BASE_URL + "api/gethomedata" |
| 74 | +_BASE_URL = "https://api.netatmo.com/" |
| 75 | +_AUTH_REQ = _BASE_URL + "oauth2/token" |
| 76 | +_GETMEASURE_REQ = _BASE_URL + "api/getmeasure" |
| 77 | +_GETSTATIONDATA_REQ = _BASE_URL + "api/getstationsdata" |
| 78 | +_GETHOMEDATA_REQ = _BASE_URL + "api/gethomedata" |
46 | 79 | _GETCAMERAPICTURE_REQ = _BASE_URL + "api/getcamerapicture" |
47 | | -_GETEVENTSUNTIL_REQ = _BASE_URL + "api/geteventsuntil" |
| 80 | +_GETEVENTSUNTIL_REQ = _BASE_URL + "api/geteventsuntil" |
| 81 | + |
48 | 82 |
|
49 | 83 |
|
50 | 84 | class NoDevice( Exception ): |
@@ -72,6 +106,7 @@ def __init__(self, clientId=_CLIENT_ID, |
72 | 106 | username=_USERNAME, |
73 | 107 | password=_PASSWORD, |
74 | 108 | scope="read_station"): |
| 109 | + |
75 | 110 | postParams = { |
76 | 111 | "grant_type" : "password", |
77 | 112 | "client_id" : clientId, |
@@ -166,7 +201,7 @@ def moduleByName(self, module, station=None): |
166 | 201 | for m in self.modules: |
167 | 202 | mod = self.modules[m] |
168 | 203 | if mod['module_name'] == module : |
169 | | - if not s or mod['main_device'] == s['_id'] : return mod |
| 204 | + return mod |
170 | 205 | return None |
171 | 206 |
|
172 | 207 | def moduleById(self, mid, sid=None): |
@@ -548,6 +583,7 @@ def getStationMinMaxTH(station=None, module=None): |
548 | 583 | result.extend(devList.MinMaxTH(station, mname)) |
549 | 584 | return result |
550 | 585 |
|
| 586 | + |
551 | 587 | # auto-test when executed directly |
552 | 588 |
|
553 | 589 | if __name__ == "__main__": |
|
0 commit comments