@switchbot/homebridge-switchbot 3.5.0 → 3.5.1-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.
- package/.github/npm-version-script.cjs +81 -0
- package/config.schema.json +4 -4
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +64 -189
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +34 -68
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +1 -1
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +65 -158
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +1 -1
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +47 -159
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +42 -105
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +1 -0
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +61 -182
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +41 -2
- package/dist/device/device.d.ts.map +1 -1
- package/dist/device/device.js +182 -5
- package/dist/device/device.js.map +1 -1
- package/dist/device/fan.d.ts.map +1 -1
- package/dist/device/fan.js +76 -145
- package/dist/device/fan.js.map +1 -1
- package/dist/device/hub.d.ts +7 -1
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +54 -17
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +55 -104
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +27 -46
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +1 -1
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +56 -120
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +1 -1
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +36 -66
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +27 -46
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +26 -45
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +24 -43
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +32 -66
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +1 -1
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +43 -88
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +23 -43
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +10 -28
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +8 -20
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +8 -20
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +8 -20
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +7 -0
- package/dist/irdevice/irdevice.d.ts.map +1 -1
- package/dist/irdevice/irdevice.js +28 -0
- package/dist/irdevice/irdevice.js.map +1 -1
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +8 -20
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +8 -20
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +8 -20
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +8 -20
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +8 -20
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/settings.d.ts +4 -2
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js.map +1 -1
- package/dist/utils.d.ts +19 -1
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +20 -1
- package/dist/utils.js.map +1 -1
- package/package.json +8 -8
- package/src/device/blindtilt.ts +71 -186
- package/src/device/bot.ts +32 -68
- package/src/device/ceilinglight.ts +67 -153
- package/src/device/colorbulb.ts +51 -155
- package/src/device/contact.ts +49 -104
- package/src/device/curtain.ts +70 -177
- package/src/device/device.ts +207 -8
- package/src/device/fan.ts +75 -147
- package/src/device/hub.ts +55 -19
- package/src/device/humidifier.ts +52 -104
- package/src/device/iosensor.ts +25 -46
- package/src/device/lightstrip.ts +54 -121
- package/src/device/lock.ts +35 -67
- package/src/device/meter.ts +25 -46
- package/src/device/meterplus.ts +24 -45
- package/src/device/motion.ts +22 -43
- package/src/device/plug.ts +30 -66
- package/src/device/robotvacuumcleaner.ts +40 -89
- package/src/device/waterdetector.ts +22 -43
- package/src/irdevice/airconditioner.ts +10 -26
- package/src/irdevice/airpurifier.ts +8 -20
- package/src/irdevice/camera.ts +8 -20
- package/src/irdevice/fan.ts +8 -20
- package/src/irdevice/irdevice.ts +29 -0
- package/src/irdevice/light.ts +8 -20
- package/src/irdevice/other.ts +8 -20
- package/src/irdevice/tv.ts +8 -20
- package/src/irdevice/vacuumcleaner.ts +8 -20
- package/src/irdevice/waterheater.ts +8 -20
- package/src/settings.ts +4 -2
- package/src/utils.ts +20 -1
package/src/device/lock.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
/* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
|
|
2
|
+
*
|
|
3
|
+
* lock.ts: @switchbot/homebridge-switchbot.
|
|
4
|
+
*/
|
|
2
5
|
import { deviceBase } from './device.js';
|
|
3
6
|
import { interval, Subject } from 'rxjs';
|
|
4
|
-
import { Devices } from '../settings.js';
|
|
5
7
|
import { debounceTime, skipWhile, take, tap } from 'rxjs/operators';
|
|
6
8
|
|
|
7
9
|
import type { SwitchBotPlatform } from '../platform.js';
|
|
8
10
|
import type { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
|
|
9
|
-
import type { device, devicesConfig,
|
|
11
|
+
import type { device, devicesConfig, serviceData, deviceStatus } from '../settings.js';
|
|
10
12
|
|
|
11
13
|
export class Lock extends deviceBase {
|
|
12
14
|
// Services
|
|
@@ -289,63 +291,43 @@ export class Lock extends deviceBase {
|
|
|
289
291
|
|
|
290
292
|
async BLERefreshStatus(): Promise<void> {
|
|
291
293
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
292
|
-
const switchbot = await this.
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
.
|
|
296
|
-
|
|
297
|
-
.toLowerCase();
|
|
298
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
299
|
-
this.getCustomBLEAddress(switchbot);
|
|
294
|
+
const switchbot = await this.switchbotBLE();
|
|
295
|
+
|
|
296
|
+
if (switchbot === undefined) {
|
|
297
|
+
await this.BLERefreshConnection(switchbot);
|
|
298
|
+
} else {
|
|
300
299
|
// Start to monitor advertisement packets
|
|
301
|
-
|
|
300
|
+
(async () => {
|
|
302
301
|
// Start to monitor advertisement packets
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
|
|
309
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
302
|
+
const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
|
|
303
|
+
// Update HomeKit
|
|
304
|
+
if (serviceData.model !== '' && serviceData.modelName !== '') {
|
|
305
|
+
await this.BLEparseStatus(serviceData);
|
|
306
|
+
await this.updateHomeKitCharacteristics();
|
|
310
307
|
} else {
|
|
311
|
-
this.
|
|
308
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
|
|
309
|
+
await this.BLERefreshConnection(switchbot);
|
|
312
310
|
}
|
|
313
|
-
};
|
|
314
|
-
// Wait 10 seconds
|
|
315
|
-
await switchbot.wait(this.scanDuration * 1000);
|
|
316
|
-
// Stop to monitor
|
|
317
|
-
await switchbot.stopScan();
|
|
318
|
-
// Update HomeKit
|
|
319
|
-
await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
|
|
320
|
-
await this.updateHomeKitCharacteristics();
|
|
321
|
-
})();
|
|
322
|
-
if (switchbot === undefined) {
|
|
323
|
-
await this.BLERefreshConnection(switchbot);
|
|
311
|
+
})();
|
|
324
312
|
}
|
|
325
313
|
}
|
|
326
314
|
|
|
327
315
|
async openAPIRefreshStatus(): Promise<void> {
|
|
328
316
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
329
317
|
try {
|
|
330
|
-
const { body, statusCode } = await this.
|
|
331
|
-
`${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
332
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
318
|
+
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
333
319
|
const deviceStatus: any = await body.json();
|
|
334
|
-
this.
|
|
335
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
320
|
+
await this.refreshStatusCodes(statusCode, deviceStatus);;
|
|
336
321
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
337
|
-
this.
|
|
338
|
-
|
|
339
|
-
this.
|
|
340
|
-
this.updateHomeKitCharacteristics();
|
|
322
|
+
await this.successfulRefreshStatus(statusCode, deviceStatus);
|
|
323
|
+
await this.openAPIparseStatus(deviceStatus);
|
|
324
|
+
await this.updateHomeKitCharacteristics();
|
|
341
325
|
} else {
|
|
342
|
-
this.
|
|
343
|
-
this.statusCode(deviceStatus.statusCode);
|
|
326
|
+
await this.statusCodes(statusCode, deviceStatus);
|
|
344
327
|
}
|
|
345
328
|
} catch (e: any) {
|
|
346
|
-
this.apiError(e);
|
|
347
|
-
this.
|
|
348
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
329
|
+
await this.apiError(e);
|
|
330
|
+
await this.openAPIRefreshError(e);
|
|
349
331
|
}
|
|
350
332
|
}
|
|
351
333
|
|
|
@@ -384,8 +366,7 @@ export class Lock extends deviceBase {
|
|
|
384
366
|
await this.openAPIpushChanges();
|
|
385
367
|
} else {
|
|
386
368
|
await this.offlineOff();
|
|
387
|
-
this.
|
|
388
|
-
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
369
|
+
await this.pushChangeDisabled();
|
|
389
370
|
}
|
|
390
371
|
// Refresh the status from the API
|
|
391
372
|
interval(15000)
|
|
@@ -403,11 +384,7 @@ export class Lock extends deviceBase {
|
|
|
403
384
|
+ ` LockTargetStateCached: ${this.accessory.context.LockTargetState}`);
|
|
404
385
|
const switchbot = await this.platform.connectBLE();
|
|
405
386
|
// Convert to BLE Address
|
|
406
|
-
|
|
407
|
-
.deviceId!.match(/.{1,2}/g)!
|
|
408
|
-
.join(':')
|
|
409
|
-
.toLowerCase();
|
|
410
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
387
|
+
await this.convertBLEAddress();
|
|
411
388
|
switchbot
|
|
412
389
|
.discover({
|
|
413
390
|
model: '',
|
|
@@ -450,24 +427,15 @@ export class Lock extends deviceBase {
|
|
|
450
427
|
});
|
|
451
428
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
452
429
|
try {
|
|
453
|
-
const { body, statusCode } = await
|
|
454
|
-
body: bodyChange,
|
|
455
|
-
method: 'POST',
|
|
456
|
-
headers: this.platform.generateHeaders(),
|
|
457
|
-
});
|
|
458
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
430
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
459
431
|
const deviceStatus: any = await body.json();
|
|
460
|
-
this.
|
|
461
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
462
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
432
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
463
433
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
464
|
-
this.
|
|
465
|
-
|
|
466
|
-
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
467
|
-
+ `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
|
|
434
|
+
await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
|
|
435
|
+
await this.updateHomeKitCharacteristics();
|
|
468
436
|
} else {
|
|
469
|
-
this.statusCode(statusCode);
|
|
470
|
-
this.statusCode(deviceStatus.statusCode);
|
|
437
|
+
await this.statusCode(statusCode);
|
|
438
|
+
await this.statusCode(deviceStatus.statusCode);
|
|
471
439
|
}
|
|
472
440
|
} catch (e: any) {
|
|
473
441
|
this.apiError(e);
|
package/src/device/meter.ts
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
* meter.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
5
|
import { Units } from 'homebridge';
|
|
6
|
-
import { Devices } from '../settings.js';
|
|
7
6
|
import { deviceBase } from './device.js';
|
|
8
7
|
import { convertUnits } from '../utils.js';
|
|
9
8
|
import { Subject, interval, skipWhile } from 'rxjs';
|
|
@@ -166,10 +165,10 @@ export class Meter extends deviceBase {
|
|
|
166
165
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor!.CurrentRelativeHumidity}%`);
|
|
167
166
|
}
|
|
168
167
|
|
|
169
|
-
//
|
|
168
|
+
// Current Temperature
|
|
170
169
|
if (!this.device.meter?.hide_temperature) {
|
|
171
|
-
serviceData.temperature!.c < 0 ? 0 : serviceData.temperature!.c > 100 ? 100 : serviceData.temperature!.c;
|
|
172
|
-
this.TemperatureSensor!.CurrentTemperature =
|
|
170
|
+
const celcius = serviceData.temperature!.c < 0 ? 0 : serviceData.temperature!.c > 100 ? 100 : serviceData.temperature!.c;
|
|
171
|
+
this.TemperatureSensor!.CurrentTemperature = celcius;
|
|
173
172
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor!.CurrentTemperature}°c`);
|
|
174
173
|
}
|
|
175
174
|
}
|
|
@@ -237,63 +236,43 @@ export class Meter extends deviceBase {
|
|
|
237
236
|
|
|
238
237
|
async BLERefreshStatus(): Promise<void> {
|
|
239
238
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
240
|
-
const switchbot = await this.
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
.
|
|
244
|
-
|
|
245
|
-
.toLowerCase();
|
|
246
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
247
|
-
this.getCustomBLEAddress(switchbot);
|
|
239
|
+
const switchbot = await this.switchbotBLE();
|
|
240
|
+
|
|
241
|
+
if (switchbot === undefined) {
|
|
242
|
+
await this.BLERefreshConnection(switchbot);
|
|
243
|
+
} else {
|
|
248
244
|
// Start to monitor advertisement packets
|
|
249
|
-
|
|
245
|
+
(async () => {
|
|
250
246
|
// Start to monitor advertisement packets
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
|
|
257
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
247
|
+
const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
|
|
248
|
+
// Update HomeKit
|
|
249
|
+
if (serviceData.model !== '' && serviceData.modelName !== '') {
|
|
250
|
+
await this.BLEparseStatus(serviceData);
|
|
251
|
+
await this.updateHomeKitCharacteristics();
|
|
258
252
|
} else {
|
|
259
|
-
this.
|
|
253
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
|
|
254
|
+
await this.BLERefreshConnection(switchbot);
|
|
260
255
|
}
|
|
261
|
-
};
|
|
262
|
-
// Wait 10 seconds
|
|
263
|
-
await switchbot.wait(this.scanDuration * 1000);
|
|
264
|
-
// Stop to monitor
|
|
265
|
-
await switchbot.stopScan();
|
|
266
|
-
// Update HomeKit
|
|
267
|
-
await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
|
|
268
|
-
await this.updateHomeKitCharacteristics();
|
|
269
|
-
})();
|
|
270
|
-
if (switchbot === undefined) {
|
|
271
|
-
await this.BLERefreshConnection(switchbot);
|
|
256
|
+
})();
|
|
272
257
|
}
|
|
273
258
|
}
|
|
274
259
|
|
|
275
260
|
async openAPIRefreshStatus(): Promise<void> {
|
|
276
261
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
277
262
|
try {
|
|
278
|
-
const { body, statusCode } = await this.
|
|
279
|
-
`${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
280
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
263
|
+
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
281
264
|
const deviceStatus: any = await body.json();
|
|
282
|
-
this.
|
|
283
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
265
|
+
await this.refreshStatusCodes(statusCode, deviceStatus);;
|
|
284
266
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
285
|
-
this.
|
|
286
|
-
|
|
287
|
-
this.
|
|
288
|
-
this.updateHomeKitCharacteristics();
|
|
267
|
+
await this.successfulRefreshStatus(statusCode, deviceStatus);
|
|
268
|
+
await this.openAPIparseStatus(deviceStatus);
|
|
269
|
+
await this.updateHomeKitCharacteristics();
|
|
289
270
|
} else {
|
|
290
|
-
this.
|
|
291
|
-
this.statusCode(deviceStatus.statusCode);
|
|
271
|
+
await this.statusCodes(statusCode, deviceStatus);
|
|
292
272
|
}
|
|
293
273
|
} catch (e: any) {
|
|
294
|
-
this.apiError(e);
|
|
295
|
-
this.
|
|
296
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
274
|
+
await this.apiError(e);
|
|
275
|
+
await this.openAPIRefreshError(e);
|
|
297
276
|
}
|
|
298
277
|
}
|
|
299
278
|
|
package/src/device/meterplus.ts
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
* meterplus.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
5
|
import { Units } from 'homebridge';
|
|
6
|
-
import { Devices } from '../settings.js';
|
|
7
6
|
import { deviceBase } from './device.js';
|
|
8
7
|
import { Subject, interval, skipWhile } from 'rxjs';
|
|
9
8
|
|
|
@@ -172,8 +171,8 @@ export class MeterPlus extends deviceBase {
|
|
|
172
171
|
|
|
173
172
|
// Current Temperature
|
|
174
173
|
if (!this.device.meter?.hide_temperature) {
|
|
175
|
-
serviceData.temperature!.c < 0 ? 0 : serviceData.temperature!.c > 100 ? 100 : serviceData.temperature!.c;
|
|
176
|
-
this.TemperatureSensor!.CurrentTemperature =
|
|
174
|
+
const celcius = serviceData.temperature!.c < 0 ? 0 : serviceData.temperature!.c > 100 ? 100 : serviceData.temperature!.c;
|
|
175
|
+
this.TemperatureSensor!.CurrentTemperature = celcius;
|
|
177
176
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor!.CurrentTemperature}°c`);
|
|
178
177
|
}
|
|
179
178
|
}
|
|
@@ -241,63 +240,43 @@ export class MeterPlus extends deviceBase {
|
|
|
241
240
|
|
|
242
241
|
async BLERefreshStatus(): Promise<void> {
|
|
243
242
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
244
|
-
const switchbot = await this.
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
.
|
|
248
|
-
|
|
249
|
-
.toLowerCase();
|
|
250
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
251
|
-
this.getCustomBLEAddress(switchbot);
|
|
243
|
+
const switchbot = await this.switchbotBLE();
|
|
244
|
+
|
|
245
|
+
if (switchbot === undefined) {
|
|
246
|
+
await this.BLERefreshConnection(switchbot);
|
|
247
|
+
} else {
|
|
252
248
|
// Start to monitor advertisement packets
|
|
253
|
-
|
|
249
|
+
(async () => {
|
|
254
250
|
// Start to monitor advertisement packets
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
|
|
261
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
251
|
+
const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
|
|
252
|
+
// Update HomeKit
|
|
253
|
+
if (serviceData.model !== '' && serviceData.modelName !== '') {
|
|
254
|
+
await this.BLEparseStatus(serviceData);
|
|
255
|
+
await this.updateHomeKitCharacteristics();
|
|
262
256
|
} else {
|
|
263
|
-
this.
|
|
257
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
|
|
258
|
+
await this.BLERefreshConnection(switchbot);
|
|
264
259
|
}
|
|
265
|
-
};
|
|
266
|
-
// Wait 10 seconds
|
|
267
|
-
await switchbot.wait(this.scanDuration * 1000);
|
|
268
|
-
// Stop to monitor
|
|
269
|
-
await switchbot.stopScan();
|
|
270
|
-
// Update HomeKit
|
|
271
|
-
await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
|
|
272
|
-
await this.updateHomeKitCharacteristics();
|
|
273
|
-
})();
|
|
274
|
-
if (switchbot === undefined) {
|
|
275
|
-
await this.BLERefreshConnection(switchbot);
|
|
260
|
+
})();
|
|
276
261
|
}
|
|
277
262
|
}
|
|
278
263
|
|
|
279
264
|
async openAPIRefreshStatus(): Promise<void> {
|
|
280
265
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
281
266
|
try {
|
|
282
|
-
const { body, statusCode } = await this.
|
|
283
|
-
`${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
284
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
267
|
+
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
285
268
|
const deviceStatus: any = await body.json();
|
|
286
|
-
this.
|
|
287
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
269
|
+
await this.refreshStatusCodes(statusCode, deviceStatus);;
|
|
288
270
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
289
|
-
this.
|
|
290
|
-
|
|
291
|
-
this.
|
|
292
|
-
this.updateHomeKitCharacteristics();
|
|
271
|
+
await this.successfulRefreshStatus(statusCode, deviceStatus);
|
|
272
|
+
await this.openAPIparseStatus(deviceStatus);
|
|
273
|
+
await this.updateHomeKitCharacteristics();
|
|
293
274
|
} else {
|
|
294
|
-
this.
|
|
295
|
-
this.statusCode(deviceStatus.statusCode);
|
|
275
|
+
await this.statusCodes(statusCode, deviceStatus);
|
|
296
276
|
}
|
|
297
277
|
} catch (e: any) {
|
|
298
|
-
this.apiError(e);
|
|
299
|
-
this.
|
|
300
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
278
|
+
await this.apiError(e);
|
|
279
|
+
await this.openAPIRefreshError(e);
|
|
301
280
|
}
|
|
302
281
|
}
|
|
303
282
|
|
package/src/device/motion.ts
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
*
|
|
4
4
|
* motion.ts: @switchbot/homebridge-switchbot.
|
|
5
5
|
*/
|
|
6
|
-
import { Devices } from '../settings.js';
|
|
7
6
|
import { deviceBase } from './device.js';
|
|
8
7
|
import { Subject, interval, skipWhile } from 'rxjs';
|
|
9
8
|
|
|
@@ -250,63 +249,43 @@ export class Motion extends deviceBase {
|
|
|
250
249
|
|
|
251
250
|
async BLERefreshStatus(): Promise<void> {
|
|
252
251
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
253
|
-
const switchbot = await this.
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
.
|
|
257
|
-
|
|
258
|
-
.toLowerCase();
|
|
259
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
260
|
-
this.getCustomBLEAddress(switchbot);
|
|
252
|
+
const switchbot = await this.switchbotBLE();
|
|
253
|
+
|
|
254
|
+
if (switchbot === undefined) {
|
|
255
|
+
await this.BLERefreshConnection(switchbot);
|
|
256
|
+
} else {
|
|
261
257
|
// Start to monitor advertisement packets
|
|
262
|
-
|
|
258
|
+
(async () => {
|
|
263
259
|
// Start to monitor advertisement packets
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
|
|
270
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
260
|
+
const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
|
|
261
|
+
// Update HomeKit
|
|
262
|
+
if (serviceData.model !== '' && serviceData.modelName !== '') {
|
|
263
|
+
await this.BLEparseStatus(serviceData);
|
|
264
|
+
await this.updateHomeKitCharacteristics();
|
|
271
265
|
} else {
|
|
272
|
-
this.
|
|
266
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
|
|
267
|
+
await this.BLERefreshConnection(switchbot);
|
|
273
268
|
}
|
|
274
|
-
};
|
|
275
|
-
// Wait 10 seconds
|
|
276
|
-
await switchbot.wait(this.scanDuration * 1000);
|
|
277
|
-
// Stop to monitor
|
|
278
|
-
await switchbot.stopScan();
|
|
279
|
-
// Update HomeKit
|
|
280
|
-
await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
|
|
281
|
-
await this.updateHomeKitCharacteristics();
|
|
282
|
-
})();
|
|
283
|
-
if (switchbot === undefined) {
|
|
284
|
-
await this.BLERefreshConnection(switchbot);
|
|
269
|
+
})();
|
|
285
270
|
}
|
|
286
271
|
}
|
|
287
272
|
|
|
288
273
|
async openAPIRefreshStatus(): Promise<void> {
|
|
289
274
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
290
275
|
try {
|
|
291
|
-
const { body, statusCode } = await this.
|
|
292
|
-
`${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
293
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
276
|
+
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
294
277
|
const deviceStatus: any = await body.json();
|
|
295
|
-
this.
|
|
296
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
278
|
+
await this.refreshStatusCodes(statusCode, deviceStatus);;
|
|
297
279
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
298
|
-
this.
|
|
299
|
-
|
|
300
|
-
this.
|
|
301
|
-
this.updateHomeKitCharacteristics();
|
|
280
|
+
await this.successfulRefreshStatus(statusCode, deviceStatus);
|
|
281
|
+
await this.openAPIparseStatus(deviceStatus);
|
|
282
|
+
await this.updateHomeKitCharacteristics();
|
|
302
283
|
} else {
|
|
303
|
-
this.
|
|
304
|
-
this.statusCode(deviceStatus.statusCode);
|
|
284
|
+
await this.statusCodes(statusCode, deviceStatus);
|
|
305
285
|
}
|
|
306
286
|
} catch (e: any) {
|
|
307
|
-
this.apiError(e);
|
|
308
|
-
this.
|
|
309
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
287
|
+
await this.apiError(e);
|
|
288
|
+
await this.openAPIRefreshError(e);
|
|
310
289
|
}
|
|
311
290
|
}
|
|
312
291
|
|
package/src/device/plug.ts
CHANGED
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
*
|
|
3
3
|
* plug.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
|
-
import { request } from 'undici';
|
|
6
|
-
import { Devices } from '../settings.js';
|
|
7
5
|
import { deviceBase } from './device.js';
|
|
8
6
|
import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
|
|
9
7
|
|
|
@@ -146,63 +144,43 @@ export class Plug extends deviceBase {
|
|
|
146
144
|
|
|
147
145
|
async BLERefreshStatus(): Promise<void> {
|
|
148
146
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
149
|
-
const switchbot = await this.
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
.
|
|
153
|
-
|
|
154
|
-
.toLowerCase();
|
|
155
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
156
|
-
this.getCustomBLEAddress(switchbot);
|
|
147
|
+
const switchbot = await this.switchbotBLE();
|
|
148
|
+
|
|
149
|
+
if (switchbot === undefined) {
|
|
150
|
+
await this.BLERefreshConnection(switchbot);
|
|
151
|
+
} else {
|
|
157
152
|
// Start to monitor advertisement packets
|
|
158
|
-
|
|
153
|
+
(async () => {
|
|
159
154
|
// Start to monitor advertisement packets
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
|
|
166
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
155
|
+
const serviceData: serviceData = await this.monitorAdvertisementPackets(switchbot);
|
|
156
|
+
// Update HomeKit
|
|
157
|
+
if (serviceData.model !== '' && serviceData.modelName !== '') {
|
|
158
|
+
await this.BLEparseStatus(serviceData);
|
|
159
|
+
await this.updateHomeKitCharacteristics();
|
|
167
160
|
} else {
|
|
168
|
-
this.
|
|
161
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
|
|
162
|
+
await this.BLERefreshConnection(switchbot);
|
|
169
163
|
}
|
|
170
|
-
};
|
|
171
|
-
// Wait 10 seconds
|
|
172
|
-
await switchbot.wait(this.scanDuration * 1000);
|
|
173
|
-
// Stop to monitor
|
|
174
|
-
await switchbot.stopScan();
|
|
175
|
-
// Update HomeKit
|
|
176
|
-
await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
|
|
177
|
-
await this.updateHomeKitCharacteristics();
|
|
178
|
-
})();
|
|
179
|
-
if (switchbot === undefined) {
|
|
180
|
-
await this.BLERefreshConnection(switchbot);
|
|
164
|
+
})();
|
|
181
165
|
}
|
|
182
166
|
}
|
|
183
167
|
|
|
184
168
|
async openAPIRefreshStatus(): Promise<void> {
|
|
185
169
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
186
170
|
try {
|
|
187
|
-
const { body, statusCode } = await this.
|
|
188
|
-
`${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
189
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
171
|
+
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
190
172
|
const deviceStatus: any = await body.json();
|
|
191
|
-
this.
|
|
192
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
173
|
+
await this.refreshStatusCodes(statusCode, deviceStatus);;
|
|
193
174
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
194
|
-
this.
|
|
195
|
-
|
|
196
|
-
this.
|
|
197
|
-
this.updateHomeKitCharacteristics();
|
|
175
|
+
await this.successfulRefreshStatus(statusCode, deviceStatus);
|
|
176
|
+
await this.openAPIparseStatus(deviceStatus);
|
|
177
|
+
await this.updateHomeKitCharacteristics();
|
|
198
178
|
} else {
|
|
199
|
-
this.
|
|
200
|
-
this.statusCode(deviceStatus.statusCode);
|
|
179
|
+
await this.statusCodes(statusCode, deviceStatus);
|
|
201
180
|
}
|
|
202
181
|
} catch (e: any) {
|
|
203
|
-
this.apiError(e);
|
|
204
|
-
this.
|
|
205
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
182
|
+
await this.apiError(e);
|
|
183
|
+
await this.openAPIRefreshError(e);
|
|
206
184
|
}
|
|
207
185
|
}
|
|
208
186
|
|
|
@@ -245,8 +223,7 @@ export class Plug extends deviceBase {
|
|
|
245
223
|
await this.openAPIpushChanges();
|
|
246
224
|
} else {
|
|
247
225
|
await this.offlineOff();
|
|
248
|
-
this.
|
|
249
|
-
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
226
|
+
await this.pushChangeDisabled();
|
|
250
227
|
}
|
|
251
228
|
// Refresh the status from the API
|
|
252
229
|
interval(15000)
|
|
@@ -264,11 +241,7 @@ export class Plug extends deviceBase {
|
|
|
264
241
|
+ ` On: ${this.Outlet.On} OnCached: ${this.accessory.context.On}`);
|
|
265
242
|
const switchbot = await this.platform.connectBLE();
|
|
266
243
|
// Convert to BLE Address
|
|
267
|
-
|
|
268
|
-
.deviceId!.match(/.{1,2}/g)!
|
|
269
|
-
.join(':')
|
|
270
|
-
.toLowerCase();
|
|
271
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
244
|
+
await this.convertBLEAddress();
|
|
272
245
|
switchbot
|
|
273
246
|
.discover({
|
|
274
247
|
model: this.device.bleModel,
|
|
@@ -321,24 +294,15 @@ export class Plug extends deviceBase {
|
|
|
321
294
|
});
|
|
322
295
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
323
296
|
try {
|
|
324
|
-
const { body, statusCode } = await
|
|
325
|
-
body: bodyChange,
|
|
326
|
-
method: 'POST',
|
|
327
|
-
headers: this.platform.generateHeaders(),
|
|
328
|
-
});
|
|
329
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
297
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
330
298
|
const deviceStatus: any = await body.json();
|
|
331
|
-
this.
|
|
332
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
333
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
299
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
334
300
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
335
|
-
this.
|
|
336
|
-
|
|
337
|
-
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
338
|
-
+ `request to SwitchBot API, body: ${JSON.stringify(JSON.parse(bodyChange))} sent successfully`);
|
|
301
|
+
await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
|
|
302
|
+
await this.updateHomeKitCharacteristics();
|
|
339
303
|
} else {
|
|
340
|
-
this.statusCode(statusCode);
|
|
341
|
-
this.statusCode(deviceStatus.statusCode);
|
|
304
|
+
await this.statusCode(statusCode);
|
|
305
|
+
await this.statusCode(deviceStatus.statusCode);
|
|
342
306
|
}
|
|
343
307
|
} catch (e: any) {
|
|
344
308
|
this.apiError(e);
|