forked from smbaker/pynest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nest.py
executable file
·331 lines (267 loc) · 11.1 KB
/
nest.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#! /usr/bin/python
# nest.py -- a python interface to the Nest Thermostat
# by Scott M Baker, [email protected], http://www.smbaker.com/
#
# Usage:
# 'nest.py help' will tell you what to do and how to do it
#
# Licensing:
# This is distributed unider the Creative Commons 3.0 Non-commecrial,
# Attribution, Share-Alike license. You can use the code for noncommercial
# purposes. You may NOT sell it. If you do use it, then you must make an
# attribution to me (i.e. Include my name and thank me for the hours I spent
# on this)
#
# Acknowledgements:
# Chris Burris's Siri Nest Proxy was very helpful to learn the nest's
# authentication and some bits of the protocol.
import time
import urllib
import urllib2
import sys
import re
from optparse import OptionParser
try:
import json
except ImportError:
try:
import simplejson as json
except ImportError:
print "No json library available. I recommend installing either python-json"
print "or simplejson. Python 2.6+ contains json library already."
sys.exit(-1)
class Nest:
def __init__(self, username, password, serial=None, index=0, units="F", debug=False):
self.username = username
self.password = password
self.serial = serial
self.units = units
self.index = index
self.debug = debug
self.headers={"user-agent":"Nest/1.1.0.10 CFNetwork/548.0.4",
"X-nl-protocol-version": "1"}
def loads(self, res):
if hasattr(json, "loads"):
res = json.loads(res)
else:
res = json.read(res)
return res
# context ['shared','structure','device']
def handle_put(self, context, data):
assert context is not None, "Context must be set to ['shared','structure','device']"
assert data is not None, "Data is None"
new_url = self.transport_url + "/v2/put/" + context + "."
if (context == "shared" or context == "device"):
new_url += self.serial
elif (context == "structure"):
new_url += self.structure_id
else:
raise ValueError, context+ " is unsupported"
req = urllib2.Request(new_url, data, self.headers)
try:
urllib2.urlopen(req).read()
except urllib2.URLError:
print "Put operation failed"
if (self.debug):
print new_url
print data
def shared_put(self, data):
self.handle_put("shared", data)
def device_put(self, data):
self.handle_put("device", data)
def structure_put(self, data):
self.handle_put("structure", data)
def login(self):
data = urllib.urlencode({"username": self.username, "password": self.password})
req = urllib2.Request("https://home.nest.com/user/login",
data, self.headers)
res = urllib2.urlopen(req).read()
res = self.loads(res)
self.transport_url = res["urls"]["transport_url"]
self.userid = res["userid"]
self.headers["Authorization"] = "Basic " + res["access_token"]
self.headers["X-nl-user-id"]= self.userid
def get_status(self):
req = urllib2.Request(self.transport_url + "/v2/mobile/user." + self.userid,
headers=self.headers)
res = urllib2.urlopen(req).read()
res = self.loads(res)
self.structure_id = res["structure"].keys()[0]
if (self.serial is None):
self.device_id = res["structure"][self.structure_id]["devices"][self.index]
self.serial = self.device_id.split(".")[1]
self.status = res
#print "res.keys", res.keys()
#print "res[structure][structure_id].keys", res["structure"][self.structure_id].keys()
#print "res[device].keys", res["device"].keys()
#print "res[device][serial].keys", res["device"][self.serial].keys()
#print "res[shared][serial].keys", res["shared"][self.serial].keys()
def temp_in(self, temp):
if (self.units == "F"):
return (temp - 32.0) / 1.8
else:
return temp
def temp_out(self, temp):
if (self.units == "F"):
return temp*1.8 + 32.0
else:
return temp
def show_status(self):
shared = self.status["shared"][self.serial]
device = self.status["device"][self.serial]
structure = self.status["structure"][self.structure_id]
# Delete the structure name so that we preserve the device name
del structure["name"]
allvars = shared
allvars.update(structure)
allvars.update(device)
for k, v in sorted(allvars.items()):
print k + "."*(32-len(k)) + ":", self.format_value(k, v)
def format_value(self, key, value):
if 'temp' in key and isinstance(value, float) and self.units == 'F':
return '%s (%s F)' % (value, self.temp_out(value))
elif 'timestamp' in key or key == 'creation_time':
if value > 0xffffffff:
value /= 1000
return time.ctime(value)
elif key == 'mac_address' and len(value) == 12:
return ':'.join(value[i:i+2] for i in xrange(0, 12, 2))
else:
return str(value)
def show_curtemp(self):
temp = self.status["shared"][self.serial]["current_temperature"]
temp = self.temp_out(temp)
print "%0.1f" % temp
def show_fan_mode(self):
fan_mode = self.status["device"][self.serial]["fan_mode"]
print fan_mode
def set_temperature(self, temp):
temp = self.temp_in(temp)
data = '{"target_change_pending":true,"target_temperature":' + '%0.1f' % temp + '}'
self.shared_put(data)
def set_fan(self, state):
data = '{"fan_mode":"' + str(state) + '"}'
self.device_put(data)
def set_mode(self, state):
data = '{"target_temperature_type":"' + str(state) + '"}'
self.shared_put(data)
def set_away(self, state):
time_since_epoch = time.time()
if (state == "away"):
data = '{"away_timestamp":' + str(time_since_epoch) + ',"away":true,"away_setter":0}'
else:
data = '{"away_timestamp":' + str(time_since_epoch) + ',"away":false,"away_setter":0}'
self.structure_put(data)
def set_auto_away(self, state):
if (state == "enable"):
data = '{"auto_away_enable":true}'
else:
data = '{"auto_away_enable":false}'
self.device_put(data)
def create_parser():
parser = OptionParser(usage="nest [options] command [command_options] [command_args]",
description="Commands: fan temp mode away auto-away",
version="unknown")
parser.add_option("-u", "--user", dest="user",
help="username for nest.com", metavar="USER", default=None)
parser.add_option("-p", "--password", dest="password",
help="password for nest.com", metavar="PASSWORD", default=None)
parser.add_option("-c", "--celsius", dest="celsius", action="store_true", default=False,
help="use celsius instead of farenheit")
parser.add_option("-s", "--serial", dest="serial", default=None,
help="optional, specify serial number of nest thermostat to talk to")
parser.add_option("-d", "--debug", dest="debug", action="store_true", default=False,
help="Print debug information")
parser.add_option("-i", "--index", dest="index", default=0, type="int",
help="optional, specify index number of nest to talk to")
return parser
def help():
print "syntax: nest [options] command [command_args]"
print "options:"
print " --user <username> ... username on nest.com"
print " --password <password> ... password on nest.com"
print " --celsius ... use celsius (the default is farenheit)"
print " --serial <number> ... optional, specify serial number of nest to use"
print " --index <number> ... optional, 0-based index of nest"
print " (use --serial or --index, but not both)"
print
print "commands: temp, fan, away, mode, show, curtemp, curhumid"
print " temp <temperature> ... set target temperature"
print " fan [auto|on] ... set fan state"
print " away [away|here] ... set away state"
print " auto-away [enable|disable]... enable or disable auto away"
print " mode [heat|cool|range] ... set thermostat mode"
print " show ... show everything"
print " curtemp ... print current temperature"
print " curhumid ... print current humidity"
print
print "examples:"
print " nest.py --user [email protected] --password swordfish temp 73"
print " nest.py --user [email protected] --password swordfish fan auto"
def validate_temp(temp):
try:
new_temp = float(temp)
except ValueError:
return -1
if new_temp < 50 or new_temp > 90:
return -1
return new_temp
def main():
parser = create_parser()
(opts, args) = parser.parse_args()
if (len(args)==0) or (args[0]=="help"):
help()
sys.exit(-1)
if (not opts.user) or (not opts.password):
print "how about specifying a --user and --password option next time?"
sys.exit(-1)
if opts.celsius:
units = "C"
else:
units = "F"
n = Nest(opts.user, opts.password, opts.serial, opts.index, units=units, debug=opts.debug)
n.login()
n.get_status()
cmd = args[0]
if (cmd == "temp"):
new_temp = -1
if len(args)>1:
new_temp = validate_temp(args[1])
if new_temp == -1:
print "please specify a temperature between 50 and 90"
sys.exit(-1)
n.set_temperature(new_temp)
elif (cmd == "fan"):
if len(args)==1:
n.show_fan_mode()
elif len(args)<2 or args[1] not in {"on", "auto"}:
print "please specify a fan state of 'on' or 'auto'"
sys.exit(-1)
else:
n.set_fan(args[1])
elif (cmd == "mode"):
if len(args)<2 or args[1] not in {"cool", "heat", "range"}:
print "please specify a thermostat mode of 'cool', 'heat' or 'range'"
sys.exit(-1)
n.set_mode(args[1])
elif (cmd == "show"):
n.show_status()
elif (cmd == "curtemp"):
n.show_curtemp()
elif (cmd == "curhumid"):
print n.status["device"][n.serial]["current_humidity"]
elif (cmd == "away"):
if len(args)<2 or args[1] not in {"away", "here"}:
print "please specify a state of 'away' or 'here'"
sys.exit(-1)
n.set_away(args[1])
elif (cmd == "auto-away"):
if len(args)<2 or args[1] not in {"enable", "disable"}:
print "please specify a state of 'enable' or 'disable'"
sys.exit(-1)
n.set_auto_away(args[1])
else:
print "misunderstood command:", cmd
print "do 'nest.py help' for help"
if __name__=="__main__":
main()