~kirkland/uhome/trunk

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
#!/usr/bin/python
#
# nest-home, nest-away
#
# Copyright 2014 Dustin Kirkland <dustin.kirkland@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import requests
import time
import urllib
import urllib2
import sys
import json
import yaml

# login
with open(os.path.expanduser("~") + "/.uhome/nest.yaml", "r") as stream:
	credentials = yaml.load(stream)
#headers = {"user-agent": "Nest/1.1.0.10 CFNetwork/548.0.4", "X-nl-protocol-version": "1"}
headers = {"X-nl-protocol-version": "1"}
response = requests.post("https://home.nest.com/user/login", credentials)
response = json.loads(response.content)
transport_url = response["urls"]["transport_url"]
userid = response["userid"]
headers["Authorization"] = "Basic " + response["access_token"]
headers["X-nl-user-id"] = userid
# get structure id
request = urllib2.Request(transport_url + "/v3/mobile/user." + userid, headers=headers)
response = urllib2.urlopen(request).read()
response = json.loads(response)
structure_id = response["structure"].keys()[0]
# set away, if necessary
currently_away = response["structure"][structure_id]["away"]
if "away" in sys.argv[0] and not currently_away:
	data = '{"away_timestamp":' + str(time.time()) + ',"away":true,"away_setter":0}'
	new_away = "True"
elif not "away" in sys.argv[0] and currently_away:
	data = '{"away_timestamp":' + str(time.time()) + ',"away":false,"away_setter":0}'
	new_away = "False"
else:
	data = ""
	new_away = str(currently_away)
if data:
	structure_url = transport_url + "/v2/put/structure" "." + structure_id
	request = urllib2.Request(structure_url, data, headers)
	try:
		urllib2.urlopen(request).read()
	except urllib2.URLError:
		print "Put operation failed"
	print "Nest updated -->  away=" + new_away
else:
	print "No need to update Nest --> away=" + new_away