node-switchbot 1.3.0 → 1.4.2-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -23
- package/LICENSE +1 -1
- package/README.md +322 -258
- package/lib/parameter-checker.js +271 -213
- package/lib/switchbot-advertising.js +305 -171
- package/lib/switchbot-device-wocolorbulb.js +81 -0
- package/lib/switchbot-device-wocontact.js +4 -7
- package/lib/switchbot-device-wocurtain.js +106 -91
- package/lib/switchbot-device-wohand.js +69 -65
- package/lib/switchbot-device-wohumi.js +69 -65
- package/lib/switchbot-device-woplugmini.js +81 -0
- package/lib/switchbot-device-wopresence.js +4 -7
- package/lib/switchbot-device-wosensorth.js +4 -7
- package/lib/switchbot-device.js +188 -149
- package/lib/switchbot.js +271 -233
- package/package.json +40 -40
package/README.md
CHANGED
|
@@ -16,7 +16,8 @@ and [SwitchBot Contact Sensor](https://www.switch-bot.com/products/contact-senso
|
|
|
16
16
|
This module is unofficial. It was developed by reference to [the official python code](https://github.com/OpenWonderLabs/python-host).
|
|
17
17
|
But some functionalities of this module were developed through trial and error. So some information obtained from this module might be wrong.
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
---
|
|
20
|
+
|
|
20
21
|
## Table of Contents
|
|
21
22
|
|
|
22
23
|
- [node-switchbot](#node-switchbot)
|
|
@@ -69,9 +70,8 @@ The node-switchbot supports only Linux-based OSes, such as Raspbian, Ubuntu, and
|
|
|
69
70
|
|
|
70
71
|
## Dependencies
|
|
71
72
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
- [Node.js](https://nodejs.org/en/) 10 +
|
|
74
|
+
- [@abandonware/noble](https://github.com/abandonware/noble)
|
|
75
75
|
|
|
76
76
|
See the document of the [@abandonware/noble](https://github.com/abandonware/noble) for details on installing the [@abandonware/noble](https://github.com/abandonware/noble).
|
|
77
77
|
|
|
@@ -95,7 +95,8 @@ $ npm install @abandonware/noble
|
|
|
95
95
|
$ npm install node-switchbot
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
---
|
|
99
|
+
|
|
99
100
|
## Quick Start
|
|
100
101
|
|
|
101
102
|
### Monitoring Advertising packets
|
|
@@ -131,23 +132,26 @@ The [`startScan()`](#startscan-method) and [`wait()`](#Switchbot-wait-method) me
|
|
|
131
132
|
|
|
132
133
|
```javascript
|
|
133
134
|
// Load the node-switchbot and get a `Switchbot` constructor object
|
|
134
|
-
const Switchbot = require(
|
|
135
|
+
const Switchbot = require("node-switchbot");
|
|
135
136
|
// Create an `Switchbot` object
|
|
136
137
|
let switchbot = new Switchbot();
|
|
137
138
|
|
|
138
139
|
// Start to monitor advertisement packets
|
|
139
|
-
switchbot
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
140
|
+
switchbot
|
|
141
|
+
.startScan()
|
|
142
|
+
.then(() => {
|
|
143
|
+
// Set an event hander
|
|
144
|
+
switchbot.onadvertisement = (ad) => {
|
|
145
|
+
console.log(JSON.stringify(ad, null, " "));
|
|
146
|
+
};
|
|
147
|
+
// Wait 10 seconds
|
|
148
|
+
return switchbot.wait(10000);
|
|
149
|
+
})
|
|
150
|
+
.then(() => {
|
|
151
|
+
// Stop to monitor
|
|
152
|
+
switchbot.stopScan();
|
|
153
|
+
process.exit();
|
|
154
|
+
});
|
|
151
155
|
```
|
|
152
156
|
|
|
153
157
|
The sample codes above will output the result as follows:
|
|
@@ -204,15 +208,15 @@ This sample discovers a Bot (WoHand), then put the Bot's arm down, finally put i
|
|
|
204
208
|
|
|
205
209
|
```javascript
|
|
206
210
|
// Load the node-switchbot and get a `Switchbot` constructor object
|
|
207
|
-
const Switchbot = require(
|
|
211
|
+
const Switchbot = require("node-switchbot");
|
|
208
212
|
// Create an `Switchbot` object
|
|
209
213
|
const switchbot = new Switchbot();
|
|
210
214
|
|
|
211
215
|
(async () => {
|
|
212
216
|
// Find a Bot (WoHand)
|
|
213
|
-
const bot_list = await switchbot.discover({ model:
|
|
217
|
+
const bot_list = await switchbot.discover({ model: "H", quick: true });
|
|
214
218
|
if (bot_list.length === 0) {
|
|
215
|
-
throw new Error(
|
|
219
|
+
throw new Error("No device was found.");
|
|
216
220
|
}
|
|
217
221
|
// The `SwitchbotDeviceWoHand` object representing the found Bot.
|
|
218
222
|
let device = bot_list[0];
|
|
@@ -230,7 +234,8 @@ In order to manipulate the arm of your Bot, you have to discover your Bot using
|
|
|
230
234
|
|
|
231
235
|
In this code, you can get a [`SwitchbotDeviceWoHand`](#SwitchbotDeviceWoHand-object) object representing the found Bot. Using the [`down()`](#SwitchbotDeviceWoHand-down-method) and [`up()`](#SwitchbotDeviceWoHand-up-method) methods of the object, you can move the arm. In addition to these methods, you can use the [`press()`](#SwitchbotDeviceWoHand-press-method), [`turnOn()`](#SwitchbotDeviceWoHand-turnOn-method), and [`turnOff()`](#SwitchbotDeviceWoHand-turnOff-method) methods as well.
|
|
232
236
|
|
|
233
|
-
|
|
237
|
+
---
|
|
238
|
+
|
|
234
239
|
## `Switchbot` object
|
|
235
240
|
|
|
236
241
|
In order to use the node-switchbot, you have to load the node-switchbot module as follows:
|
|
@@ -247,9 +252,9 @@ const switchbot = new Switchbot();
|
|
|
247
252
|
|
|
248
253
|
The `Switchbot` constructor takes an argument optionally. It must be a hash object containing the properties as follows:
|
|
249
254
|
|
|
250
|
-
Property | Type
|
|
251
|
-
|
|
252
|
-
`noble` | Noble
|
|
255
|
+
| Property | Type | Required | Description |
|
|
256
|
+
| :------- | :---- | :------- | :---------------------------------------------------------------------------------------- |
|
|
257
|
+
| `noble` | Noble | option | a Noble object of the [`@abandonware/noble`](https://github.com/abandonware/noble) module |
|
|
253
258
|
|
|
254
259
|
The node-switchbot module uses the [`@abandonware/noble`](https://github.com/abandonware/noble) module in order to interact with BLE devices. If you want to interact other BLE devices using the `@abandonware/noble` module, you can create an `Noble` object by yourself, then pass it to this module. If you don't specify a `Noble` object to the `noble` property, this module automatically create a `Noble` object internally.
|
|
255
260
|
|
|
@@ -270,12 +275,12 @@ In the code snippet above, the variable `switchbot` is an `Switchbot` object. Th
|
|
|
270
275
|
|
|
271
276
|
The `discover` method finds devices. This method returns a `Promise` object. This method takes an argument which is a hash object containing parameters as follows:
|
|
272
277
|
|
|
273
|
-
Property
|
|
274
|
-
|
|
275
|
-
`duration`
|
|
276
|
-
`model`
|
|
277
|
-
`id`
|
|
278
|
-
`quick`
|
|
278
|
+
| Property | Type | Required | Description |
|
|
279
|
+
| :--------- | :------ | :------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
280
|
+
| `duration` | Integer | Optional | Duration for discovery process (msec). The default value is 5000 (msec). |
|
|
281
|
+
| `model` | String | Optional | `"H"`, `"T"` or `"c"`. If `"H"` is specified, this method will discover only Bots. If `"T"` is specified, this method will discover only Meters. If `"c"` is specified, this method will discover only Curtains. |
|
|
282
|
+
| `id` | String | Optional | If this value is set, this method will discover only a device whose ID is as same as this value. The ID is identical to the MAC address. This parameter is case-insensitive, and colons are ignored. |
|
|
283
|
+
| `quick` | Boolean | Optional | If this value is `true`, this method finishes the discovery process when the first device is found, then calls the `resolve()` function without waiting the specified `duration`. The default value is `false`. |
|
|
279
284
|
|
|
280
285
|
In the code snippet below, no parameter is passed to the method:
|
|
281
286
|
|
|
@@ -287,7 +292,7 @@ switchbot.discover().then((device_list) => {
|
|
|
287
292
|
});
|
|
288
293
|
```
|
|
289
294
|
|
|
290
|
-
If no parameter is passed to the method as the code above,
|
|
295
|
+
If no parameter is passed to the method as the code above, an `Array` object will be passed to the `resolve()` function in 5 seconds. The `Array` object contains [`SwitchbotDevice`](#SwitchbotDevice-object) objects representing the found devices. See the section "[`SwitchbotDevice`](#SwitchbotDevice-object) objects" for more details.
|
|
291
296
|
|
|
292
297
|
If you want a quick response, you can set the `quick` property to `true`.
|
|
293
298
|
|
|
@@ -333,10 +338,10 @@ The discovery process was finished.
|
|
|
333
338
|
|
|
334
339
|
The `startScan()` method starts to scan advertising packets coming from devices. This method takes an argument which is a hash object containing the parameters as follows:
|
|
335
340
|
|
|
336
|
-
Property
|
|
337
|
-
|
|
338
|
-
`model`
|
|
339
|
-
`id`
|
|
341
|
+
| Property | Type | Required | Description |
|
|
342
|
+
| :------- | :----- | :------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
343
|
+
| `model` | String | Optional | `"H"`, `"T"` or `"c"`. If `"H"` is specified, this method will discover only Bots. If `"T"` is specified, this method will discover only Meters. If `"c"` is specified, this method will discover only Curtains. |
|
|
344
|
+
| `id` | String | Optional | If this value is set, this method will discover only a device whose ID is as same as this value. The ID is identical to the MAC address. This value is case-insensitive, and colons are ignored. |
|
|
340
345
|
|
|
341
346
|
Whenever a packet is received, the callback function set to the [`onadvertisement`](#Switchbot-onadvertisement-event-handler) property of the [`Switchbot`](#Switchbot-object) object will be called. When a packet is received, a hash object representing the packet will be passed to the callback function.
|
|
342
347
|
|
|
@@ -384,7 +389,7 @@ The `serviceData` property depends on the model of the device. See the section "
|
|
|
384
389
|
|
|
385
390
|
### `stopScan()` method
|
|
386
391
|
|
|
387
|
-
The `stopScan()` method stops to scan advertising packets coming from devices. This method returns nothing. Note that this method is
|
|
392
|
+
The `stopScan()` method stops to scan advertising packets coming from devices. This method returns nothing. Note that this method is _not_ asynchronous but synchronous unlike the other methods. See the section "[`startScan()` method](#startscan-method)" for details.
|
|
388
393
|
|
|
389
394
|
### `onadvertisement` event handler
|
|
390
395
|
|
|
@@ -398,7 +403,8 @@ The `wait()` method waits for the specified milliseconds. This method takes an i
|
|
|
398
403
|
|
|
399
404
|
This method has nothing to do with Switchbot devices. It's just a utility method. See the section "[Quick Start](#Quick-Start)" for details of the usage of this method.
|
|
400
405
|
|
|
401
|
-
|
|
406
|
+
---
|
|
407
|
+
|
|
402
408
|
## `SwitchbotDevice` object
|
|
403
409
|
|
|
404
410
|
The `SwitchbotDevice` object represents a Switchbot device (Bot, Meter, Curtain, Contact or Motion), which is created through the discovery process triggered by the [`Switchbot.discover()`](#Switchbot-discover-method) method.
|
|
@@ -411,15 +417,15 @@ You can use the properties and methods described in this section on Bot, Meter,
|
|
|
411
417
|
|
|
412
418
|
The `SwitchbotDevice` object supports the properties as follows:
|
|
413
419
|
|
|
414
|
-
Property
|
|
415
|
-
|
|
416
|
-
`id`
|
|
417
|
-
`address`
|
|
418
|
-
`model` | String | This value is `"H"` "Bot (WoHand)", `"T"` "Meter (WoSensorTH)", `"c"` "Curtain (WoCurtain)", `"d"` "Contact (WoContact)" or `"s"` "Motion (WoMotion)".
|
|
419
|
-
`modelName` | String | This value is `"WoHand"`, `"WoSensorTH"`, `WoCurtain`, `WoContect` or `WoMotion`.
|
|
420
|
-
`connectionState` | String | This value indicates the BLE connection state. `"connecting"`, `"connected"`, `"disconnecting"`, or `"disconnected"`.
|
|
421
|
-
`onconnect` | Function | See the section "[`onconnect` event handler](#SwitchbotDevice-onconnect-event-handler)" for details.
|
|
422
|
-
`ondisconnect` | Function | See the section "[`ondisconnect` event handler](#SwitchbotDevice-ondisconnect-event-handler)" for details.
|
|
420
|
+
| Property | Type | Description |
|
|
421
|
+
| :---------------- | :------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
422
|
+
| `id` | String | ID of the device. (e.g., `"cb4eb903c96d"`) |
|
|
423
|
+
| `address` | String | MAC address of the device. Basically it is as same as the value of the `id` except that this value includes `:` in the string. (e.g., `"cb:4e:b9:03:c9:6d"`) |
|
|
424
|
+
| `model` | String | This value is `"H"` "Bot (WoHand)", `"T"` "Meter (WoSensorTH)", `"c"` "Curtain (WoCurtain)", `"d"` "Contact (WoContact)" or `"s"` "Motion (WoMotion)". |
|
|
425
|
+
| `modelName` | String | This value is `"WoHand"`, `"WoSensorTH"`, `WoCurtain`, `WoContect` or `WoMotion`. |
|
|
426
|
+
| `connectionState` | String | This value indicates the BLE connection state. `"connecting"`, `"connected"`, `"disconnecting"`, or `"disconnected"`. |
|
|
427
|
+
| `onconnect` | Function | See the section "[`onconnect` event handler](#SwitchbotDevice-onconnect-event-handler)" for details. |
|
|
428
|
+
| `ondisconnect` | Function | See the section "[`ondisconnect` event handler](#SwitchbotDevice-ondisconnect-event-handler)" for details. |
|
|
423
429
|
|
|
424
430
|
### `getDeviceName()` method
|
|
425
431
|
|
|
@@ -430,21 +436,25 @@ If no connection is established with the device, this method automatically estab
|
|
|
430
436
|
If the device name is fetched successfully, the device name will be passed to the `resolve()`.
|
|
431
437
|
|
|
432
438
|
```javascript
|
|
433
|
-
switchbot
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
})
|
|
439
|
+
switchbot
|
|
440
|
+
.discover({ model: "H", quick: true })
|
|
441
|
+
.then((device_list) => {
|
|
442
|
+
return device_list[0].getDeviceName();
|
|
443
|
+
})
|
|
444
|
+
.then((name) => {
|
|
445
|
+
console.log(name);
|
|
446
|
+
process.exit();
|
|
447
|
+
})
|
|
448
|
+
.catch((error) => {
|
|
449
|
+
console.error(error);
|
|
450
|
+
process.exit();
|
|
451
|
+
});
|
|
442
452
|
```
|
|
443
453
|
|
|
444
454
|
The code above will output the result as follows:
|
|
445
455
|
|
|
446
456
|
```javascript
|
|
447
|
-
WoHand
|
|
457
|
+
WoHand;
|
|
448
458
|
```
|
|
449
459
|
|
|
450
460
|
### `setDeviceName()` method
|
|
@@ -456,15 +466,19 @@ If no connection is established with the device, this method automatically estab
|
|
|
456
466
|
The character set of the device name saved in the device is UTF-8. The byte length of the name must be less than or equal to 20 bytes. If the name consists of only ASCII characters, up to 20 characters would be allowed. But if the name consists of multibyte characters, the upper limit of characters would be fewer than half. For example, Japanese characters could be saved at most 6 characters because most of Japanese characters consume 3 byte per each character.
|
|
457
467
|
|
|
458
468
|
```javascript
|
|
459
|
-
switchbot
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
})
|
|
469
|
+
switchbot
|
|
470
|
+
.discover({ model: "H", quick: true })
|
|
471
|
+
.then((device_list) => {
|
|
472
|
+
return device_list[0].setDeviceName("Bot in kitchen");
|
|
473
|
+
})
|
|
474
|
+
.then(() => {
|
|
475
|
+
console.log("Done.");
|
|
476
|
+
process.exit();
|
|
477
|
+
})
|
|
478
|
+
.catch((error) => {
|
|
479
|
+
console.error(error);
|
|
480
|
+
process.exit();
|
|
481
|
+
});
|
|
468
482
|
```
|
|
469
483
|
|
|
470
484
|
### `connect()` method
|
|
@@ -480,34 +494,42 @@ The code snippet below establishes a connection with the Bot using the `connect(
|
|
|
480
494
|
```javascript
|
|
481
495
|
let device = null;
|
|
482
496
|
|
|
483
|
-
switchbot
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
497
|
+
switchbot
|
|
498
|
+
.discover({ model: "H", quick: true })
|
|
499
|
+
.then((device_list) => {
|
|
500
|
+
device = device_list[0];
|
|
501
|
+
if (!device) {
|
|
502
|
+
console.log("No device was found.");
|
|
503
|
+
process.exit();
|
|
504
|
+
}
|
|
505
|
+
console.log(device.modelName + " (" + device.address + ") was found.");
|
|
506
|
+
console.log("Connecting...");
|
|
507
|
+
return device.connect();
|
|
508
|
+
})
|
|
509
|
+
.then(() => {
|
|
510
|
+
console.log("Putting the arm down...");
|
|
511
|
+
return device.down();
|
|
512
|
+
})
|
|
513
|
+
.then(() => {
|
|
514
|
+
console.log("Waiting for 5 seconds...");
|
|
515
|
+
return switchbot.wait(5000);
|
|
516
|
+
})
|
|
517
|
+
.then(() => {
|
|
518
|
+
console.log("Putting the arm up...");
|
|
519
|
+
return device.up();
|
|
520
|
+
})
|
|
521
|
+
.then(() => {
|
|
522
|
+
console.log("Disconnecting...");
|
|
523
|
+
return device.disconnect();
|
|
524
|
+
})
|
|
525
|
+
.then(() => {
|
|
526
|
+
console.log("Done.");
|
|
487
527
|
process.exit();
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
})
|
|
493
|
-
console.log('Putting the arm down...');
|
|
494
|
-
return device.down();
|
|
495
|
-
}).then(() => {
|
|
496
|
-
console.log('Waiting for 5 seconds...');
|
|
497
|
-
return switchbot.wait(5000);
|
|
498
|
-
}).then(() => {
|
|
499
|
-
console.log('Putting the arm up...');
|
|
500
|
-
return device.up();
|
|
501
|
-
}).then(() => {
|
|
502
|
-
console.log('Disconnecting...');
|
|
503
|
-
return device.disconnect();
|
|
504
|
-
}).then(() => {
|
|
505
|
-
console.log('Done.');
|
|
506
|
-
process.exit();
|
|
507
|
-
}).catch((error) => {
|
|
508
|
-
console.error(error);
|
|
509
|
-
process.exit();
|
|
510
|
-
});
|
|
528
|
+
})
|
|
529
|
+
.catch((error) => {
|
|
530
|
+
console.error(error);
|
|
531
|
+
process.exit();
|
|
532
|
+
});
|
|
511
533
|
```
|
|
512
534
|
|
|
513
535
|
The result will be as follows:
|
|
@@ -535,31 +557,35 @@ The `onconnect` event handler will be called when the connection with the device
|
|
|
535
557
|
The code below calls the [`press()`](#SwitchbotDeviceWoHand-press-method) method, while callback functions are attached to the `onconnect` and `ondisconnect`.
|
|
536
558
|
|
|
537
559
|
```javascript
|
|
538
|
-
switchbot
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
560
|
+
switchbot
|
|
561
|
+
.discover({ model: "H", quick: true })
|
|
562
|
+
.then((device_list) => {
|
|
563
|
+
let device = device_list[0];
|
|
564
|
+
if (!device) {
|
|
565
|
+
console.log("No device was found.");
|
|
566
|
+
process.exit();
|
|
567
|
+
}
|
|
568
|
+
console.log(device.modelName + " (" + device.address + ") was found.");
|
|
569
|
+
|
|
570
|
+
// Set event handers
|
|
571
|
+
device.onconnect = () => {
|
|
572
|
+
console.log("Connected.");
|
|
573
|
+
};
|
|
574
|
+
device.ondisconnect = () => {
|
|
575
|
+
console.log("Disconnected.");
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
console.log("Pressing the switch...");
|
|
579
|
+
return device.press();
|
|
580
|
+
})
|
|
581
|
+
.then(() => {
|
|
582
|
+
console.log("Done.");
|
|
542
583
|
process.exit();
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
console.log('Connected.');
|
|
549
|
-
};
|
|
550
|
-
device.ondisconnect = () => {
|
|
551
|
-
console.log('Disconnected.');
|
|
552
|
-
};
|
|
553
|
-
|
|
554
|
-
console.log('Pressing the switch...');
|
|
555
|
-
return device.press();
|
|
556
|
-
}).then(() => {
|
|
557
|
-
console.log('Done.');
|
|
558
|
-
process.exit();
|
|
559
|
-
}).catch((error) => {
|
|
560
|
-
console.error(error);
|
|
561
|
-
process.exit();
|
|
562
|
-
});
|
|
584
|
+
})
|
|
585
|
+
.catch((error) => {
|
|
586
|
+
console.error(error);
|
|
587
|
+
process.exit();
|
|
588
|
+
});
|
|
563
589
|
```
|
|
564
590
|
|
|
565
591
|
The code above will output the result as follows:
|
|
@@ -578,7 +604,8 @@ Seeing the result, you would find the [`press()`](#SwitchbotDeviceWoHand-press-m
|
|
|
578
604
|
|
|
579
605
|
The `ondisconnect` event handler will be called when the connection with the device is closed. Nothing will be passed to the handler. See the previous section "[`onconnect` event handler](#SwitchbotDevice-onconnect-event-handler)" for more details.
|
|
580
606
|
|
|
581
|
-
|
|
607
|
+
---
|
|
608
|
+
|
|
582
609
|
## `SwitchbotDeviceWoHand` object
|
|
583
610
|
|
|
584
611
|
The `SwitchbotDeviceWoHand` object represents a Bot, which is created through the discovery process triggered by the [`Switchbot.discover()`](#Switchbot-discover-method) method.
|
|
@@ -592,13 +619,17 @@ The `press()` method sends a press command to the Bot. This method returns a `Pr
|
|
|
592
619
|
If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
|
|
593
620
|
|
|
594
621
|
```javascript
|
|
595
|
-
switchbot
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
})
|
|
600
|
-
|
|
601
|
-
|
|
622
|
+
switchbot
|
|
623
|
+
.discover({ model: "H", quick: true })
|
|
624
|
+
.then((device_list) => {
|
|
625
|
+
return device_list[0].press();
|
|
626
|
+
})
|
|
627
|
+
.then(() => {
|
|
628
|
+
console.log("Done.");
|
|
629
|
+
})
|
|
630
|
+
.catch((error) => {
|
|
631
|
+
console.error(error);
|
|
632
|
+
});
|
|
602
633
|
```
|
|
603
634
|
|
|
604
635
|
When the Bot receives this command, the Bot's arm will be put down (stretched), then put up (retracted) in a few seconds.
|
|
@@ -611,20 +642,24 @@ If no connection is established with the device, this method automatically estab
|
|
|
611
642
|
|
|
612
643
|
When the Bot receives this command, the Bot's arm will be put down (stretched) or put up (retracted) depending on the mode setting.
|
|
613
644
|
|
|
614
|
-
Mode
|
|
615
|
-
|
|
616
|
-
Press mode
|
|
617
|
-
Switch mode
|
|
618
|
-
|
|
645
|
+
| Mode | Inverse the on/off direction | Physical position of the arm |
|
|
646
|
+
| :---------- | :--------------------------- | :------------------------------------ |
|
|
647
|
+
| Press mode | N/A | Down (stretched), then Up (retracted) |
|
|
648
|
+
| Switch mode | Disabled | Down (stretched) |
|
|
649
|
+
| | Enabled | Up (retracted) |
|
|
619
650
|
|
|
620
651
|
```javascript
|
|
621
|
-
switchbot
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
})
|
|
626
|
-
|
|
627
|
-
|
|
652
|
+
switchbot
|
|
653
|
+
.discover({ model: "H", quick: true })
|
|
654
|
+
.then((device_list) => {
|
|
655
|
+
return device_list[0].turnOn();
|
|
656
|
+
})
|
|
657
|
+
.then(() => {
|
|
658
|
+
console.log("Done.");
|
|
659
|
+
})
|
|
660
|
+
.catch((error) => {
|
|
661
|
+
console.error(error);
|
|
662
|
+
});
|
|
628
663
|
```
|
|
629
664
|
|
|
630
665
|
### `turnOff()` method
|
|
@@ -635,20 +670,24 @@ If no connection is established with the device, this method automatically estab
|
|
|
635
670
|
|
|
636
671
|
When the Bot receives this command, the Bot's arm will be put down (stretched) or put up (retracted) depending on the mode setting.
|
|
637
672
|
|
|
638
|
-
Mode
|
|
639
|
-
|
|
640
|
-
Press mode
|
|
641
|
-
Switch mode
|
|
642
|
-
|
|
673
|
+
| Mode | Inverse the on/off direction | Physical position of the arm |
|
|
674
|
+
| :---------- | :--------------------------- | :------------------------------------ |
|
|
675
|
+
| Press mode | N/A | Down (stretched), then Up (retracted) |
|
|
676
|
+
| Switch mode | Disabled | Up (retracted) |
|
|
677
|
+
| | Enabled | Down (stretched) |
|
|
643
678
|
|
|
644
679
|
```javascript
|
|
645
|
-
switchbot
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
})
|
|
650
|
-
|
|
651
|
-
|
|
680
|
+
switchbot
|
|
681
|
+
.discover({ model: "H", quick: true })
|
|
682
|
+
.then((device_list) => {
|
|
683
|
+
return device_list[0].turnOff();
|
|
684
|
+
})
|
|
685
|
+
.then(() => {
|
|
686
|
+
console.log("Done.");
|
|
687
|
+
})
|
|
688
|
+
.catch((error) => {
|
|
689
|
+
console.error(error);
|
|
690
|
+
});
|
|
652
691
|
```
|
|
653
692
|
|
|
654
693
|
### `down()` method
|
|
@@ -660,13 +699,17 @@ If no connection is established with the device, this method automatically estab
|
|
|
660
699
|
When the Bot receives this command, the Bot's arm will be put down (stretched) regardless of the mode setting.
|
|
661
700
|
|
|
662
701
|
```javascript
|
|
663
|
-
switchbot
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
})
|
|
668
|
-
|
|
669
|
-
|
|
702
|
+
switchbot
|
|
703
|
+
.discover({ model: "H", quick: true })
|
|
704
|
+
.then((device_list) => {
|
|
705
|
+
return device_list[0].down();
|
|
706
|
+
})
|
|
707
|
+
.then(() => {
|
|
708
|
+
console.log("Done.");
|
|
709
|
+
})
|
|
710
|
+
.catch((error) => {
|
|
711
|
+
console.error(error);
|
|
712
|
+
});
|
|
670
713
|
```
|
|
671
714
|
|
|
672
715
|
### `up()` method
|
|
@@ -678,16 +721,21 @@ If no connection is established with the device, this method automatically estab
|
|
|
678
721
|
When the Bot receives this command, the Bot's arm will be put up (retracted) regardless of the mode setting.
|
|
679
722
|
|
|
680
723
|
```javascript
|
|
681
|
-
switchbot
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
})
|
|
686
|
-
|
|
687
|
-
|
|
724
|
+
switchbot
|
|
725
|
+
.discover({ model: "H", quick: true })
|
|
726
|
+
.then((device_list) => {
|
|
727
|
+
return device_list[0].up();
|
|
728
|
+
})
|
|
729
|
+
.then(() => {
|
|
730
|
+
console.log("Done.");
|
|
731
|
+
})
|
|
732
|
+
.catch((error) => {
|
|
733
|
+
console.error(error);
|
|
734
|
+
});
|
|
688
735
|
```
|
|
689
736
|
|
|
690
|
-
|
|
737
|
+
---
|
|
738
|
+
|
|
691
739
|
## `SwitchbotDeviceWoCurtain` object
|
|
692
740
|
|
|
693
741
|
The `SwitchbotDeviceWoCurtain` object represents a Curtain, which is created through the discovery process triggered by the [`Switchbot.discover()`](#Switchbot-discover-method) method.
|
|
@@ -697,13 +745,17 @@ Actually, the `SwitchbotDeviceWoCurtain` is an object inherited from the [`Switc
|
|
|
697
745
|
### `open()` method
|
|
698
746
|
|
|
699
747
|
```javascript
|
|
700
|
-
switchbot
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
})
|
|
705
|
-
|
|
706
|
-
|
|
748
|
+
switchbot
|
|
749
|
+
.discover({ model: "c", quick: true })
|
|
750
|
+
.then((device_list) => {
|
|
751
|
+
return device_list[0].open();
|
|
752
|
+
})
|
|
753
|
+
.then(() => {
|
|
754
|
+
console.log("Done.");
|
|
755
|
+
})
|
|
756
|
+
.catch((error) => {
|
|
757
|
+
console.error(error);
|
|
758
|
+
});
|
|
707
759
|
```
|
|
708
760
|
|
|
709
761
|
### `close()` method
|
|
@@ -715,13 +767,17 @@ If no connection is established with the device, this method automatically estab
|
|
|
715
767
|
When the Curtain receives this command, the Curtain will close the curtain (100% position). If not calibrated, the Curtain does not move.
|
|
716
768
|
|
|
717
769
|
```javascript
|
|
718
|
-
switchbot
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
})
|
|
723
|
-
|
|
724
|
-
|
|
770
|
+
switchbot
|
|
771
|
+
.discover({ model: "c", quick: true })
|
|
772
|
+
.then((device_list) => {
|
|
773
|
+
return device_list[0].close();
|
|
774
|
+
})
|
|
775
|
+
.then(() => {
|
|
776
|
+
console.log("Done.");
|
|
777
|
+
})
|
|
778
|
+
.catch((error) => {
|
|
779
|
+
console.error(error);
|
|
780
|
+
});
|
|
725
781
|
```
|
|
726
782
|
|
|
727
783
|
### `pause()` method
|
|
@@ -733,13 +789,17 @@ If no connection is established with the device, this method automatically estab
|
|
|
733
789
|
When the Curtain receives this command, the Curtain will pause.
|
|
734
790
|
|
|
735
791
|
```javascript
|
|
736
|
-
switchbot
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
})
|
|
741
|
-
|
|
742
|
-
|
|
792
|
+
switchbot
|
|
793
|
+
.discover({ model: "c", quick: true })
|
|
794
|
+
.then((device_list) => {
|
|
795
|
+
return device_list[0].pause();
|
|
796
|
+
})
|
|
797
|
+
.then(() => {
|
|
798
|
+
console.log("Done.");
|
|
799
|
+
})
|
|
800
|
+
.catch((error) => {
|
|
801
|
+
console.error(error);
|
|
802
|
+
});
|
|
743
803
|
```
|
|
744
804
|
|
|
745
805
|
### `runToPos()` method
|
|
@@ -750,39 +810,43 @@ If no connection is established with the device, this method automatically estab
|
|
|
750
810
|
|
|
751
811
|
When the Curtain receives this command, the Curtain will run to the percentage position. If not calibrated, the Curtain does not move.
|
|
752
812
|
|
|
753
|
-
|
|
754
813
|
The `open()` method sends an open command to the Curtain. This method returns a `Promise` object. Nothing will be passed to the `resove()`.
|
|
755
814
|
|
|
756
815
|
If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
|
|
757
816
|
|
|
758
817
|
When the Curtain receives this command, the Curtain will open the curtain (0% position). If not calibrated, the Curtain does not move.
|
|
759
818
|
|
|
760
|
-
Property
|
|
761
|
-
|
|
762
|
-
`percent`
|
|
763
|
-
`mode`
|
|
819
|
+
| Property | Type | Required | Description |
|
|
820
|
+
| :-------- | :------ | :------- | :--------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
821
|
+
| `percent` | Integer | Required | The percentage of target position (`0-100`). (e.g., `50`) |
|
|
822
|
+
| `mode` | Integer | Optional | The running mode of Curtain. <br/>`0x00` - Performance mode.<br/> `0x01` - Silent mode. <br/>`0xff` - Default. Unspecified, from Curtain's settings. |
|
|
764
823
|
|
|
765
824
|
```javascript
|
|
766
|
-
switchbot
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
})
|
|
771
|
-
|
|
772
|
-
|
|
825
|
+
switchbot
|
|
826
|
+
.discover({ model: "c", quick: true })
|
|
827
|
+
.then((device_list) => {
|
|
828
|
+
return device_list[0].runToPos(50);
|
|
829
|
+
})
|
|
830
|
+
.then(() => {
|
|
831
|
+
console.log("Done.");
|
|
832
|
+
})
|
|
833
|
+
.catch((error) => {
|
|
834
|
+
console.error(error);
|
|
835
|
+
});
|
|
773
836
|
```
|
|
774
837
|
|
|
775
|
-
|
|
838
|
+
---
|
|
839
|
+
|
|
776
840
|
## Advertisement data
|
|
777
841
|
|
|
778
842
|
After the [`startScan()`](#startscan-method) method is invoked, the [`onadvertisement`](#Switchbot-onadvertisement-event-handler) event handler will be called whenever an advertising packet comes from the switchbot devices. An object containing the properties as follows will be passed to the event handler:
|
|
779
843
|
|
|
780
|
-
Property | Type | Description
|
|
781
|
-
|
|
782
|
-
`id` | String | ID of the device. (e.g., `"cb4eb903c96d"`)
|
|
783
|
-
`address` | String | MAC address of the device. Basically it is as same as the value of the `id` except that this value includes `:` in the string. (e.g., `"cb:4e:b9:03:c9:6d"`)
|
|
784
|
-
`rssi` | Integer | RSSI. (e.g., `-62`)
|
|
785
|
-
`serviceData` | Object | An object including the device-specific data.
|
|
844
|
+
| Property | Type | Description |
|
|
845
|
+
| :------------ | :------ | :----------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
846
|
+
| `id` | String | ID of the device. (e.g., `"cb4eb903c96d"`) |
|
|
847
|
+
| `address` | String | MAC address of the device. Basically it is as same as the value of the `id` except that this value includes `:` in the string. (e.g., `"cb:4e:b9:03:c9:6d"`) |
|
|
848
|
+
| `rssi` | Integer | RSSI. (e.g., `-62`) |
|
|
849
|
+
| `serviceData` | Object | An object including the device-specific data. |
|
|
786
850
|
|
|
787
851
|
The structures of the `serviceData` are described in the following sections.
|
|
788
852
|
|
|
@@ -807,27 +871,26 @@ Example of the advertisement data:
|
|
|
807
871
|
|
|
808
872
|
Structure of the `serviceData`:
|
|
809
873
|
|
|
810
|
-
Property | Type | Description
|
|
811
|
-
|
|
812
|
-
`model` | String | This value is always `"H"`, which means "Bot (WoHand)".
|
|
813
|
-
`modelName` | String | This value is always `"WoHand"`, which means "Bot".
|
|
814
|
-
`mode` | Boolean | This indicates the mode setting. When the mode is "Switch mode", this value is `true`. When the mode is "Press mode", this value is `false`.
|
|
815
|
-
`state` | Boolean | This value indicates whether the switch status is ON or OFF.
|
|
816
|
-
`battery` | Integer | (**experimental**) This value indicates the battery level (`%`).
|
|
874
|
+
| Property | Type | Description |
|
|
875
|
+
| :---------- | :------ | :------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
876
|
+
| `model` | String | This value is always `"H"`, which means "Bot (WoHand)". |
|
|
877
|
+
| `modelName` | String | This value is always `"WoHand"`, which means "Bot". |
|
|
878
|
+
| `mode` | Boolean | This indicates the mode setting. When the mode is "Switch mode", this value is `true`. When the mode is "Press mode", this value is `false`. |
|
|
879
|
+
| `state` | Boolean | This value indicates whether the switch status is ON or OFF. |
|
|
880
|
+
| `battery` | Integer | (**experimental**) This value indicates the battery level (`%`). |
|
|
817
881
|
|
|
818
882
|
The `mode` can be changed only using the official smartphone app. The node-switchbot does not support changing the mode because the BLE protocol is non-public.
|
|
819
883
|
|
|
820
|
-
If the `mode` is `false`, which means the "Press mode" is selected, the `state` is always `false`. If the `mode` is `true`, which means the "Switch mode" is selected, the `state` represents the logical state (ON or OFF). Note that it does
|
|
821
|
-
|
|
822
|
-
"Inverse the on/off direction" | Value of the `state` | Logical state | Physical arm position
|
|
823
|
-
:------------------------------|:---------------------|:--------------|:------------
|
|
824
|
-
disabled | `true` | OFF | Up (retracted)
|
|
825
|
-
| `false` | ON | Down (stretched)
|
|
826
|
-
enabled | `true` | OFF | Down (stretched)
|
|
827
|
-
| `false` | ON | Up (retracted)
|
|
884
|
+
If the `mode` is `false`, which means the "Press mode" is selected, the `state` is always `false`. If the `mode` is `true`, which means the "Switch mode" is selected, the `state` represents the logical state (ON or OFF). Note that it does _not_ mean the physical arm position. The physical arm position depends on the setting "Inverse the on/off direction" on the official smartphone app.
|
|
828
885
|
|
|
829
|
-
|
|
886
|
+
| "Inverse the on/off direction" | Value of the `state` | Logical state | Physical arm position |
|
|
887
|
+
| :----------------------------- | :------------------- | :------------ | :-------------------- |
|
|
888
|
+
| disabled | `true` | OFF | Up (retracted) |
|
|
889
|
+
| | `false` | ON | Down (stretched) |
|
|
890
|
+
| enabled | `true` | OFF | Down (stretched) |
|
|
891
|
+
| | `false` | ON | Up (retracted) |
|
|
830
892
|
|
|
893
|
+
The `battery` is _experimental_ for now. I'm not sure whether the value is correct or not. Never trust this value for now.
|
|
831
894
|
|
|
832
895
|
### Meter (WoSensorTH)
|
|
833
896
|
|
|
@@ -854,20 +917,20 @@ Example of the advertisement data:
|
|
|
854
917
|
|
|
855
918
|
Structure of the `data`:
|
|
856
919
|
|
|
857
|
-
Property
|
|
858
|
-
|
|
859
|
-
`model`
|
|
860
|
-
`modelName`
|
|
861
|
-
`temperature`
|
|
862
|
-
`c`
|
|
863
|
-
`f`
|
|
864
|
-
`fahrenheit`
|
|
865
|
-
`humidity`
|
|
866
|
-
`battery`
|
|
920
|
+
| Property | Type | Description |
|
|
921
|
+
| :--------------- | :------ | :----------------------------------------------------------------------------------------------------------- |
|
|
922
|
+
| `model` | String | This value is always `"T"`, which means "Meter (WoSensorTH)". |
|
|
923
|
+
| `modelName` | String | This value is always `"WoSensorTH"`, which means "Meter". |
|
|
924
|
+
| `temperature` | Object |
|
|
925
|
+
| `c` | Float | Temperature (degree Celsius/°C) |
|
|
926
|
+
| `f` | Float | Temperature (degree Fahrenheit/℉) |
|
|
927
|
+
| `fahrenheit` | Boolean | The flag whether the Meter shows Fahrenheit (`true`) or Celsius (`false`) for the temperature on the display |
|
|
928
|
+
| `humidity` | Integer | Humidity (`%`) |
|
|
929
|
+
| `battery` | Integer | (**experimental**) This value indicates the battery level (`%`). |
|
|
867
930
|
|
|
868
|
-
The `fahrenheit` indicates the setting on the device. Note that it does
|
|
931
|
+
The `fahrenheit` indicates the setting on the device. Note that it does _not_ indicate the setting on the official smartphone app. The setting of the temperature unit on the device and the setting on the app are independent.
|
|
869
932
|
|
|
870
|
-
The `battery` is
|
|
933
|
+
The `battery` is _experimental_ for now. I'm not sure whether the value is correct or not. Never trust this value for now.
|
|
871
934
|
|
|
872
935
|
### Curtain (WoCurtain)
|
|
873
936
|
|
|
@@ -891,14 +954,14 @@ Example of the advertisement data:
|
|
|
891
954
|
|
|
892
955
|
Structure of the `serviceData`:
|
|
893
956
|
|
|
894
|
-
Property | Type | Description
|
|
895
|
-
|
|
896
|
-
`model` | String | This value is always `"c"`, which means "Curtain (WoCurtain)".
|
|
897
|
-
`modelName` | String | This value is always `"WoCurtain"`, which means "Curtain".
|
|
898
|
-
`calibration` | Boolean | This value indicates the calibration status (`true` or `false`).
|
|
899
|
-
`battery` | Integer | This value indicates the battery level (`1-100`, `%`).
|
|
900
|
-
`position` | Integer | This value indicates the percentage of current position (`0-100`, 0 is open, `%`).
|
|
901
|
-
`lightLevel` | Integer | This value indicates the light level of the light source currently set (`1-10`).
|
|
957
|
+
| Property | Type | Description |
|
|
958
|
+
| :------------ | :------ | :--------------------------------------------------------------------------------- |
|
|
959
|
+
| `model` | String | This value is always `"c"`, which means "Curtain (WoCurtain)". |
|
|
960
|
+
| `modelName` | String | This value is always `"WoCurtain"`, which means "Curtain". |
|
|
961
|
+
| `calibration` | Boolean | This value indicates the calibration status (`true` or `false`). |
|
|
962
|
+
| `battery` | Integer | This value indicates the battery level (`1-100`, `%`). |
|
|
963
|
+
| `position` | Integer | This value indicates the percentage of current position (`0-100`, 0 is open, `%`). |
|
|
964
|
+
| `lightLevel` | Integer | This value indicates the light level of the light source currently set (`1-10`). |
|
|
902
965
|
|
|
903
966
|
### Contact (WoContact)
|
|
904
967
|
|
|
@@ -922,14 +985,14 @@ Example of the advertisement data:
|
|
|
922
985
|
|
|
923
986
|
Structure of the `serviceData`:
|
|
924
987
|
|
|
925
|
-
Property
|
|
926
|
-
|
|
927
|
-
`model`
|
|
928
|
-
`modelName`
|
|
929
|
-
`movement`
|
|
930
|
-
`battery`
|
|
931
|
-
`doorState`
|
|
932
|
-
`lightLevel`
|
|
988
|
+
| Property | Type | Description |
|
|
989
|
+
| :----------- | :------ | :--------------------------------------------------------------------------- |
|
|
990
|
+
| `model` | String | This value is always `"c"`, which means "Contact (WoContact)". |
|
|
991
|
+
| `modelName` | String | This value is always `"WoContact"`, which means "Contact". |
|
|
992
|
+
| `movement` | Boolean | This value indicates the motion status (`true` or `false`). |
|
|
993
|
+
| `battery` | Integer | This value indicates the battery level (`1-100`, `%`). |
|
|
994
|
+
| `doorState` | String | This value indicates the door Status (`close`, `open`, `timeout no closed`). |
|
|
995
|
+
| `lightLevel` | String | This value indicates the light level (`dark`, `bright`). |
|
|
933
996
|
|
|
934
997
|
### Motion (WoMotion)
|
|
935
998
|
|
|
@@ -952,16 +1015,17 @@ Example of the advertisement data:
|
|
|
952
1015
|
|
|
953
1016
|
Structure of the `serviceData`:
|
|
954
1017
|
|
|
955
|
-
Property
|
|
956
|
-
|
|
957
|
-
`model`
|
|
958
|
-
`modelName`
|
|
959
|
-
`movement`
|
|
960
|
-
`battery`
|
|
961
|
-
`lightLevel`
|
|
1018
|
+
| Property | Type | Description |
|
|
1019
|
+
| :----------- | :------ | :----------------------------------------------------------- |
|
|
1020
|
+
| `model` | String | This value is always `"s"`, which means "Motion (WoMotion)". |
|
|
1021
|
+
| `modelName` | String | This value is always `"WoMotion"`, which means "Motion". |
|
|
1022
|
+
| `movement` | Boolean | This value indicates the motion status (`true` or `false`). |
|
|
1023
|
+
| `battery` | Integer | This value indicates the battery level (`1-100`, `%`). |
|
|
1024
|
+
| `lightLevel` | String | This value indicates the light level (`dark`, `bright`). |
|
|
1025
|
+
|
|
1026
|
+
---
|
|
962
1027
|
|
|
963
|
-
---------------------------------------
|
|
964
1028
|
## References
|
|
965
1029
|
|
|
966
|
-
|
|
967
|
-
|
|
1030
|
+
- [Switchbot official global site](https://www.switch-bot.com/)
|
|
1031
|
+
- [GitHub - OpenWonderLabs/SwitchBotAPI-BLE](https://github.com/OpenWonderLabs/SwitchBotAPI-BLE)
|