GithubHelp home page GithubHelp logo

nest's Introduction

#nest wercker status

A Go library for the Nest API for Nest devices. This is early work and only supports querying the devices object as well as the REST Streaming API for devices.

Version

0.0.1

Installation

go get github.com/jsgoecke/nest

Documentation

http://godoc.org/github.com/jsgoecke/nest

Usage

Thermostats

package main

import (
	"github.com/jsgoecke/nest"
	"encoding/json"
	"fmt"
	"os"
)

const (
	ClientID          = "<client-id>"
	State             = "STATE"
	ClientSecret      = "<client-secret>"
	AuthorizationCode = "<authorization-code> - https://developer.nest.com/documentation/how-to-auth"
	Token             = "<token>"
)

func main() {
	client := nest.New(ClientID, State, ClientSecret, AuthorizationCode)
	client.Token = Token
	devicesChan := make(chan *nest.Devices)
	go func() {
		client.DevicesStream(func(devices *nest.Devices, err error) {
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
			devicesChan <- devices
		})
	}()

	for i := 0; i < 7; i++ {
		devices := <-devicesChan
		thermostat := devices.Thermostats["1cf6CGENM20W3UsKiJTT4Cqpa4SSjzbd"]
		switch i {
		case 0:
			logEvent(devices, i)
			fmt.Println("Setting target temp")
			err := thermostat.SetTargetTempF(thermostat.TargetTemperatureF + 1)
			if err != nil {
				fmt.Printf("Error: %s - %d\n", err.Description, i)
				os.Exit(2)
			}
		case 1:
			logEvent(devices, i)
			fmt.Println("Setting HvacMode to HeatCool")
			err := thermostat.SetHvacMode(nest.HeatCool)
			if err != nil {
				fmt.Printf("Error: %s - %d\n", err.Description, i)
				os.Exit(2)
			}
		case 2:
			logEvent(devices, i)
			fmt.Println("Setting TargetTempHighLow")
			err := thermostat.SetTargetTempHighLowF(thermostat.TargetTemperatureHighF+1, thermostat.TargetTemperatureLowF+1)
			if err != nil {
				fmt.Printf("Error: %s - %d\n", err.Description, i)
				os.Exit(2)
			}
		case 3:
			logEvent(devices, i)
			fmt.Println("Setting HvacMode to Heat")
			err := thermostat.SetHvacMode(nest.Heat)
			if err != nil {
				fmt.Printf("Error: %s - %d\n", err.Description, i)
				os.Exit(2)
			}
		case 4:
			logEvent(devices, i)
			fmt.Println("Setting FanTimeActive to true")
			err := thermostat.SetFanTimerActive(true)
			if err != nil {
				fmt.Printf("Error: %s - %d\n", err.Description, i)
				os.Exit(2)
			}
		case 5:
			logEvent(devices, i)
			fmt.Println("Setting FanTimeActive to false")
			err := thermostat.SetFanTimerActive(false)
			if err != nil {
				fmt.Printf("Error: %s - %d\n", err.Description, i)
				os.Exit(2)
			}
		case 6:
			logEvent(devices, i)
			break
		}
	}
}

func logEvent(devices *nest.Devices, cnt int) {
	fmt.Printf(">>>>>%d<<<<<\n", cnt)
	data, _ := json.MarshalIndent(devices, "", "  ")
	fmt.Println(string(data))
	fmt.Printf(">>>>>%d<<<<<\n", cnt)
}

Structures

package main

import (
	"github.com/jsgoecke/nest"
	"encoding/json"
	"fmt"
	"os"
	"time"
)

const (
	ClientID          = "<client-id>"
	State             = "STATE"
	ClientSecret      = "<client-secret>"
	AuthorizationCode = "<authorization-code> - https://developer.nest.com/documentation/how-to-auth"
	Token             = "<token>"
)

func main() {
	client := nest.New(ClientID, State, ClientSecret, AuthorizationCode)
	client.Token = Token
	structuresChan := make(chan map[string]*nest.Structure)
	go func() {
		client.StructuresStream(func(structures map[string]*nest.Structure, err error) {
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
			structuresChan <- structures
		})
	}()

	for i := 0; i < 3; i++ {
		structures := <-structuresChan
		switch i {
		case 0:
			logEvent(structures, i)
			fmt.Println("Setting away status")
			err := structures["h68sn..."].SetAway(nest.Away)
			if err != nil {
				fmt.Printf("Error: %s - %d\n", err.Description, i)
				os.Exit(2)
			}
		case 2:
			logEvent(structures, i)
			fmt.Println("Setting ETA")
			err := structures["h68sn..."].SetETA("foobar-trip-id", time.Now().Add(10*time.Minute), time.Now().Add(30*time.Minute))
			if err != nil {
				fmt.Printf("Error: %s - %d\n", err.Description, i)
				os.Exit(2)
			}
			logEvent(structures, i)
		case 3:
			logEvent(structures, i)
			break
		}
	}
}

func logEvent(structures map[string]*nest.Structure, cnt int) {
	fmt.Printf(">>>>>%d<<<<<\n", cnt)
	data, _ := json.MarshalIndent(structures, "", "  ")
	fmt.Println(string(data))
	fmt.Printf(">>>>>%d<<<<<\n", cnt)
}

Testing

cd nest
go test

License

MIT, see LICENSE.txt

Author

Jason Goecke @jsgoecke

nest's People

Contributors

ablyler avatar jsgoecke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.