forked from smbaker/pynest
-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding setmode and show mode
- Loading branch information
Showing
2 changed files
with
160 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,142 @@ | |
|
||
import sys | ||
from optparse import OptionParser | ||
<<<<<<< HEAD | ||
from nest_thermostat import Nest | ||
======= | ||
|
||
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 simpejson." | ||
sys.exit(-1) | ||
|
||
class Nest: | ||
def __init__(self, username, password, serial=None, index=0, units="F"): | ||
self.username = username | ||
self.password = password | ||
self.serial = serial | ||
self.units = units | ||
self.index = index | ||
|
||
def loads(self, res): | ||
if hasattr(json, "loads"): | ||
res = json.loads(res) | ||
else: | ||
res = json.read(res) | ||
return res | ||
|
||
def login(self): | ||
data = urllib.urlencode({"username": self.username, "password": self.password}) | ||
|
||
req = urllib2.Request("https://home.nest.com/user/login", | ||
data, | ||
{"user-agent":"Nest/1.1.0.10 CFNetwork/548.0.4"}) | ||
|
||
res = urllib2.urlopen(req).read() | ||
|
||
res = self.loads(res) | ||
|
||
self.transport_url = res["urls"]["transport_url"] | ||
self.access_token = res["access_token"] | ||
self.userid = res["userid"] | ||
|
||
def get_status(self): | ||
req = urllib2.Request(self.transport_url + "/v2/mobile/user." + self.userid, | ||
headers={"user-agent":"Nest/1.1.0.10 CFNetwork/548.0.4", | ||
"Authorization":"Basic " + self.access_token, | ||
"X-nl-user-id": self.userid, | ||
"X-nl-protocol-version": "1"}) | ||
|
||
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] | ||
|
||
allvars = shared | ||
allvars.update(device) | ||
|
||
for k in sorted(allvars.keys()): | ||
print k + "."*(32-len(k)) + ":", allvars[k] | ||
|
||
def show_curtemp(self): | ||
temp = self.status["shared"][self.serial]["current_temperature"] | ||
temp = self.temp_out(temp) | ||
|
||
print "%0.1f" % temp | ||
|
||
def show_curmode(self): | ||
mode = self.status["shared"][self.serial]["target_temperature_type"] | ||
|
||
print mode | ||
|
||
def set_temperature(self, temp): | ||
temp = self.temp_in(temp) | ||
|
||
data = '{"target_change_pending":true,"target_temperature":' + '%0.1f' % temp + '}' | ||
req = urllib2.Request(self.transport_url + "/v2/put/shared." + self.serial, | ||
data, | ||
{"user-agent":"Nest/1.1.0.10 CFNetwork/548.0.4", | ||
"Authorization":"Basic " + self.access_token, | ||
"X-nl-protocol-version": "1"}) | ||
|
||
res = urllib2.urlopen(req).read() | ||
|
||
print res | ||
|
||
def set_fan(self, state): | ||
data = '{"fan_mode":"' + str(state) + '"}' | ||
req = urllib2.Request(self.transport_url + "/v2/put/device." + self.serial, | ||
data, | ||
{"user-agent":"Nest/1.1.0.10 CFNetwork/548.0.4", | ||
"Authorization":"Basic " + self.access_token, | ||
"X-nl-protocol-version": "1"}) | ||
|
||
res = urllib2.urlopen(req).read() | ||
|
||
print res | ||
>>>>>>> pull_10 | ||
|
||
def set_mode(self, state): | ||
data = '{"target_temperature_type":"' + str(state) + '"}' | ||
req = urllib2.Request(self.transport_url + "/v2/put/shared." + self.serial, data, {"user-agent":"Nest/1.1.0.10 CFNetwork/548.0.4", "Authorization":"Basic " + self.access_token, "X-nl-protocol-version": "1"}) | ||
|
||
res = urllib2.urlopen(req).read() | ||
|
||
print res | ||
|
||
def create_parser(): | ||
parser = OptionParser(usage="nest [options] command [command_options] [command_args]", | ||
|
@@ -41,12 +176,14 @@ def help(): | |
print " --index <number> ... optional, 0-based index of nest" | ||
print " (use --serial or --index, but not both)" | ||
print "commands: temp, fan, show, curtemp, curhumid" | ||
print " temp <temperature> ... set target temperature" | ||
print " fan [auto|on] ... set fan state" | ||
print " show ... show everything" | ||
print " curtemp ... print current temperature" | ||
print " curhumid ... print current humidity" | ||
print "commands:" | ||
print " temp <temperature> ... set target temperature" | ||
print " fan [auto|on] ... set fan state" | ||
print " mode [cool|heat|range|off] ... set fan state" | ||
print " show ... show everything" | ||
print " curtemp ... print current temperature" | ||
print " curhumid ... print current humidity" | ||
print " curmode ... print current mode" | ||
print "examples:" | ||
print " nest.py --user [email protected] --password swordfish temp 73" | ||
|
@@ -85,10 +222,17 @@ def main(): | |
print "please specify a fan state of 'on' or 'auto'" | ||
sys.exit(-1) | ||
n.set_fan(args[1]) | ||
elif (cmd == "mode"): | ||
if len(args)<2: | ||
print "valid modes are cool, heat, range, and off" | ||
sys.exit(-1) | ||
n.set_mode(args[1]) | ||
elif (cmd == "show"): | ||
n.show_status() | ||
elif (cmd == "curtemp"): | ||
n.show_curtemp() | ||
elif (cmd == "curmode"): | ||
n.show_curmode() | ||
elif (cmd == "curhumid"): | ||
print n.status["device"][n.serial]["current_humidity"] | ||
else: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters