nest

package module
v0.0.0-...-3ea2347 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2015 License: MIT Imports: 7 Imported by: 4

README

#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

Documentation

Index

Constants

View Source
const (
	// Version is the version of the library
	Version = "0.0.1"
	// APIURL is the main URL for the API
	APIURL = "https://developer-api.nest.com"
	// AccessTokenURL is the Next API URL to get an access_token
	AccessTokenURL = "https://api.home.nest.com/oauth2/access_token"
	// NoStream indicates wedo not want to stream on a GET for server side events
	NoStream = iota
	//Stream indicates we want to stream on a GET for server side events
	Stream
	// Cool sets HvacMode to "cool"
	Cool
	// Heat sets HvacMode to "heat"
	Heat
	// HeatCool sets HvacMode to "heat-cool"
	HeatCool
	// Off sets HvacMode to "off"
	Off
	// Home sets Away mode to "home"
	Home
	// Away sets Away mode to "away"
	Away
	// AutoAway sets Away mode to "auto-away"
	AutoAway
)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Error       string `json:"error,omitempty"`
	Description string `json:"error_description,omitempty"`
	Status      string
	StatusCode  int
}

APIError represents an error object from the Nest API

type Access

type Access struct {
	Token     string `json:"access_token,omitempty"`
	ExpiresIn int    `json:"expires_in,omitempty"`
}

Access represents a Nest access token object

type Client

type Client struct {
	ID                string
	State             string
	AuthorizationCode string
	Secret            string
	Token             string
	ExpiresIn         int
	AccessTokenURL    string
	APIURL            string
	RedirectURL       string
}

Client represents a client object

func New

func New(clientID string, state string, clientSecret string, authorizationCode string) *Client

New creates a new Nest client

client := New("1234", "STATE", "<secret>", "<auth-code>")

func (*Client) Authorize

func (c *Client) Authorize() *APIError

Authorize fetches and sets the Nest API token https://developer.nest.com/documentation/how-to-auth

client.Authorize()

func (*Client) Devices

func (c *Client) Devices() (*Devices, *APIError)

Devices returns a list of devices

devices := client.Devices()

func (*Client) DevicesStream

func (c *Client) DevicesStream(callback func(devices *Devices, err error))

DevicesStream emits events from the Nest devices REST streaming API

client.DevicesStream(func(event *Devices) {
	fmt.Println(event)
})

func (*Client) Structures

func (c *Client) Structures() (map[string]*Structure, *APIError)

Structures returns a map of structures https://developer.nest.com/documentation/api#structures

structures := client.Structures()

func (*Client) StructuresStream

func (c *Client) StructuresStream(callback func(structures map[string]*Structure, err error))

Structures Stream emits events from the Nest structures REST streaming API

client.StructuresStream(func(event map[string]*Structure) {
	fmt.Println(event)
})

type Combined

type Combined struct {
	Devices    *Devices              `json:"devices,omitempty"`
	Structures map[string]*Structure `json:"structures,omitempty"`
}

Combined represents an object for the entire API including devices and structures

type Devices

type Devices struct {
	Thermostats   map[string]*Thermostat   `json:"thermostats,omitempty"`
	SmokeCoAlarms map[string]*SmokeCoAlarm `json:"smoke_co_alarms,omitempty"`
}

Devices represents devices that include thermostats and smokecoalarms

type DevicesEvent

type DevicesEvent struct {
	Path string   `json:"path,omitempty"`
	Data *Devices `json:"data,omitempty"`
}

DevicesEvent represents an object returned by the REST streaming API

type ETA

type ETA struct {
	TripID                      string    `json:"trip_id,omitempty"`
	EstimatedArrivalWindowBegin time.Time `json:"estimated_arrival_window_begin,omitempty"`
	EstimatedArrivalWindowEnd   time.Time `json:"estimated_arrival_window_end,omitempty"`
}

Eta represents an eta object (estimated time of a arrival for a structure)

type SmokeCoAlarm

type SmokeCoAlarm struct {
	DeviceID        string    `json:"device_id,omitempty"`
	Locale          string    `json:"locale,omitempty"`
	SoftwareVersion string    `json:"software_version,omitempty"`
	StructureID     string    `json:"structure_id,omitempty"`
	Name            string    `json:"name,omitempty"`
	NameLong        string    `json:"name_long,omitempty"`
	LastConnection  time.Time `json:"last_connection,omitempty"`
	IsOnline        bool      `json:"is_online,omitempty"`
	BatteryHealth   string    `json:"battery_health,omitempty"`
	CoAlarmState    string    `json:"co_alarm_state,omitempty"`
	SmokeAlarmState string    `json:"smoke_alarm_state,omitempty"`
	UIColorState    string    `json:"ui_color_state,omitempty"`
	Client          *Client
}

SmokeCoAlarm represents a Nest smokecoalarm object (Smoke & CO2 alarm) https://developer.nest.com/documentation/api#smoke_co_alarms https://developer.nest.com/documentation/how-to-smoke-co-alarms-object

type Structure

type Structure struct {
	StructureID         string    `json:"structure_id,omitempty"`
	Thermostats         []string  `json:"thermostats,omitempty"`
	SmokeCoAlarms       []string  `json:"smoke_co_alarms,omitempty"`
	Away                string    `json:"away,omitempty"`
	Name                string    `json:"name,omitempty"`
	CountryCode         string    `json:"country_code,omitempty"`
	PeakPeriodStartTime time.Time `json:"peak_period_start_time,omitempty"`
	PeakPeriodEndTime   time.Time `json:"peak_period_end_time,omitempty"`
	TimeZone            string    `json:"time_zone,omitempty"`
	ETA                 *ETA      `json:"eta,omitempty"`
	Client              *Client
}

Structure represents a Next structure object https://developer.nest.com/documentation/api#structures https://developer.nest.com/documentation/how-to-structures-object

func (*Structure) SetAway

func (s *Structure) SetAway(mode int) *APIError

SetAway sets the away status of a structure https://developer.nest.com/documentation/api#away

s.SetAway(nest.Away)

func (*Structure) SetETA

func (s *Structure) SetETA(tripID string, begin time.Time, end time.Time) *APIError

SetETA sets the ETA for the Nest API https://developer.nest.com/documentation/eta-reference

type StructuresEvent

type StructuresEvent struct {
	Path string                `json:"path,omitempty"`
	Data map[string]*Structure `json:"data,omitempty"`
}

StructuresEvent represents an object returned by the REST streaming API

type Tempratures

type Tempratures struct {
	TargetTemperatureF     int     `json:"target_temperature_f,omitempty"`
	TargetTemperatureC     float32 `json:"target_temperature_c,omitempty"`
	TargetTemperatureHighF int     `json:"target_temperature_high_f,omitempty"`
	TargetTemperatureHighC float32 `json:"target_temperature_high_c,omitempty"`
	TargetTemperatureLowF  int     `json:"target_temperature_low_f,omitempty"`
	TargetTemperatureLowC  float32 `json:"target_temperature_low_c,omitempty"`
}

Tempratures represents all of the possible temprature settings for a Nest thermostat

type Thermostat

type Thermostat struct {
	DeviceID               string    `json:"device_id,omitempty"`
	Locale                 string    `json:"locale,omitempty"`
	SoftwareVersion        string    `json:"software_version,omitempty"`
	StructureID            string    `json:"structure_id,omitempty"`
	Name                   string    `json:"name,omitempty"`
	NameLong               string    `json:"name_long,omitempty"`
	LastConnection         time.Time `json:"last_connection,omitempty"`
	IsOnline               bool      `json:"is_online,omitempty"`
	CanCool                bool      `json:"can_cool,omitempty"`
	CanHeat                bool      `json:"can_heat,omitempty"`
	IsUsingEmergencyHeat   bool      `json:"is_using_emergency_heat,omitempty"`
	HasFan                 bool      `json:"has_fan,omitempty"`
	FanTimerActive         bool      `json:"fan_timer_active,omitempty"`
	FanTimerTimeout        time.Time `json:"fan_timer_timeout,omitempty"`
	HasLeaf                bool      `json:"has_leaf,omitempty"`
	TemperatureScale       string    `json:"temperature_scale,omitempty"`
	TargetTemperatureF     int       `json:"target_temperature_f,omitempty"`
	TargetTemperatureC     float32   `json:"target_temperature_c,omitempty"`
	TargetTemperatureHighF int       `json:"target_temperature_high_f,omitempty"`
	TargetTemperatureHighC float32   `json:"target_temperature_high_c,omitempty"`
	TargetTemperatureLowF  int       `json:"target_temperature_low_f,omitempty"`
	TargetTemperatureLowC  float32   `json:"target_temperature_low_c,omitempty"`
	AwayTemperatureHighF   int       `json:"away_temperature_high_f,omitempty"`
	AwayTemperatureHighC   float32   `json:"away_temperature_high_c,omitempty"`
	AwayTemperatureLowF    int       `json:"away_temperature_low_f,omitempty"`
	AwayTemperatureLowC    float32   `json:"away_temperature_low_c,omitempty"`
	HvacMode               string    `json:"hvac_mode,omitempty"`
	AmbientTemperatureF    int       `json:"ambient_temperature_f,omitempty"`
	AmbientTemperatureC    float32   `json:"ambient_temperature_c,omitempty"`
	Humidity               int       `json:"humidity,omitempty"`
	HvacState              string    `json:"hvac_state,omitempty"`
	WhereID                string    `json:"where_id,omitempty"`
	Client                 *Client
}

Thermostat represents a Nest thermostat object https://developer.nest.com/documentation/api#thermostats https://developer.nest.com/documentation/how-to-thermostats-object

func (*Thermostat) SetFanTimerActive

func (t *Thermostat) SetFanTimerActive(setting bool) *APIError

SetFanTimerActive sets the fan timer on or off https://developer.nest.com/documentation/api#fan_timer_active

t.SetFanTimerActive(true)

func (*Thermostat) SetHvacMode

func (t *Thermostat) SetHvacMode(mode int) *APIError

SetHvacMode sets the HvacMode when a thermostat may heat and cool https://developer.nest.com/documentation/api#hvac_mode

t.SetHvacMode(Cool)

func (*Thermostat) SetTargetTempC

func (t *Thermostat) SetTargetTempC(temp float32) *APIError

SetTargetTempC sets the thermostat to an intended temp in celcius https://developer.nest.com/documentation/api#target_temperature_c

t.SetTargetTempC(28.5)

func (*Thermostat) SetTargetTempF

func (t *Thermostat) SetTargetTempF(temp int) *APIError

SetTargetTempF sets the thermostat to an intended temp in farenheit https://developer.nest.com/documentation/api#target_temperature_f

t.SetTargetTempF(78)

func (*Thermostat) SetTargetTempHighLowC

func (t *Thermostat) SetTargetTempHighLowC(high float32, low float32) *APIError

SetTargetTempHighLowC sets the high target temp in celcius when HvacMode is HeatCool https://developer.nest.com/documentation/api#target_temperature_high_c https://developer.nest.com/documentation/api#target_temperature_low_c

t.SetTargetTempHighLowF(75, 65)

func (*Thermostat) SetTargetTempHighLowF

func (t *Thermostat) SetTargetTempHighLowF(high int, low int) *APIError

SetTargetTempHighLowF sets the high target temp in farenheit when HvacMode is HeatCool https://developer.nest.com/documentation/api#target_temperature_high_f https://developer.nest.com/documentation/api#target_temperature_low_f

t.SetTargetTempHighLowF(75, 65)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL