#! /usr/bin/env python

import requests
import sys

my_token = 'my_nest_access_token'

everything = requests.get('',
             headers={'Authorization': 'Bearer ' + my_token, 'Content-Type': 'application/json'})

for x in everything.json()['thermostats']:
  device_id = x
  break

if sys.argv[1] == 'off':
  print 'off'
  cmd = 'off'
elif sys.argv[1] == 'on':
  print 'on'
  cmd = 'heat-cool'

set = requests.put('' +
      device_id +
      '?auth=my_nest_access_token',
      headers={'Authorization': 'Bearer ' + my_token, 'Content-type': 'application/json'},
               data='{"hvac_mode": cmd}')
Tags: