-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetatmo.py
executable file
·165 lines (141 loc) · 5.25 KB
/
netatmo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
import datetime
import json
import getpass
import os
import pprint
import sys
import time
import urllib
import urllib2
API_URL = 'https://api.netatmo.com/api'
OAUTH_TOKEN_URL = 'https://api.netatmo.com/oauth2/token'
OAUTH_CLIENT_ID = ''
OAUTH_CLIENT_SECRET = ''
HOME_ID = ''
CAMERA_IDS = []
DEVICES = {
'sarah': {
'ip': '192.168.1.10',
},
'john': {
'ip': '192.168.1.11',
'ip': '192.168.1.12'
}
}
TIME_BEFORE_ENABLE = 300
FILE_BASE = '%s/.netatmo' % os.path.expanduser('~')
FILE_TOKENS = '%s/tokens' % FILE_BASE
FILE_STATE = '%s/camera_state' % FILE_BASE
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
class util:
@staticmethod
def request(url, get={}, post={}):
if get:
url = '%s?%s' % (url, urllib.urlencode(get))
req = urllib2.Request(url, urllib.urlencode(post))
return urllib2.urlopen(req).read()
@staticmethod
def scan_ip(ips):
return [ip for ip in ips if os.system("ping -c 1 -W 30 " + ip) == 0]
class netatmo:
@staticmethod
def save_tokens(tokens):
tokens['expires_on'] = (datetime.datetime.now() + datetime.timedelta(seconds=tokens.get('expires_in'))).strftime(DATETIME_FORMAT)
with open(FILE_TOKENS, 'w') as fp:
fp.write(json.dumps(tokens))
@staticmethod
def authenticate():
username, password = raw_input("Netatmo username (login/email):"), getpass.getpass("Netatmo password:")
values = {
'grant_type': 'password',
'client_id': OAUTH_CLIENT_ID,
'client_secret': OAUTH_CLIENT_SECRET,
'username': username,
'password': password,
'scope': 'read_camera access_camera',
}
try:
tokens = json.loads(util.request(OAUTH_TOKEN_URL, post=values))
netatmo.save_tokens(tokens)
except urllib2.HTTPError:
sys.exit('Cannot authenticate to Netatmo services.')
return tokens
@staticmethod
def refresh_access_token(refresh_token):
values = {
'grant_type': 'refresh_token',
'refresh_token': refresh_token,
'client_id': OAUTH_CLIENT_ID,
'client_secret': OAUTH_CLIENT_SECRET,
}
try:
tokens = json.loads(util.request(OAUTH_TOKEN_URL, post=values))
netatmo.save_tokens(tokens)
except urllib2.HTTPError:
sys.exit('Cannot refresh access token.')
return tokens
@staticmethod
def get_access_token():
tokens = None
with open(FILE_TOKENS, 'r') as fp:
saved_tokens = fp.read()
if saved_tokens:
tokens = json.loads(saved_tokens)
if datetime.datetime.now() > datetime.datetime.strptime(tokens['expires_on'], DATETIME_FORMAT):
tokens = netatmo.refresh_access_token(tokens['refresh_token'])
if not tokens:
tokens = netatmo.authenticate()
return tokens['access_token']
@staticmethod
def get_home_data():
access_token = netatmo.get_access_token()
params = {'access_token': access_token}
if HOME_ID:
params['home_id'] = HOME_ID
return json.loads(util.request('%s/gethomedata' % API_URL, get=params))
@staticmethod
def cameras_change_status(status):
change = False
if os.path.exists(FILE_STATE):
with open(FILE_STATE, 'r') as fp:
try:
last_status, last_status_time = fp.read().split(',')
if last_status != status:
if last_status == 'on' or time.time() - float(last_status_time) > TIME_BEFORE_ENABLE:
change = True
except Exception:
change = True
else:
change = True
if change:
home_data = netatmo.get_home_data()
pprint.pprint(home_data)
homes = home_data.get('body', {}).get('homes', [])
for home in homes:
cameras = home.get('cameras')
for camera in cameras:
if camera['id'] in CAMERA_IDS and camera['status'] != status:
res = json.loads(util.request('%s/command/ping' % (camera['vpn_url'],), get={'status': status}))
util.request('%s/command/changestatus' % res['local_url'], get={'status': status})
if change or status == 'off':
with open(FILE_STATE, 'w') as fp:
fp.write('%s,%s' % (status, time.time()))
if __name__ == "__main__":
if not os.path.exists(FILE_BASE):
os.makedirs(FILE_BASE)
if not os.path.exists(FILE_TOKENS):
netatmo.authenticate()
scans = set(prop for device, properties in DEVICES.items() for prop in properties)
presence = {}
for scan in scans:
scan_method = getattr(util, 'scan_%s' % scan)
if scan_method:
ips = [properties[scan] for device, properties in DEVICES.items() if scan in properties]
presence[scan] = scan_method(ips)
for device, properties in DEVICES.items():
if all(value in presence.get(prop, []) for prop, value in properties.items()):
netatmo.cameras_change_status('off')
break
else:
netatmo.cameras_change_status('on')