-
Notifications
You must be signed in to change notification settings - Fork 10
/
device_test.go
387 lines (335 loc) · 10.5 KB
/
device_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package switchbot_test
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/nasa9084/go-switchbot/v3"
)
// https://github.com/OpenWonderLabs/SwitchBotAPI/blob/7a68353d84d07d439a11cb5503b634f24302f733/README.md#get-all-devices
func TestDevices(t *testing.T) {
srv := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{
"statusCode": 100,
"body": {
"deviceList": [
{
"deviceId": "500291B269BE",
"deviceName": "Living Room Humidifier",
"deviceType": "Humidifier",
"enableCloudService": true,
"hubDeviceId": "000000000000"
}
],
"infraredRemoteList": [
{
"deviceId": "02-202008110034-13",
"deviceName": "Living Room TV",
"remoteType": "TV",
"hubDeviceId": "FA7310762361"
}
]
},
"message": "success"
}`))
}),
)
defer srv.Close()
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))
devices, infrared, err := c.Device().List(context.Background())
if err != nil {
t.Fatal(err)
}
t.Run("devices", func(t *testing.T) {
if len(devices) != 1 {
t.Errorf("the number of devices is expected to 1, but %d", len(devices))
return
}
got := devices[0]
want := switchbot.Device{
ID: "500291B269BE",
Name: "Living Room Humidifier",
Type: switchbot.Humidifier,
IsEnableCloudService: true,
Hub: "000000000000",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("device mismatch (-want +got):\n%s", diff)
}
})
t.Run("infrared devices", func(t *testing.T) {
if len(infrared) != 1 {
t.Errorf("the number of infrared devices is expected to 1, but %d", len(infrared))
return
}
got := infrared[0]
want := switchbot.InfraredDevice{
ID: "02-202008110034-13",
Name: "Living Room TV",
Type: switchbot.TV,
Hub: "FA7310762361",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Fatalf("infrared device mismatch (-want +got):\n%s", diff)
}
})
}
func TestDeviceStatus(t *testing.T) {
// https://github.com/OpenWonderLabs/SwitchBotAPI/blob/7a68353d84d07d439a11cb5503b634f24302f733/README.md#switchbot-meter-example
t.Run("meter", func(t *testing.T) {
srv := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1.1/devices/C271111EC0AB/status" {
t.Fatalf("unexpected request path: %s", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{
"statusCode": 100,
"body": {
"deviceId": "C271111EC0AB",
"deviceType": "Meter",
"hubDeviceId": "FA7310762361",
"humidity": 52,
"temperature": 26.1
},
"message": "success"
}`))
}),
)
defer srv.Close()
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))
got, err := c.Device().Status(context.Background(), "C271111EC0AB")
if err != nil {
t.Fatal(err)
}
want := switchbot.DeviceStatus{
ID: "C271111EC0AB",
Type: switchbot.Meter,
Hub: "FA7310762361",
Humidity: 52,
Temperature: 26.1,
}
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.1/devices/E2F6032048AB/status" {
t.Fatalf("unexpected request path: %s", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{
"statusCode": 100,
"body": {
"deviceId": "E2F6032048AB",
"deviceType": "Curtain",
"hubDeviceId": "FA7310762361",
"calibrate": true,
"group": false,
"moving": false,
"slidePosition": 0
},
"message": "success"
}`))
}),
)
defer srv.Close()
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))
got, err := c.Device().Status(context.Background(), "E2F6032048AB")
if err != nil {
t.Fatal(err)
}
want := switchbot.DeviceStatus{
ID: "E2F6032048AB",
Type: switchbot.Curtain,
Hub: "FA7310762361",
IsCalibrated: true,
IsGrouped: false,
IsMoving: false,
SlidePosition: 0,
}
if diff := cmp.Diff(want, got, cmp.AllowUnexported(switchbot.BrightnessState{})); diff != "" {
t.Fatalf("status mismatch (-want +got):\n%s", diff)
}
})
}
func isSameStringErr(err1, err2 error) bool {
if err1 == nil && err2 == nil {
return true
}
if (err1 == nil && err2 != nil) || (err1 != nil && err2 == nil) {
return false
}
return err1.Error() == err2.Error()
}
func TestDeviceStatusBrightness(t *testing.T) {
type wants struct {
IntValue int
IntErr error
AmbientValue switchbot.AmbientBrightness
AmbientErr error
}
tests := []struct {
label string
body string
want wants
}{
{
label: "color bulb",
body: `{ "deviceType": "Color Bulb", "brightness": 100 }`,
want: wants{
IntValue: 100,
AmbientErr: errors.New("ambient brightness value is only available for motion sensor, contact sensor devices"),
},
},
{
label: "motion sensor",
body: `{ "deviceType": "Motion Sensor", "brightness": "bright" }`,
want: wants{
IntValue: -1,
IntErr: errors.New("integer brightness value is only available for color bulb devices"),
AmbientValue: switchbot.AmbientBrightnessBright,
},
},
{
label: "contact sensor",
body: `{ "devcieType": "Contact Sensor", "brightness": "dim" }`,
want: wants{
IntValue: -1,
IntErr: errors.New("integer brightness value is only available for color bulb devices"),
AmbientValue: switchbot.AmbientBrightnessDim,
},
},
}
for _, tt := range tests {
t.Run(tt.label, func(t *testing.T) {
srv := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf(`{
"statusCode": 100,
"body": %s,
"message": "success"
}`, tt.body)))
}),
)
defer srv.Close()
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))
got, err := c.Device().Status(context.Background(), "E2F6032048AB")
if err != nil {
t.Fatal(err)
}
if gotint, err := got.Brightness.Int(); gotint != tt.want.IntValue || !isSameStringErr(err, tt.want.IntErr) {
t.Errorf("unexpected result for int brightness\n int value: %d != %d\n error: %v != %v", gotint, tt.want.IntValue, err, tt.want.IntErr)
return
}
if gotAmbient, err := got.Brightness.AmbientBrightness(); gotAmbient != tt.want.AmbientValue || !isSameStringErr(err, tt.want.AmbientErr) {
t.Errorf("unexpected result for ambient brightness\n ambient brightness value: %s != %s\n error: %v != %v", gotAmbient, tt.want.AmbientValue, err, tt.want.AmbientErr)
return
}
})
}
}
func testDeviceCommand(t *testing.T, wantPath string, wantBody string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != wantPath {
t.Fatalf("unexpected request path: %s != %s", r.URL.Path, wantPath)
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
if got := string(b); got != wantBody {
t.Fatalf("unexpected request body:\n got: %s\n want: %s",
got, wantBody,
)
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{
"statusCode": 100,
"body": {},
"message": "success"
}`))
})
}
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.1/devices/210/commands",
`{"command":"turnOn","parameter":"default","commandType":"command"}
`,
))
defer srv.Close()
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))
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.1/devices/84F70353A411/commands",
`{"command":"setColor","parameter":"122:80:20","commandType":"command"}
`,
))
defer srv.Close()
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))
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.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))
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.1/devices/02-202007201626-10/commands",
`{"command":"ボタン","parameter":"default","commandType":"customize"}
`,
))
defer srv.Close()
c := switchbot.New("", "", switchbot.WithEndpoint(srv.URL))
if err := c.Device().Command(context.Background(), "02-202007201626-10", switchbot.ButtonPushCommand("ボタン")); err != nil {
t.Fatal(err)
}
})
}