Skip to content

Commit

Permalink
support v1.1 API
Browse files Browse the repository at this point in the history
  • Loading branch information
nasa9084 committed Nov 11, 2022
1 parent 53d4270 commit 9d58089
Show file tree
Hide file tree
Showing 10 changed files with 867 additions and 241 deletions.
430 changes: 345 additions & 85 deletions device.go

Large diffs are not rendered by default.

180 changes: 73 additions & 107 deletions device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/nasa9084/go-switchbot"
)

Expand Down Expand Up @@ -44,7 +46,7 @@ func TestDevices(t *testing.T) {
)
defer srv.Close()

c := switchbot.New("", switchbot.WithEndpoint(srv.URL))
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))
devices, infrared, err := c.Device().List(context.Background())
if err != nil {
t.Fatal(err)
Expand All @@ -58,29 +60,16 @@ func TestDevices(t *testing.T) {

got := devices[0]

if want := "500291B269BE"; got.ID != want {
t.Errorf("device ID is not match: %s != %s", got.ID, want)
return
}

if want := "Living Room Humidifier"; got.Name != want {
t.Errorf("device name is not match: %s != %s", got.Name, want)
return
}

if want := switchbot.Humidifier; got.Type != want {
t.Errorf("device type is not match: %s != %s", got.Type, want)
return
}

if !got.IsEnableCloudService {
t.Errorf("device.enableCloudService should be true but false")
return
want := switchbot.Device{
ID: "500291B269BE",
Name: "Living Room Humidifier",
Type: switchbot.Humidifier,
IsEnableCloudService: true,
Hub: "000000000000",
}

if want := "000000000000"; got.Hub != want {
t.Errorf("device's parent hub id is not match: %s != %s", got.Hub, want)
return
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("device mismatch (-want +got):\n%s", diff)
}
})

Expand All @@ -92,24 +81,15 @@ func TestDevices(t *testing.T) {

got := infrared[0]

if want := "02-202008110034-13"; got.ID != want {
t.Errorf("infrared device ID is not match: %s != %s", got.ID, want)
return
}

if want := "Living Room TV"; got.Name != want {
t.Errorf("infrared device name is not match: %s != %s", got.Name, want)
return
want := switchbot.InfraredDevice{
ID: "02-202008110034-13",
Name: "Living Room TV",
Type: switchbot.TV,
Hub: "FA7310762361",
}

if want := switchbot.TV; got.Type != want {
t.Errorf("infrared device type is not match: %s != %s", got.Type, want)
return
}

if want := "FA7310762361"; got.Hub != want {
t.Errorf("infrared device's parent hub id is not match: %s != %s", got.Hub, want)
return
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("infrared device mismatch (-want +got):\n%s", diff)
}
})
}
Expand All @@ -119,7 +99,7 @@ func TestDeviceStatus(t *testing.T) {
t.Run("meter", func(t *testing.T) {
srv := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1.0/devices/C271111EC0AB/status" {
if r.URL.Path != "/v1.1/devices/C271111EC0AB/status" {
t.Fatalf("unexpected request path: %s", r.URL.Path)
}

Expand All @@ -139,43 +119,30 @@ func TestDeviceStatus(t *testing.T) {
)
defer srv.Close()

c := switchbot.New("", switchbot.WithEndpoint(srv.URL))
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))
got, err := c.Device().Status(context.Background(), "C271111EC0AB")
if err != nil {
t.Fatal(err)
}

if want := "C271111EC0AB"; got.ID != want {
t.Errorf("devicee id is not match: %s != %s", got.ID, want)
return
}

if want := switchbot.Meter; got.Type != want {
t.Errorf("device type is not match: %s != %s", got.Type, want)
return
want := switchbot.DeviceStatus{
ID: "C271111EC0AB",
Type: switchbot.Meter,
Hub: "FA7310762361",
Humidity: 52,
Temperature: 26.1,
}

if want := "FA7310762361"; got.Hub != want {
t.Errorf("device's parent hub id is not match: %s != %s", got.Hub, want)
return
}

if want := 52; got.Humidity != want {
t.Errorf("humidity is not match: %d != %d", got.Humidity, want)
return
}

if want := 26.1; got.Temperature != want {
t.Errorf("temperature is not match: %f != %f", got.Temperature, want)
return
if diff := cmp.Diff(want, got, cmp.AllowUnexported(switchbot.BrightnessState{})); diff != "" {
t.Fatalf("status mismatch (-want +got):\n%s", diff)
}
})

// https://github.com/OpenWonderLabs/SwitchBotAPI/blob/7a68353d84d07d439a11cb5503b634f24302f733/README.md#switchbot-curtain-example
t.Run("curtain", func(t *testing.T) {
srv := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1.0/devices/E2F6032048AB/status" {
if r.URL.Path != "/v1.1/devices/E2F6032048AB/status" {
t.Fatalf("unexpected request path: %s", r.URL.Path)
}

Expand All @@ -197,45 +164,24 @@ func TestDeviceStatus(t *testing.T) {
)
defer srv.Close()

c := switchbot.New("", switchbot.WithEndpoint(srv.URL))
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))
got, err := c.Device().Status(context.Background(), "E2F6032048AB")
if err != nil {
t.Fatal(err)
}

if want := "E2F6032048AB"; got.ID != want {
t.Errorf("devicee id is not match: %s != %s", got.ID, want)
return
want := switchbot.DeviceStatus{
ID: "E2F6032048AB",
Type: switchbot.Curtain,
Hub: "FA7310762361",
IsCalibrated: true,
IsGrouped: false,
IsMoving: false,
SlidePosition: 0,
}

if want := switchbot.Curtain; got.Type != want {
t.Errorf("device type is not match: %s != %s", got.Type, want)
return
}

if want := "FA7310762361"; got.Hub != want {
t.Errorf("device's parent hub id is not match: %s != %s", got.Hub, want)
return
}

if !got.IsCalibrated {
t.Error("device is calibrated but got false")
return
}

if got.IsGrouped {
t.Error("device is not grouped but got true")
return
}

if got.IsMoving {
t.Error("device is not moving but got true")
return
}

if want := 0; got.SlidePosition != want {
t.Errorf("slide position is not match: %d != %d", got.Humidity, want)
return
if diff := cmp.Diff(want, got, cmp.AllowUnexported(switchbot.BrightnessState{})); diff != "" {
t.Fatalf("status mismatch (-want +got):\n%s", diff)
}
})
}
Expand Down Expand Up @@ -309,7 +255,7 @@ func TestDeviceStatusBrightness(t *testing.T) {
)
defer srv.Close()

c := switchbot.New("", switchbot.WithEndpoint(srv.URL))
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))
got, err := c.Device().Status(context.Background(), "E2F6032048AB")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -355,66 +301,86 @@ func testDeviceCommand(t *testing.T, wantPath string, wantBody string) http.Hand
}

func TestDeviceCommand(t *testing.T) {
t.Run("create a temporary passcode", func(t *testing.T) {
srv := httptest.NewServer(testDeviceCommand(
t,
"/v1.1/devices/F7538E1ABCEB/commands",
`{"command":"createKey","parameter":"{\"name\":\"Guest Code\",\"type\":\"timeLimit\",\"password\":\"12345678\",\"startTime\":1664640056,\"endTime\":1665331432}","commandType":"command"}
`,
))
defer srv.Close()

c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))

cmd, err := switchbot.CreateKeyCommand("Guest Code", switchbot.TimeLimitPasscode, "12345678", time.Date(2022, time.October, 1, 16, 00, 56, 0, time.UTC), time.Date(2022, time.October, 9, 16, 3, 52, 0, time.UTC))
if err != nil {
t.Fatal(err)
}

if err := c.Device().Command(context.Background(), "F7538E1ABCEB", cmd); err != nil {
t.Fatal(err)
}
})
t.Run("turn a bot on", func(t *testing.T) {
srv := httptest.NewServer(testDeviceCommand(
t,
"/v1.0/devices/210/commands",
"/v1.1/devices/210/commands",
`{"command":"turnOn","parameter":"default","commandType":"command"}
`,
))
defer srv.Close()

c := switchbot.New("", switchbot.WithEndpoint(srv.URL))
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))

if err := c.Device().Command(context.Background(), "210", switchbot.TurnOn()); err != nil {
if err := c.Device().Command(context.Background(), "210", switchbot.TurnOnCommand()); err != nil {
t.Fatal(err)
}
})

t.Run("set the color value of a Color Bulb Request", func(t *testing.T) {
srv := httptest.NewServer(testDeviceCommand(
t,
"/v1.0/devices/84F70353A411/commands",
"/v1.1/devices/84F70353A411/commands",
`{"command":"setColor","parameter":"122:80:20","commandType":"command"}
`,
))
defer srv.Close()

c := switchbot.New("", switchbot.WithEndpoint(srv.URL))
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))

if err := c.Device().Command(context.Background(), "84F70353A411", switchbot.SetColor(122, 80, 20)); err != nil {
if err := c.Device().Command(context.Background(), "84F70353A411", switchbot.SetColorCommand(122, 80, 20)); err != nil {
t.Fatal(err)
}
})

t.Run("set an air conditioner", func(t *testing.T) {
srv := httptest.NewServer(testDeviceCommand(
t,
"/v1.0/devices/02-202007201626-70/commands",
"/v1.1/devices/02-202007201626-70/commands",
`{"command":"setAll","parameter":"26,1,3,on","commandType":"command"}
`,
))
defer srv.Close()

c := switchbot.New("", switchbot.WithEndpoint(srv.URL))
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))

if err := c.Device().Command(context.Background(), "02-202007201626-70", switchbot.ACSetAll(26, switchbot.ACAuto, switchbot.ACMedium, switchbot.PowerOn)); err != nil {
if err := c.Device().Command(context.Background(), "02-202007201626-70", switchbot.ACSetAllCommand(26, switchbot.ACAuto, switchbot.ACMedium, switchbot.PowerOn)); err != nil {
t.Fatal(err)
}
})

t.Run("set trigger a customized button", func(t *testing.T) {
srv := httptest.NewServer(testDeviceCommand(
t,
"/v1.0/devices/02-202007201626-10/commands",
"/v1.1/devices/02-202007201626-10/commands",
`{"command":"ボタン","parameter":"default","commandType":"customize"}
`,
))
defer srv.Close()

c := switchbot.New("", switchbot.WithEndpoint(srv.URL))
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))

if err := c.Device().Command(context.Background(), "02-202007201626-10", switchbot.ButtonPush("ボタン")); err != nil {
if err := c.Device().Command(context.Background(), "02-202007201626-10", switchbot.ButtonPushCommand("ボタン")); err != nil {
t.Fatal(err)
}
})
Expand Down
7 changes: 5 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
)

func ExamplePrintPhysicalDevices() {
const openToken = "blahblahblah"
const (
openToken = "blahblahblah"
secretKey = "blahblahblah"
)

c := switchbot.New(openToken)
c := switchbot.New(openToken, secretKey)

// get physical devices and show
pdev, _, _ := c.Device().List(context.Background())
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/nasa9084/go-switchbot

go 1.18

require github.com/google/go-cmp v0.5.9 // indirect
require (
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9 changes: 5 additions & 4 deletions scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Scene struct {
// List get a list of manual scenes created by the current user.
// The first returned value is a list of scenes.
func (svc *SceneService) List(ctx context.Context) ([]Scene, error) {
const path = "/v1.0/scenes"
const path = "/v1.1/scenes"

resp, err := svc.c.get(ctx, path)
if err != nil {
Expand All @@ -57,15 +57,16 @@ func (svc *SceneService) List(ctx context.Context) ([]Scene, error) {
}

type sceneExecuteResponse struct {
StatusCode int `json:"statusCode"`
Message string `json:"message"`
StatusCode int `json:"statusCode"`
Message string `json:"message"`
Body interface{} `json:"body"`
}

// Execute sends a request to execute a manual scene.
// The first given argument `id` is a scene ID which you want to execute, which can
// be retrieved by (*Client).Scene().List() function.
func (svc *SceneService) Execute(ctx context.Context, id string) error {
path := "/v1.0/scenes/" + id + "/execute"
path := "/v1.1/scenes/" + id + "/execute"

resp, err := svc.c.post(ctx, path, nil)
if err != nil {
Expand Down
Loading

0 comments on commit 9d58089

Please sign in to comment.