smbaker / pynest

python API for talking to nest thermostat
165 stars 81 forks source link

Add Change Mode Functionality #4

Open youngrh opened 11 years ago

youngrh commented 11 years ago

I am trying to periodically run with cron a script that will change my Nest from heat to cool and back based on logic using current set point and current temp.

I can read the current temp and change the set point with this code but cannot get the mode change working with the hacked code that I am trying. I have added this section to the code I downloaded:

def set_mode(self, state):
    data = '{"target_change_pending":true,"target_temperature_type":"' + 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

I get this when I try to run it from the command line on my CentOS box that does have curl installed as a module and with yum:

./nest.py --user mylogin@wherever.com --password notmypassword setmode off Traceback (most recent call last): File "./nest.py", line 261, in ? main() File "./nest.py", line 245, in main n.set_mode(args[1]) File "./nest.py", line 157, in set_mode res = urllib2.urlopen(req).read() File "/usr/lib64/python2.4/urllib2.py", line 130, in urlopen return _opener.open(url, data) File "/usr/lib64/python2.4/urllib2.py", line 364, in open response = meth(req, response) File "/usr/lib64/python2.4/urllib2.py", line 471, in http_response response = self.parent.error( File "/usr/lib64/python2.4/urllib2.py", line 402, in error return self._call_chain(_args) File "/usr/lib64/python2.4/urllib2.py", line 337, in _call_chain result = func(_args) File "/usr/lib64/python2.4/urllib2.py", line 480, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 400: Bad Request

Can you help me get this working from the command line please?

Ron

RandyLevensalor commented 11 years ago

The mode is set at the structure context, not the device context. Use /v2/put/shared. for the url.

The valid modes are cool, heat, range, and off.

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
youngrh commented 11 years ago

Randy,

Thank you so much for that hint. Not being a python programmer certainly put me at a disadvantage!.

All I wanted to do was to create a feature that the Nest folks left out. I like to sleep in a cool house but this time of year it really doesn't get cold outside until very late in the evening. I wanted to switch the Nest to cool before going to bed and have a program switch it back to heat sometime early in the morning after the house had cooled down below a certain temp. The result would be that the system would switch to heat mode while I was sleeping but not actually turn on the heat until it got down to my scheduled setpoint.

In other words, switch between my heating and cooling schedules instead of Nest's Range mode which is useless to me. If one sits in the house all day like my disabled wife does, a 5 degree temp range is just uncomfortable part of the time!

smbaker and/or Randy,

I have attached the nest.py.rhy (rhy is my initials) that contains my inexperienced hacks to your code. I have also attached the script that I wrote to use your code. Please feel free to use, modify, include in your repos, etc. I execute the code every 20 min and write output to a log with the following crontab for root entry:

/20 * * * \ /root/bin/nestAPI >> /var/log/nest.log # run this every 20 minutes

Thank you so much for all the contributor's help! It was fun adding functionality to a community project that I could actually use!!

Ron

Regards,

Ron Young 919-621-9015 http://www.linkedin.com/in/ronhyoung

+++++++++++++++++++ Little tiny dreams require little tiny thoughts and little tiny steps. Great big dreams require great big thoughts and little tiny steps. +++++++++++++++++++ Kosh: The avalanche has already started. It is too late for the pebbles to vote.

On Sun, Nov 25, 2012 at 6:24 PM, RandyLevensalor notifications@github.comwrote:

The mode is set at the structure context, not the device context. Use /v2/put/shared. for the url.

The valid modes are cool, heat, range, and off.

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

— Reply to this email directly or view it on GitHubhttps://github.com/smbaker/pynest/issues/4#issuecomment-10700319.

abemassry commented 11 years ago

Just submitted this pull request #10 based on this issue https://github.com/abemassry/pynest/commit/89d371335d81e8a5896127a3ef2736d82ad91f81

It's working just like @RandyLevensalor shows Thanks, Abe