@switchbot/homebridge-switchbot 1.6.1 → 1.6.2
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 +10 -0
- package/dist/devices/bots.d.ts +1 -0
- package/dist/devices/bots.d.ts.map +1 -1
- package/dist/devices/bots.js +130 -114
- package/dist/devices/bots.js.map +1 -1
- package/dist/devices/contact.d.ts +1 -0
- package/dist/devices/contact.d.ts.map +1 -1
- package/dist/devices/contact.js +65 -55
- package/dist/devices/contact.js.map +1 -1
- package/dist/devices/curtains.d.ts +2 -1
- package/dist/devices/curtains.d.ts.map +1 -1
- package/dist/devices/curtains.js +95 -75
- package/dist/devices/curtains.js.map +1 -1
- package/dist/devices/humidifiers.d.ts +2 -1
- package/dist/devices/humidifiers.d.ts.map +1 -1
- package/dist/devices/humidifiers.js +90 -74
- package/dist/devices/humidifiers.js.map +1 -1
- package/dist/devices/meters.d.ts +1 -0
- package/dist/devices/meters.d.ts.map +1 -1
- package/dist/devices/meters.js +68 -62
- package/dist/devices/meters.js.map +1 -1
- package/dist/devices/motion.d.ts +1 -0
- package/dist/devices/motion.d.ts.map +1 -1
- package/dist/devices/motion.js +60 -54
- package/dist/devices/motion.js.map +1 -1
- package/dist/devices/plugs.js +2 -2
- package/dist/devices/plugs.js.map +1 -1
- package/dist/platform.d.ts +2 -0
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +12 -0
- package/dist/platform.js.map +1 -1
- package/package.json +4 -4
- package/src/devices/bots.ts +109 -92
- package/src/devices/contact.ts +65 -52
- package/src/devices/curtains.ts +94 -72
- package/src/devices/humidifiers.ts +89 -71
- package/src/devices/meters.ts +69 -59
- package/src/devices/motion.ts +60 -51
- package/src/devices/plugs.ts +2 -2
- package/src/platform.ts +13 -0
package/src/devices/meters.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import Switchbot from 'node-switchbot';
|
|
2
1
|
import { interval, Subject } from 'rxjs';
|
|
3
2
|
import { skipWhile } from 'rxjs/operators';
|
|
4
3
|
import { SwitchBotPlatform } from '../platform';
|
|
@@ -200,12 +199,12 @@ export class Meter {
|
|
|
200
199
|
if (this.device.scanDuration) {
|
|
201
200
|
this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
|
|
202
201
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
203
|
-
this.warnLog(`
|
|
202
|
+
this.warnLog(`Meter: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
204
203
|
}
|
|
205
204
|
} else {
|
|
206
205
|
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
207
206
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
208
|
-
this.warnLog(`
|
|
207
|
+
this.warnLog(`Meter: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
209
208
|
}
|
|
210
209
|
}
|
|
211
210
|
}
|
|
@@ -305,77 +304,88 @@ export class Meter {
|
|
|
305
304
|
}
|
|
306
305
|
|
|
307
306
|
private connectBLE() {
|
|
308
|
-
|
|
307
|
+
// Convert to BLE Address
|
|
309
308
|
this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
|
|
310
309
|
this.debugLog(`Meter: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
310
|
+
const switchbot = this.platform.connectBLE();
|
|
311
311
|
return switchbot;
|
|
312
312
|
}
|
|
313
313
|
|
|
314
|
-
|
|
314
|
+
|
|
315
|
+
private async BLErefreshStatus() {
|
|
315
316
|
this.debugLog(`Meter: ${this.accessory.displayName} BLE RefreshStatus`);
|
|
316
317
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
317
318
|
const switchbot = this.connectBLE();
|
|
318
319
|
// Start to monitor advertisement packets
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
320
|
+
this.debugLog(`Meter: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
|
|
321
|
+
if (switchbot) {
|
|
322
|
+
switchbot.startScan({
|
|
323
|
+
model: 'T',
|
|
324
|
+
id: this.device.bleMac,
|
|
325
|
+
}).then(() => {
|
|
326
|
+
// Set an event hander
|
|
327
|
+
switchbot.onadvertisement = (ad: ad) => {
|
|
328
|
+
this.serviceData = ad.serviceData;
|
|
329
|
+
this.temperature = ad.serviceData.temperature!.c;
|
|
330
|
+
this.humidity = ad.serviceData.humidity;
|
|
331
|
+
this.battery = ad.serviceData.battery;
|
|
332
|
+
this.debugLog(`Meter: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
333
|
+
this.debugLog(`Meter: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, `
|
|
334
|
+
+ `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, `
|
|
335
|
+
+ `battery: ${ad.serviceData.battery}`);
|
|
336
|
+
|
|
337
|
+
if (this.serviceData) {
|
|
338
|
+
this.connected = true;
|
|
339
|
+
this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
340
|
+
} else {
|
|
341
|
+
this.connected = false;
|
|
342
|
+
this.debugLog(`Meter: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
// Wait 10 seconds
|
|
346
|
+
return switchbot.wait(this.scanDuration * 1000);
|
|
347
|
+
}).then(async () => {
|
|
348
|
+
// Stop to monitor
|
|
349
|
+
switchbot.stopScan();
|
|
350
|
+
if (this.connected) {
|
|
351
|
+
this.CurrentTemperatureCached = this.temperature;
|
|
352
|
+
this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
|
|
353
|
+
this.CurrentRelativeHumidityCached = this.humidity!;
|
|
354
|
+
this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
|
|
355
|
+
this.BatteryLevelCached = this.battery!;
|
|
356
|
+
this.accessory.context.BatteryLevel = this.BatteryLevelCached;
|
|
357
|
+
this.parseStatus();
|
|
358
|
+
this.updateHomeKitCharacteristics();
|
|
337
359
|
} else {
|
|
338
|
-
this.
|
|
339
|
-
|
|
360
|
+
await this.BLEconnection();
|
|
361
|
+
}
|
|
362
|
+
}).catch(async (e: any) => {
|
|
363
|
+
this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
364
|
+
if (this.deviceLogging === 'debug') {
|
|
365
|
+
this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
366
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
367
|
+
}
|
|
368
|
+
if (this.platform.debugMode) {
|
|
369
|
+
this.errorLog(`Meter: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
370
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
340
371
|
}
|
|
341
|
-
};
|
|
342
|
-
// Wait 10 seconds
|
|
343
|
-
return switchbot.wait(this.scanDuration * 1000);
|
|
344
|
-
}).then(async () => {
|
|
345
|
-
// Stop to monitor
|
|
346
|
-
switchbot.stopScan();
|
|
347
|
-
if (this.connected) {
|
|
348
|
-
this.CurrentTemperatureCached = this.temperature;
|
|
349
|
-
this.accessory.context.CurrentTemperature = this.CurrentTemperatureCached;
|
|
350
|
-
this.CurrentRelativeHumidityCached = this.humidity!;
|
|
351
|
-
this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidityCached;
|
|
352
|
-
this.BatteryLevelCached = this.battery!;
|
|
353
|
-
this.accessory.context.BatteryLevel = this.BatteryLevelCached;
|
|
354
|
-
this.parseStatus();
|
|
355
|
-
this.updateHomeKitCharacteristics();
|
|
356
|
-
} else {
|
|
357
|
-
this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
358
372
|
if (this.platform.config.credentials?.openToken) {
|
|
359
373
|
this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
360
374
|
this.openAPIRefreshStatus();
|
|
361
375
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
this.openAPIRefreshStatus();
|
|
376
|
-
}
|
|
377
|
-
this.apiError(e);
|
|
378
|
-
});
|
|
376
|
+
this.apiError(e);
|
|
377
|
+
});
|
|
378
|
+
} else {
|
|
379
|
+
await this.BLEconnection();
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
private async BLEconnection() {
|
|
384
|
+
this.errorLog(`Meter: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
385
|
+
if (this.platform.config.credentials?.openToken) {
|
|
386
|
+
this.warnLog(`Meter: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
387
|
+
await this.openAPIRefreshStatus();
|
|
388
|
+
}
|
|
379
389
|
}
|
|
380
390
|
|
|
381
391
|
private async openAPIRefreshStatus() {
|
package/src/devices/motion.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import Switchbot from 'node-switchbot';
|
|
2
1
|
import { interval, Subject } from 'rxjs';
|
|
3
2
|
import { skipWhile } from 'rxjs/operators';
|
|
4
3
|
import { SwitchBotPlatform } from '../platform';
|
|
@@ -131,12 +130,12 @@ export class Motion {
|
|
|
131
130
|
if (this.device.scanDuration) {
|
|
132
131
|
this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
|
|
133
132
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
134
|
-
this.warnLog(`
|
|
133
|
+
this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
135
134
|
}
|
|
136
135
|
} else {
|
|
137
136
|
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
138
137
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
139
|
-
this.warnLog(`
|
|
138
|
+
this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
140
139
|
}
|
|
141
140
|
}
|
|
142
141
|
}
|
|
@@ -228,9 +227,10 @@ export class Motion {
|
|
|
228
227
|
}
|
|
229
228
|
|
|
230
229
|
private connectBLE() {
|
|
231
|
-
|
|
230
|
+
// Convert to BLE Address
|
|
232
231
|
this.device.bleMac = ((this.device.deviceId!.match(/.{1,2}/g))!.join(':')).toLowerCase();
|
|
233
232
|
this.debugLog(`Motion Sensor: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
233
|
+
const switchbot = this.platform.connectBLE();
|
|
234
234
|
return switchbot;
|
|
235
235
|
}
|
|
236
236
|
|
|
@@ -239,59 +239,68 @@ export class Motion {
|
|
|
239
239
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
240
240
|
const switchbot = this.connectBLE();
|
|
241
241
|
// Start to monitor advertisement packets
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
242
|
+
this.debugLog(`Motion Sensor: ${this.accessory.displayName} platform.Switchbot: ${JSON.stringify(switchbot)}`);
|
|
243
|
+
if (switchbot) {
|
|
244
|
+
switchbot.startScan({
|
|
245
|
+
model: 's',
|
|
246
|
+
id: this.device.bleMac,
|
|
247
|
+
}).then(() => {
|
|
248
|
+
// Set an event hander
|
|
249
|
+
switchbot.onadvertisement = (ad: any) => {
|
|
250
|
+
this.serviceData = ad.serviceData;
|
|
251
|
+
this.movement = ad.serviceData.movement;
|
|
252
|
+
this.battery = ad.serviceData.battery;
|
|
253
|
+
this.lightLevel = ad.serviceData.lightLevel;
|
|
254
|
+
this.debugLog(`Motion Sensor: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
255
|
+
this.debugLog(`Motion Sensor: ${this.accessory.displayName} movement: ${ad.serviceData.movement}, lightLevel: `
|
|
256
|
+
+ `${ad.serviceData.lightLevel}, battery: ${ad.serviceData.battery}`);
|
|
257
|
+
|
|
258
|
+
if (this.serviceData) {
|
|
259
|
+
this.connected = true;
|
|
260
|
+
this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
261
|
+
} else {
|
|
262
|
+
this.connected = false;
|
|
263
|
+
this.debugLog(`Motion Sensor: ${this.accessory.displayName} connected: ${this.connected}`);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
// Wait 10 seconds
|
|
267
|
+
return switchbot.wait(this.scanDuration * 1000);
|
|
268
|
+
}).then(async () => {
|
|
269
|
+
// Stop to monitor
|
|
270
|
+
switchbot.stopScan();
|
|
271
|
+
if (this.connected) {
|
|
272
|
+
this.parseStatus();
|
|
273
|
+
this.updateHomeKitCharacteristics();
|
|
259
274
|
} else {
|
|
260
|
-
this.
|
|
261
|
-
|
|
275
|
+
await this.BLEconnection();
|
|
276
|
+
}
|
|
277
|
+
}).catch(async (e: any) => {
|
|
278
|
+
this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
279
|
+
if (this.deviceLogging === 'debug') {
|
|
280
|
+
this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
281
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
282
|
+
}
|
|
283
|
+
if (this.platform.debugMode) {
|
|
284
|
+
this.errorLog(`Motion Sensor: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
|
|
285
|
+
+ ` Error: ${JSON.stringify(e)}`);
|
|
262
286
|
}
|
|
263
|
-
};
|
|
264
|
-
// Wait 10 seconds
|
|
265
|
-
return switchbot.wait(this.scanDuration * 1000);
|
|
266
|
-
}).then(async () => {
|
|
267
|
-
// Stop to monitor
|
|
268
|
-
switchbot.stopScan();
|
|
269
|
-
if (this.connected) {
|
|
270
|
-
this.parseStatus();
|
|
271
|
-
this.updateHomeKitCharacteristics();
|
|
272
|
-
} else {
|
|
273
|
-
this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
274
287
|
if (this.platform.config.credentials?.openToken) {
|
|
275
288
|
this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
276
289
|
await this.openAPIRefreshStatus();
|
|
277
290
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
await this.openAPIRefreshStatus();
|
|
292
|
-
}
|
|
293
|
-
this.apiError(e);
|
|
294
|
-
});
|
|
291
|
+
this.apiError(e);
|
|
292
|
+
});
|
|
293
|
+
} else {
|
|
294
|
+
await this.BLEconnection();
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private async BLEconnection() {
|
|
299
|
+
this.errorLog(`Motion Sensor: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
|
|
300
|
+
if (this.platform.config.credentials?.openToken) {
|
|
301
|
+
this.warnLog(`Motion Sensor: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
302
|
+
await this.openAPIRefreshStatus();
|
|
303
|
+
}
|
|
295
304
|
}
|
|
296
305
|
|
|
297
306
|
private async openAPIRefreshStatus() {
|
package/src/devices/plugs.ts
CHANGED
|
@@ -134,12 +134,12 @@ export class Plug {
|
|
|
134
134
|
if (this.device.scanDuration) {
|
|
135
135
|
this.scanDuration = this.accessory.context.scanDuration = this.device.scanDuration;
|
|
136
136
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
137
|
-
this.warnLog(`
|
|
137
|
+
this.warnLog(`Plug: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
138
138
|
}
|
|
139
139
|
} else {
|
|
140
140
|
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
141
141
|
if (this.platform.debugMode || (this.deviceLogging === 'debug')) {
|
|
142
|
-
this.warnLog(`
|
|
142
|
+
this.warnLog(`Plug: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
}
|
package/src/platform.ts
CHANGED
|
@@ -41,6 +41,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
41
41
|
registeringDevice!: boolean;
|
|
42
42
|
debugMode!: boolean;
|
|
43
43
|
platformLogging?: string;
|
|
44
|
+
switchbot: any;
|
|
44
45
|
|
|
45
46
|
constructor(public readonly log: Logger, public readonly config: SwitchBotPlatformConfig, public readonly api: API) {
|
|
46
47
|
this.logs();
|
|
@@ -192,6 +193,18 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
192
193
|
}
|
|
193
194
|
}
|
|
194
195
|
|
|
196
|
+
connectBLE() {
|
|
197
|
+
try {
|
|
198
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
199
|
+
const Switchbot = require('node-switchbot');
|
|
200
|
+
this.switchbot = new Switchbot();
|
|
201
|
+
} catch (e) {
|
|
202
|
+
this.switchbot = false;
|
|
203
|
+
this.errorLog(`Was 'node-switchbot' found: ${this.switchbot}`);
|
|
204
|
+
}
|
|
205
|
+
return this.switchbot;
|
|
206
|
+
}
|
|
207
|
+
|
|
195
208
|
/**
|
|
196
209
|
* this method discovers the Locations
|
|
197
210
|
*/
|