go>> nest>> 返回
项目作者: jtsiros

项目描述 :
Nest is an implementation of the Nest API in Go.
高级语言: Go
项目地址: git://github.com/jtsiros/nest.git
创建时间: 2019-01-11T21:07:21Z
项目社区:https://github.com/jtsiros/nest

开源协议:MIT License

下载


Nest Build Status Coverage Status GoDoc Version Go Report Card


Gopher Stand by: Kari Linder

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.

Author

Jon Tsiros

Want to support me?

Buy Me a Coffee at ko-fi.com

Installation

  1. go get github.com/jtsiros/nest

Usage

Devices

Existing Token

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "github.com/jtsiros/nest"
  7. "github.com/jtsiros/nest/auth"
  8. "github.com/jtsiros/nest/config"
  9. )
  10. func main() {
  11. // Interactive OAuth2 configuration
  12. appConfig := config.Config{
  13. APIURL: config.APIURL,
  14. }
  15. conf := auth.NewConfig(appConfig)
  16. tok, err := auth.NewConfigWithToken("[TOKEN]").Token()
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. client := conf.Client(context.Background(), tok)
  21. n, err := nest.NewClient(appConfig, client)
  22. fmt.Println(n.Devices())
  23. }

No existing Token

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "time"
  7. "github.com/jtsiros/nest"
  8. "github.com/jtsiros/nest/auth"
  9. "github.com/jtsiros/nest/config"
  10. "golang.org/x/oauth2"
  11. )
  12. func main() {
  13. // Interactive OAuth2 configuration
  14. appConfig := config.Config{
  15. ClientID: "[CLIENT_ID]",
  16. Secret: "[SECRET]",
  17. APIURL: config.APIURL,
  18. }
  19. conf := auth.NewConfig(appConfig)
  20. url := conf.AuthCodeURL("STATE")
  21. fmt.Printf("Enter code from this authorization URL: %v\n", url)
  22. var code string
  23. if _, err := fmt.Scan(&code); err != nil {
  24. log.Fatal(err)
  25. }
  26. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  27. defer cancel()
  28. token, err := conf.Exchange(ctx, code,
  29. oauth2.SetAuthURLParam("client_id", appConfig.ClientID),
  30. oauth2.SetAuthURLParam("client_secret", appConfig.Secret),
  31. )
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. client := conf.Client(ctx, token)
  36. n, err := nest.NewClient(appConfig, client)
  37. fmt.Println(n.Devices())
  38. }

Thermostats

  1. thermostat, err := n.Thermostats.Get("[DEVICE_ID]")
  2. // ... error handling
  3. fmt.Println(thermostat.TargetTemperatureF)
  4. n.Thermostats.SetHVACMode(thermostat.DeviceID, nest.Heat)

SmokeCoAlarms

  1. smokeCoAlarm, err := n.SmokeCoAlarms.Get("[DEVICE_ID]")
  2. // ... error handling
  3. fmt.Println(smokeCoAlarm.LastConnection)

Cameras

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.

  1. camera, err := n.Cameras.Get("[DEVICE_ID]")
  2. // ... error handling
  3. fmt.Println(camera.IsStreaming)

Credits

Go Gopher Coding it up by: Kari Linder