Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COSM integration, separation of library and script #8

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
*.py[cod]

# C extensions
*.so

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg
lib
lib64

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox
nosetests.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Confirm files
cosm.cfg

73 changes: 66 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,87 @@
pynest -- a python interface for the Nest Thermostat
==================================
by Scott M Baker, [email protected], http://www.smbaker.com/

API:
----

nest.py define Nest class which could be used to communicate with thermostat.

Comand-line tool:
--------------
Usage:
'nest.py help' will tell you what to do and how to do it
'nesttool.py help' will tell you what to do and how to do it

Example:
'nest.py --user [email protected] --password swordfish temp 73'
'nesttool.py --user [email protected] --password swordfish temp 73'
set the temperature to 73 degrees

'nest.py --user [email protected] --password swordfish fan auto'
'nesttool.py --user [email protected] --password swordfish fan auto'
set the fan to automatic

Installation:
'python ./setup.py install' will install nest.py to the right place,

COSM submission:
---------------

'nest_cosm.py' script could be used to submit thermostat data to COSM: http://cosm.com/

Usage:

./nest_cosm.py [-f <cfg file>] [-c] [-d]a

-c -- log to console instead of log file
-d -- dry-run mode. No data submitted.
-f <cfg file> -- config file name. Default is 'cosm.cfg'
-l <log file> -- config file name. Default is 'cosm.log'

Configuration file example:

{
"key":"your key"
"feed":123,
"nest_user":"[email protected]",
"nest_password":"secret",
"units":"C",
"fields": {
"current_temperature":{"datastream":1},
"current_humidity":{"datastream":2},
"fan_mode":{"datastream":3,
"mapping":{
"off":-1,
"on":1,
"auto":0
}},
"hvac_ac_state": {"datastream":4,"mapping":{
"False":0,
"True":1
}},
"hvac_heater_state":{"datastream":5,"mapping":{
"False":0,
"True":1
}},
"battery_level":{"datastream":100}
}
}

Sample feed: https://cosm.com/feeds/131118



Installation:
----------
'python ./setup.py install' will install nesttool.py and nest_cosm.py to the right place,
usually your /usr/bin directory.

Licensing:
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:
Acknowledgements:
----------------
Chris Burris's Siri Nest Proxy was very helpful to learn the nest's
authentication and some bits of the protocol.

33 changes: 33 additions & 0 deletions cosm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Simple API for COSM.com
"""
import urllib2

def submit_datapoints(feed,datastream,key,csv):
"""
Submit CSV-formatted list of datapoints to specified datastream
"""
if len(csv)==0:
return
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request("http://api.cosm.com/v2/feeds/%s/datastreams/%s/datapoints.csv" % (feed,datastream), csv)
request.add_header('Host','api.cosm.com')
request.add_header('Content-type','text/csv')
request.add_header('X-ApiKey', key)
opener.open(request)


def update_feed(feed,key,csv):
"""
Submit CSV-formatted data to update the feed.
@see https://cosm.com/docs/v2/feed/update.html
"""
if len(csv)==0:
return
opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request("http://api.cosm.com/v2/feeds/%s?_method=put" % (feed), csv)
request.add_header('Host','api.cosm.com')
request.add_header('Content-type','text/csv')
request.add_header('X-ApiKey', key)
opener.open(request)

97 changes: 0 additions & 97 deletions nest.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
# 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
Expand All @@ -20,7 +17,6 @@
import urllib
import urllib2
import sys
from optparse import OptionParser

try:
import json
Expand Down Expand Up @@ -141,97 +137,4 @@ def set_fan(self, state):

print res

def create_parser():
parser = OptionParser(usage="nest [options] command [command_options] [command_args]",
description="Commands: fan temp",
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("-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, 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
print "examples:"
print " nest.py --user [email protected] --password swordfish temp 73"
print " nest.py --user [email protected] --password swordfish fan auto"

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)
n.login()
n.get_status()

cmd = args[0]

if (cmd == "temp"):
if len(args)<2:
print "please specify a temperature"
sys.exit(-1)
n.set_temperature(int(args[1]))
elif (cmd == "fan"):
if len(args)<2:
print "please specify a fan state of 'on' or 'auto'"
sys.exit(-1)
n.set_fan(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"]
else:
print "misunderstood command:", cmd
print "do 'nest.py help' for help"

if __name__=="__main__":
main()





Loading