node-switchbot 1.10.0 → 2.0.0-beta.1

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.
Files changed (93) hide show
  1. package/.gitattributes +2 -0
  2. package/.github/dependabot.yml +17 -0
  3. package/.github/labeler.yml +38 -0
  4. package/.github/npm-version-script.cjs +81 -0
  5. package/.github/workflows/beta-release.yml +31 -0
  6. package/.github/workflows/build.yml +18 -0
  7. package/.github/workflows/changerelease.yml +13 -0
  8. package/.github/workflows/dependabot.yml +17 -0
  9. package/.github/workflows/labeler.yml +16 -0
  10. package/.github/workflows/release-drafter.yml +14 -0
  11. package/.github/workflows/release.yml +24 -0
  12. package/.github/workflows/stale.yml +12 -0
  13. package/CHANGELOG.md +308 -0
  14. package/dist/advertising.d.ts +172 -0
  15. package/dist/advertising.d.ts.map +1 -0
  16. package/dist/advertising.js +620 -0
  17. package/dist/advertising.js.map +1 -0
  18. package/dist/device/woblindtilt.d.ts +9 -0
  19. package/dist/device/woblindtilt.d.ts.map +1 -0
  20. package/dist/device/woblindtilt.js +108 -0
  21. package/dist/device/woblindtilt.js.map +1 -0
  22. package/dist/device/wobulb.d.ts +39 -0
  23. package/dist/device/wobulb.d.ts.map +1 -0
  24. package/dist/device/wobulb.js +156 -0
  25. package/dist/device/wobulb.js.map +1 -0
  26. package/dist/device/wocontact.d.ts +4 -0
  27. package/dist/device/wocontact.d.ts.map +1 -0
  28. package/dist/device/wocontact.js +8 -0
  29. package/dist/device/wocontact.js.map +1 -0
  30. package/dist/device/wocurtain.d.ts +9 -0
  31. package/dist/device/wocurtain.d.ts.map +1 -0
  32. package/dist/device/wocurtain.js +104 -0
  33. package/dist/device/wocurtain.js.map +1 -0
  34. package/dist/device/wohand.d.ts +10 -0
  35. package/dist/device/wohand.d.ts.map +1 -0
  36. package/dist/device/wohand.js +93 -0
  37. package/dist/device/wohand.js.map +1 -0
  38. package/dist/device/wohumi.d.ts +10 -0
  39. package/dist/device/wohumi.d.ts.map +1 -0
  40. package/dist/device/wohumi.js +93 -0
  41. package/dist/device/wohumi.js.map +1 -0
  42. package/dist/device/woiosensorth.d.ts +4 -0
  43. package/dist/device/woiosensorth.d.ts.map +1 -0
  44. package/dist/device/woiosensorth.js +4 -0
  45. package/dist/device/woiosensorth.js.map +1 -0
  46. package/dist/device/woplugmini.d.ts +31 -0
  47. package/dist/device/woplugmini.d.ts.map +1 -0
  48. package/dist/device/woplugmini.js +68 -0
  49. package/dist/device/woplugmini.js.map +1 -0
  50. package/dist/device/wopresence.d.ts +4 -0
  51. package/dist/device/wopresence.d.ts.map +1 -0
  52. package/dist/device/wopresence.js +4 -0
  53. package/dist/device/wopresence.js.map +1 -0
  54. package/dist/device/wosensorth.d.ts +4 -0
  55. package/dist/device/wosensorth.d.ts.map +1 -0
  56. package/dist/device/wosensorth.js +4 -0
  57. package/dist/device/wosensorth.js.map +1 -0
  58. package/dist/device/wostrip.d.ts +39 -0
  59. package/dist/device/wostrip.d.ts.map +1 -0
  60. package/dist/device/wostrip.js +145 -0
  61. package/dist/device/wostrip.js.map +1 -0
  62. package/dist/device.d.ts +46 -0
  63. package/dist/device.d.ts.map +1 -0
  64. package/dist/device.js +498 -0
  65. package/dist/device.js.map +1 -0
  66. package/dist/index.d.ts +2 -0
  67. package/dist/index.d.ts.map +1 -0
  68. package/dist/index.js +6 -0
  69. package/dist/index.js.map +1 -0
  70. package/dist/parameter-checker.d.ts +25 -0
  71. package/dist/parameter-checker.d.ts.map +1 -0
  72. package/dist/parameter-checker.js +488 -0
  73. package/dist/parameter-checker.js.map +1 -0
  74. package/dist/switchbot.d.ts +29 -0
  75. package/dist/switchbot.d.ts.map +1 -0
  76. package/dist/switchbot.js +461 -0
  77. package/dist/switchbot.js.map +1 -0
  78. package/package.json +19 -12
  79. package/lib/parameter-checker.js +0 -524
  80. package/lib/switchbot-advertising.js +0 -713
  81. package/lib/switchbot-device-woblindtilt.js +0 -120
  82. package/lib/switchbot-device-wobulb.js +0 -188
  83. package/lib/switchbot-device-wocontact.js +0 -6
  84. package/lib/switchbot-device-wocurtain.js +0 -117
  85. package/lib/switchbot-device-wohand.js +0 -106
  86. package/lib/switchbot-device-wohumi.js +0 -106
  87. package/lib/switchbot-device-woiosensorth.js +0 -6
  88. package/lib/switchbot-device-woplugmini.js +0 -84
  89. package/lib/switchbot-device-wopresence.js +0 -6
  90. package/lib/switchbot-device-wosensorth.js +0 -6
  91. package/lib/switchbot-device-wostrip.js +0 -182
  92. package/lib/switchbot-device.js +0 -536
  93. package/lib/switchbot.js +0 -496
package/lib/switchbot.js DELETED
@@ -1,496 +0,0 @@
1
- "use strict";
2
- const parameterChecker = require("./parameter-checker.js");
3
- const switchbotAdvertising = require("./switchbot-advertising.js");
4
-
5
- const SwitchbotDevice = require("./switchbot-device.js");
6
- const SwitchbotDeviceWoHand = require("./switchbot-device-wohand.js");
7
- const SwitchbotDeviceWoCurtain = require("./switchbot-device-wocurtain.js");
8
- const SwitchbotDeviceWoBlindTilt = require("./switchbot-device-woblindtilt.js");
9
- const SwitchbotDeviceWoPresence = require("./switchbot-device-wopresence.js");
10
- const SwitchbotDeviceWoContact = require("./switchbot-device-wocontact.js");
11
- const SwitchbotDeviceWoSensorTH = require("./switchbot-device-wosensorth.js");
12
- const SwitchbotDeviceWoIOSensorTH = require("./switchbot-device-woiosensorth.js");
13
- const SwitchbotDeviceWoHumi = require("./switchbot-device-wohumi.js");
14
- const SwitchbotDeviceWoPlugMini = require("./switchbot-device-woplugmini.js");
15
- const SwitchbotDeviceWoBulb = require("./switchbot-device-wobulb.js");
16
- const SwitchbotDeviceWoStrip = require("./switchbot-device-wostrip.js");
17
-
18
- class Switchbot {
19
- /* ------------------------------------------------------------------
20
- * Constructor
21
- *
22
- * [Arguments]
23
- * - params | Object | Optional |
24
- * - noble | Noble | Optional | The Noble object created by the noble module.
25
- * | | | This parameter is optional.
26
- * | | | If you don't specify this parameter, this
27
- * | | | module automatically creates it.
28
- * ---------------------------------------------------------------- */
29
- constructor(params) {
30
- // Check parameters
31
- let noble = null;
32
- if (params && params.noble) {
33
- noble = params.noble;
34
- } else {
35
- noble = require("@abandonware/noble");
36
- }
37
-
38
- // Public properties
39
- this.noble = noble;
40
- this.ondiscover = null;
41
- this.onadvertisement = null;
42
- this.onlog = null;
43
-
44
- // Private properties
45
- this._scanning = false;
46
- this._DEFAULT_DISCOVERY_DURATION = 5000;
47
- this._PRIMARY_SERVICE_UUID_LIST = [];
48
- }
49
-
50
- /* ------------------------------------------------------------------
51
- * discover([params])
52
- * - Discover switchbot devices
53
- *
54
- * [Arguments]
55
- * - params | Object | Optional |
56
- * - duration | Integer | Optional | Duration for discovery process (msec).
57
- * | | | The value must be in the range of 1 to 60000.
58
- * | | | The default value is 5000 (msec).
59
- * - model | String | Optional | "H", "T", "e", "s", "d", "c", "{", "u", "g", "o", "i", or "r".
60
- * | | | If "H" is specified, this method will discover only Bots.
61
- * | | | If "T" is specified, this method will discover only Meters.
62
- * | | | If "e" is specified, this method will discover only Humidifiers.
63
- * | | | If "s" is specified, this method will discover only Motion Sensors.
64
- * | | | If "d" is specified, this method will discover only Contact Sensors.
65
- * | | | If "c" is specified, this method will discover only Curtains.
66
- * | | | If "{" is specified, this method will discover only Curtain 3.
67
- * | | | If "u" is specified, this method will discover only Color Bulbs.
68
- * | | | If "g" is specified, this method will discover only Plugs.
69
- * | | | If "o" is specified, this method will discover only Locks.
70
- * | | | If "i" is specified, this method will discover only Meter Pluses.
71
- * | | | If "r" is specified, this method will discover only Locks.
72
- * - id | String | Optional | If this value is set, this method will discover
73
- * | | | only a device whose ID is as same as this value.
74
- * | | | The ID is identical to the MAC address.
75
- * | | | This parameter is case-insensitive, and
76
- * | | | colons are ignored.
77
- * - quick | Boolean | Optional | If this value is true, this method finishes
78
- * | | | the discovery process when the first device
79
- * | | | is found, then calls the resolve() function
80
- * | | | without waiting the specified duration.
81
- * | | | The default value is false.
82
- *
83
- * [Return value]
84
- * - Promise object
85
- * An array will be passed to the `resolve()`, which includes
86
- * `SwitchbotDevice` objects representing the found devices.
87
- * ---------------------------------------------------------------- */
88
- discover(params = {}) {
89
- const promise = new Promise((resolve, reject) => {
90
- // Check the parameters
91
- const valid = parameterChecker.check(
92
- params,
93
- {
94
- duration: { required: false, type: "integer", min: 1, max: 60000 },
95
- model: {
96
- required: false,
97
- type: "string",
98
- enum: [
99
- "H",
100
- "T",
101
- "e",
102
- "s",
103
- "d",
104
- "c",
105
- "{",
106
- "u",
107
- "g",
108
- "j",
109
- "o",
110
- "i",
111
- "r",
112
- "x",
113
- "w",
114
- ],
115
- },
116
- id: { required: false, type: "string", min: 12, max: 17 },
117
- quick: { required: false, type: "boolean" },
118
- },
119
- false
120
- );
121
-
122
- if (!valid) {
123
- reject(new Error(parameterChecker.error.message));
124
- return;
125
- }
126
-
127
- if (!params) {
128
- params = {};
129
- }
130
-
131
- // Determine the values of the parameters
132
- const p = {
133
- duration: params.duration || this._DEFAULT_DISCOVERY_DURATION,
134
- model: params.model || "",
135
- id: params.id || "",
136
- quick: params.quick ? true : false,
137
- };
138
-
139
- // Initialize the noble object
140
- this._init()
141
- .then(() => {
142
- let peripherals = {};
143
- let timer = null;
144
- const finishDiscovery = () => {
145
- if (timer) {
146
- clearTimeout(timer);
147
- }
148
- this.noble.removeAllListeners("discover");
149
- this.noble.stopScanning();
150
- let device_list = [];
151
- for (let addr in peripherals) {
152
- device_list.push(peripherals[addr]);
153
- }
154
- if (device_list.length) {
155
- resolve(device_list);
156
- }
157
- };
158
-
159
- // Set a handler for the 'discover' event
160
- this.noble.on("discover", (peripheral) => {
161
- const device = this._getDeviceObject(peripheral, p.id, p.model);
162
- if (!device) {
163
- return;
164
- }
165
- const id = device.id;
166
- peripherals[id] = device;
167
-
168
- if (this.ondiscover && typeof this.ondiscover === "function") {
169
- this.ondiscover(device);
170
- }
171
-
172
- if (p.quick) {
173
- finishDiscovery();
174
- return;
175
- }
176
- });
177
-
178
- // Start scanning
179
- this.noble.startScanning(
180
- this._PRIMARY_SERVICE_UUID_LIST,
181
- false,
182
- (error) => {
183
- if (error) {
184
- reject(error);
185
- return;
186
- }
187
- timer = setTimeout(() => {
188
- finishDiscovery();
189
- }, p.duration);
190
- }
191
- );
192
- })
193
- .catch((error) => {
194
- reject(error);
195
- });
196
- });
197
- return promise;
198
- }
199
-
200
- _init() {
201
- const promise = new Promise((resolve, reject) => {
202
- let err;
203
- if (this.noble.state === "poweredOn") {
204
- resolve();
205
- return;
206
- }
207
- this.noble.once('stateChange', state => {
208
- switch (state) {
209
- case ("unsupported", "unauthorized", "poweredOff"):
210
- err = new Error(
211
- "Failed to initialize the Noble object: " + this.noble.state
212
- );
213
- reject(err);
214
- return;
215
- case ("resetting", "unknown"):
216
- err = new Error(
217
- "Adapter is not ready: " + this.noble.state
218
- );
219
- reject(err);
220
- return;
221
- case "poweredOn":
222
- resolve();
223
- return;
224
- default:
225
- err = new Error(
226
- "Uknown state: " + this.noble.state
227
- );
228
- reject(err);
229
- return;
230
- }
231
- });
232
- });
233
- return promise;
234
- }
235
-
236
- _getDeviceObject(peripheral, id, model) {
237
- const ad = switchbotAdvertising.parse(peripheral, this.onlog);
238
- if (this._filterAdvertising(ad, id, model)) {
239
- let device = null;
240
- switch (ad.serviceData.model) {
241
- case "H":
242
- device = new SwitchbotDeviceWoHand(peripheral, this.noble);
243
- break;
244
- case "T":
245
- device = new SwitchbotDeviceWoSensorTH(peripheral, this.noble);
246
- break;
247
- case "e":
248
- device = new SwitchbotDeviceWoHumi(peripheral, this.noble);
249
- break;
250
- case "s":
251
- device = new SwitchbotDeviceWoPresence(peripheral, this.noble);
252
- break;
253
- case "d":
254
- device = new SwitchbotDeviceWoContact(peripheral, this.noble);
255
- break;
256
- case "c":
257
- case "{":
258
- device = new SwitchbotDeviceWoCurtain(peripheral, this.noble);
259
- break;
260
- case "x":
261
- device = new SwitchbotDeviceWoBlindTilt(peripheral, this.noble);
262
- break;
263
- case "u":
264
- device = new SwitchbotDeviceWoBulb(peripheral, this.noble);
265
- break;
266
- case "g":
267
- case "j":
268
- device = new SwitchbotDeviceWoPlugMini(peripheral, this.noble);
269
- break;
270
- case "o":
271
- //device = new SwitchbotDeviceWoSmartLock(peripheral, this.noble);
272
- break;
273
- case "i":
274
- device = new SwitchbotDeviceWoSensorTH(peripheral, this.noble);
275
- break;
276
- case "w":
277
- device = new SwitchbotDeviceWoIOSensorTH(peripheral, this.noble);
278
- break;
279
- case "r":
280
- device = new SwitchbotDeviceWoStrip(peripheral, this.noble);
281
- break;
282
- default: // 'resetting', 'unknown'
283
- device = new SwitchbotDevice(peripheral, this.noble);
284
- }
285
- return device;
286
- } else {
287
- return null;
288
- }
289
- }
290
-
291
- _filterAdvertising(ad, id, model) {
292
- if (!ad) {
293
- return false;
294
- }
295
- if (id) {
296
- id = id.toLowerCase().replace(/:/g, "");
297
- const ad_id = ad.address.toLowerCase().replace(/[^a-z0-9]/g, "");
298
- if (ad_id !== id) {
299
- return false;
300
- }
301
- }
302
- if (model) {
303
- if (ad.serviceData.model !== model) {
304
- return false;
305
- }
306
- }
307
- return true;
308
- }
309
-
310
- /* ------------------------------------------------------------------
311
- * startScan([params])
312
- * - Start to monitor advertising packets coming from switchbot devices
313
- *
314
- * [Arguments]
315
- * - params | Object | Optional |
316
- * - model | String | Optional | "H", "T", "e", "s", "d", "c", "{", "u", "g", "o", "i", "x", or "r".
317
- * | | | If "H" is specified, the `onadvertisement`
318
- * | | | event handler will be called only when advertising
319
- * | | | packets comes from Bots.
320
- * | | | If "T" is specified, the `onadvertisement`
321
- * | | | event handler will be called only when advertising
322
- * | | | packets comes from Meters.
323
- * | | | If "e" is specified, the `onadvertisement`
324
- * | | | event handler will be called only when advertising
325
- * | | | packets comes from Humidifiers.
326
- * | | | If "s" is specified, the `onadvertisement`
327
- * | | | event handler will be called only when advertising
328
- * | | | packets comes from Motion Sensor.
329
- * | | | If "d" is specified, the `onadvertisement`
330
- * | | | event handler will be called only when advertising
331
- * | | | packets comes from Contact Sensor.
332
- * | | | If "c" is specified, the `onadvertisement`
333
- * | | | event handler will be called only when advertising
334
- * | | | packets comes from Curtains.
335
- * | | | If "{" is specified, the `onadvertisement`
336
- * | | | event handler will be called only when advertising
337
- * | | | packets comes from Curtain 3.
338
- * | | | If "x" is specified, the `onadvertisement`
339
- * | | | event handler will be called only when advertising
340
- * | | | packets comes from BlindTilt.
341
- * | | | If "u" is specified, the `onadvertisement`
342
- * | | | event handler will be called only when advertising
343
- * | | | packets comes from Color Bulb.
344
- * | | | If "g" is specified, the `onadvertisement`
345
- * | | | event handler will be called only when advertising
346
- * | | | packets comes from Plug Mini.
347
- * | | | If "o" is specified, the `onadvertisement`
348
- * | | | event handler will be called only when advertising
349
- * | | | packets comes from Smart Lock.
350
- * | | | If "i" is specified, the `onadvertisement`
351
- * | | | event handler will be called only when advertising
352
- * | | | packets comes from Meter Plus.
353
- * | | | If "r" is specified, the `onadvertisement`
354
- * | | | event handler will be called only when advertising
355
- * | | | packets comes from LED Strip Light.
356
- * - id | String | Optional | If this value is set, the `onadvertisement`
357
- * | | | event handler will be called only when advertising
358
- * | | | packets comes from devices whose ID is as same as
359
- * | | | this value.
360
- * | | | The ID is identical to the MAC address.
361
- * | | | This parameter is case-insensitive, and
362
- * | | | colons are ignored.
363
- *
364
- * [Return value]
365
- * - Promise object
366
- * Nothing will be passed to the `resolve()`.
367
- * ---------------------------------------------------------------- */
368
- startScan(params) {
369
- const promise = new Promise((resolve, reject) => {
370
- // Check the parameters
371
- const valid = parameterChecker.check(
372
- params,
373
- {
374
- model: {
375
- required: false,
376
- type: "string",
377
- enum: [
378
- "H",
379
- "T",
380
- "e",
381
- "s",
382
- "d",
383
- "c",
384
- "{",
385
- "u",
386
- "g",
387
- "j",
388
- "o",
389
- "i",
390
- "r",
391
- "x",
392
- "w",
393
- ],
394
- },
395
- id: { required: false, type: "string", min: 12, max: 17 },
396
- },
397
- false
398
- );
399
- if (!valid) {
400
- reject(new Error(parameterChecker.error.message));
401
- return;
402
- }
403
-
404
- if (!params) {
405
- params = {};
406
- }
407
-
408
- // Initialize the noble object
409
- this._init()
410
- .then(() => {
411
- // Determine the values of the parameters
412
- const p = {
413
- model: params.model || "",
414
- id: params.id || "",
415
- };
416
-
417
- // Set a handler for the 'discover' event
418
- this.noble.on("discover", (peripheral) => {
419
- const ad = switchbotAdvertising.parse(peripheral, this.onlog);
420
- if (this._filterAdvertising(ad, p.id, p.model)) {
421
- if (
422
- this.onadvertisement &&
423
- typeof this.onadvertisement === "function"
424
- ) {
425
- this.onadvertisement(ad);
426
- }
427
- }
428
- });
429
-
430
- // Start scanning
431
- this.noble.startScanning(
432
- this._PRIMARY_SERVICE_UUID_LIST,
433
- true,
434
- (error) => {
435
- if (error) {
436
- reject(error);
437
- } else {
438
- resolve();
439
- }
440
- }
441
- );
442
- })
443
- .catch((error) => {
444
- reject(error);
445
- });
446
- });
447
- return promise;
448
- }
449
-
450
- /* ------------------------------------------------------------------
451
- * stopScan()
452
- * - Stop to monitor advertising packets coming from switchbot devices
453
- *
454
- * [Arguments]
455
- * - none
456
- *
457
- * [Return value]
458
- * - none
459
- * ---------------------------------------------------------------- */
460
- stopScan() {
461
- this.noble.removeAllListeners("discover");
462
- this.noble.stopScanning();
463
- }
464
-
465
- /* ------------------------------------------------------------------
466
- * wait(msec) {
467
- * - Wait for the specified time (msec)
468
- *
469
- * [Arguments]
470
- * - msec | Integer | Required | Msec.
471
- *
472
- * [Return value]
473
- * - Promise object
474
- * Nothing will be passed to the `resolve()`.
475
- * ---------------------------------------------------------------- */
476
- wait(msec) {
477
- return new Promise((resolve, reject) => {
478
- // Check the parameters
479
- const valid = parameterChecker.check(
480
- { msec: msec },
481
- {
482
- msec: { required: true, type: "integer", min: 0 },
483
- }
484
- );
485
-
486
- if (!valid) {
487
- reject(new Error(parameterChecker.error.message));
488
- return;
489
- }
490
- // Set a timer
491
- setTimeout(resolve, msec);
492
- });
493
- }
494
- }
495
-
496
- module.exports = Switchbot;