Nest is an implementation of the Nest API in Go.
A Go library for Nest devices. This library provides basic support for Nest Cameras (work-in-progress), Thermostats, and SmokeCoAlarms. There is support for integrating golang OAuth2.0 support into the HTTP client and is expected when constructing a new client.
Jon Tsiros
Want to support me?
go get github.com/jtsiros/nest
package main
import (
"context"
"fmt"
"log"
"github.com/jtsiros/nest"
"github.com/jtsiros/nest/auth"
"github.com/jtsiros/nest/config"
)
func main() {
// Interactive OAuth2 configuration
appConfig := config.Config{
APIURL: config.APIURL,
}
conf := auth.NewConfig(appConfig)
tok, err := auth.NewConfigWithToken("[TOKEN]").Token()
if err != nil {
log.Fatal(err)
}
client := conf.Client(context.Background(), tok)
n, err := nest.NewClient(appConfig, client)
fmt.Println(n.Devices())
}
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/jtsiros/nest"
"github.com/jtsiros/nest/auth"
"github.com/jtsiros/nest/config"
"golang.org/x/oauth2"
)
func main() {
// Interactive OAuth2 configuration
appConfig := config.Config{
ClientID: "[CLIENT_ID]",
Secret: "[SECRET]",
APIURL: config.APIURL,
}
conf := auth.NewConfig(appConfig)
url := conf.AuthCodeURL("STATE")
fmt.Printf("Enter code from this authorization URL: %v\n", url)
var code string
if _, err := fmt.Scan(&code); err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
token, err := conf.Exchange(ctx, code,
oauth2.SetAuthURLParam("client_id", appConfig.ClientID),
oauth2.SetAuthURLParam("client_secret", appConfig.Secret),
)
if err != nil {
log.Fatal(err)
}
client := conf.Client(ctx, token)
n, err := nest.NewClient(appConfig, client)
fmt.Println(n.Devices())
}
thermostat, err := n.Thermostats.Get("[DEVICE_ID]")
// ... error handling
fmt.Println(thermostat.TargetTemperatureF)
n.Thermostats.SetHVACMode(thermostat.DeviceID, nest.Heat)
smokeCoAlarm, err := n.SmokeCoAlarms.Get("[DEVICE_ID]")
// ... error handling
fmt.Println(smokeCoAlarm.LastConnection)
At this time, only read-only portion of the API is implemented. I’m planning on implementing the write calls
once I integrate with my HomeKit integration.
camera, err := n.Cameras.Get("[DEVICE_ID]")
// ... error handling
fmt.Println(camera.IsStreaming)
Go Gopher Coding it up by: Kari Linder