@switchbot/homebridge-switchbot 2.0.0-beta.19 → 2.0.0-beta.20
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 +6 -1
- package/README.md +1 -2
- package/config.schema.json +51 -32
- package/dist/device/bot.d.ts +32 -29
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +549 -575
- package/dist/device/bot.js.map +1 -1
- package/dist/device/colorbulb.d.ts +33 -15
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +426 -341
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +16 -9
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +246 -208
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +21 -18
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +449 -449
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/humidifier.d.ts +22 -20
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +346 -324
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/lock.d.ts +22 -10
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +257 -178
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +21 -14
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +287 -239
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +23 -10
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +308 -190
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +17 -10
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +225 -193
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +24 -17
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +270 -267
- package/dist/device/plug.js.map +1 -1
- package/dist/device/striplight.d.ts +36 -14
- package/dist/device/striplight.d.ts.map +1 -1
- package/dist/device/striplight.js +447 -299
- package/dist/device/striplight.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +7 -14
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +177 -169
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +3 -5
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +72 -57
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +3 -4
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +47 -33
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +3 -4
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +66 -48
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/light.d.ts +3 -4
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +48 -47
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +3 -4
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +47 -33
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +10 -11
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +64 -48
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +7 -12
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +52 -41
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +6 -11
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +52 -42
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +0 -4
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +44 -66
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +3 -3
- package/dist/settings.d.ts.map +1 -1
- package/package.json +7 -6
- package/src/device/bot.ts +551 -589
- package/src/device/colorbulb.ts +441 -375
- package/src/device/contact.ts +249 -213
- package/src/device/curtain.ts +452 -461
- package/src/device/humidifier.ts +358 -365
- package/src/device/lock.ts +263 -179
- package/src/device/meter.ts +302 -253
- package/src/device/meterplus.ts +319 -194
- package/src/device/motion.ts +230 -199
- package/src/device/plug.ts +273 -274
- package/src/device/striplight.ts +460 -316
- package/src/irdevice/airconditioner.ts +181 -183
- package/src/irdevice/airpurifier.ts +70 -60
- package/src/irdevice/camera.ts +46 -36
- package/src/irdevice/fan.ts +64 -49
- package/src/irdevice/light.ts +47 -51
- package/src/irdevice/other.ts +46 -36
- package/src/irdevice/tv.ts +64 -51
- package/src/irdevice/vacuumcleaner.ts +52 -45
- package/src/irdevice/waterheater.ts +50 -45
- package/src/platform.ts +44 -68
- package/src/settings.ts +3 -3
package/dist/device/contact.js
CHANGED
|
@@ -20,13 +20,15 @@ class Contact {
|
|
|
20
20
|
this.platform = platform;
|
|
21
21
|
this.accessory = accessory;
|
|
22
22
|
this.device = device;
|
|
23
|
+
// Connection
|
|
24
|
+
this.BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'Both');
|
|
25
|
+
this.OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'Both');
|
|
23
26
|
// default placeholders
|
|
24
27
|
this.logs(device);
|
|
25
|
-
this.openAPI();
|
|
26
28
|
this.scan(device);
|
|
27
29
|
this.refreshRate(device);
|
|
28
30
|
this.config(device);
|
|
29
|
-
this.
|
|
31
|
+
this.context();
|
|
30
32
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
31
33
|
this.doContactUpdate = new rxjs_1.Subject();
|
|
32
34
|
this.contactUbpdateInProgress = false;
|
|
@@ -87,12 +89,12 @@ class Contact {
|
|
|
87
89
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
|
|
88
90
|
}
|
|
89
91
|
// Battery Service
|
|
90
|
-
if (!
|
|
92
|
+
if (!this.BLE) {
|
|
91
93
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Battery Service`);
|
|
92
94
|
this.batteryService = this.accessory.getService(this.platform.Service.Battery);
|
|
93
95
|
accessory.removeService(this.batteryService);
|
|
94
96
|
}
|
|
95
|
-
else if (
|
|
97
|
+
else if (this.BLE && !this.batteryService) {
|
|
96
98
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
|
|
97
99
|
(this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
|
|
98
100
|
`${accessory.displayName} Battery`;
|
|
@@ -114,15 +116,20 @@ class Contact {
|
|
|
114
116
|
* Parse the device status from the SwitchBot api
|
|
115
117
|
*/
|
|
116
118
|
async parseStatus() {
|
|
117
|
-
if (this.
|
|
119
|
+
if (this.BLE && !this.SwitchToOpenAPI) {
|
|
120
|
+
await this.BLEparseStatus();
|
|
121
|
+
}
|
|
122
|
+
else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
123
|
+
this.SwitchToOpenAPI = false;
|
|
118
124
|
await this.openAPIparseStatus();
|
|
119
125
|
}
|
|
120
126
|
else {
|
|
121
|
-
|
|
127
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
128
|
+
+ ` ${this.device.connectionType}, parseStatus will not happen.`);
|
|
122
129
|
}
|
|
123
130
|
}
|
|
124
131
|
async BLEparseStatus() {
|
|
125
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}
|
|
132
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
|
|
126
133
|
// Door State
|
|
127
134
|
switch (this.doorState) {
|
|
128
135
|
case 'open':
|
|
@@ -171,57 +178,56 @@ class Contact {
|
|
|
171
178
|
+ `StatusLowBattery: ${this.StatusLowBattery}`);
|
|
172
179
|
}
|
|
173
180
|
async openAPIparseStatus() {
|
|
174
|
-
|
|
175
|
-
|
|
181
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
|
|
182
|
+
// Contact State
|
|
183
|
+
if (this.openState === 'open') {
|
|
184
|
+
this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
|
|
185
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
|
|
176
186
|
}
|
|
177
|
-
if (this.
|
|
178
|
-
this.
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
switch (this.brightness) {
|
|
201
|
-
case 'dim':
|
|
202
|
-
this.CurrentAmbientLightLevel = this.set_minLux;
|
|
203
|
-
break;
|
|
204
|
-
case 'bright':
|
|
205
|
-
default:
|
|
206
|
-
this.CurrentAmbientLightLevel = this.set_maxLux;
|
|
207
|
-
}
|
|
208
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
|
|
187
|
+
else if (this.openState === 'close') {
|
|
188
|
+
this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
|
|
189
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openState: ${this.openState}`);
|
|
193
|
+
}
|
|
194
|
+
// Motion State
|
|
195
|
+
if (!this.device.contact?.hide_motionsensor) {
|
|
196
|
+
this.MotionDetected = this.moveDetected;
|
|
197
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
|
|
198
|
+
}
|
|
199
|
+
// Light Level
|
|
200
|
+
if (!this.device.contact?.hide_lightsensor) {
|
|
201
|
+
this.set_minLux = this.minLux();
|
|
202
|
+
this.set_maxLux = this.maxLux();
|
|
203
|
+
switch (this.brightness) {
|
|
204
|
+
case 'dim':
|
|
205
|
+
this.CurrentAmbientLightLevel = this.set_minLux;
|
|
206
|
+
break;
|
|
207
|
+
case 'bright':
|
|
208
|
+
default:
|
|
209
|
+
this.CurrentAmbientLightLevel = this.set_maxLux;
|
|
209
210
|
}
|
|
211
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
|
|
210
212
|
}
|
|
211
213
|
}
|
|
212
214
|
/**
|
|
213
215
|
* Asks the SwitchBot API for the latest device information
|
|
214
216
|
*/
|
|
215
217
|
async refreshStatus() {
|
|
216
|
-
if (this.
|
|
218
|
+
if (this.BLE) {
|
|
217
219
|
await this.BLERefreshStatus();
|
|
218
220
|
}
|
|
219
|
-
else {
|
|
221
|
+
else if (this.OpenAPI) {
|
|
220
222
|
await this.openAPIRefreshStatus();
|
|
221
223
|
}
|
|
224
|
+
else {
|
|
225
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
226
|
+
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
227
|
+
}
|
|
222
228
|
}
|
|
223
229
|
async BLERefreshStatus() {
|
|
224
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}
|
|
230
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
225
231
|
const switchbot = await this.platform.connectBLE();
|
|
226
232
|
// Convert to BLE Address
|
|
227
233
|
this.device.bleMac =
|
|
@@ -281,109 +287,69 @@ class Contact {
|
|
|
281
287
|
}
|
|
282
288
|
})
|
|
283
289
|
.catch(async (e) => {
|
|
284
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
|
|
285
|
-
if (this.deviceLogging.includes('debug')) {
|
|
286
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` +
|
|
287
|
-
` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
288
|
-
}
|
|
289
|
-
if (this.platform.config.credentials?.token) {
|
|
290
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
291
|
-
this.SwitchToOpenAPI = true;
|
|
292
|
-
await this.openAPIRefreshStatus();
|
|
293
|
-
}
|
|
294
290
|
this.apiError(e);
|
|
291
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
|
|
292
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
293
|
+
await this.BLEconnection(switchbot);
|
|
295
294
|
});
|
|
296
295
|
}
|
|
297
296
|
else {
|
|
298
297
|
await this.BLEconnection(switchbot);
|
|
299
298
|
}
|
|
300
299
|
}
|
|
301
|
-
async getCustomBLEAddress(switchbot) {
|
|
302
|
-
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
303
|
-
(async () => {
|
|
304
|
-
// Start to monitor advertisement packets
|
|
305
|
-
await switchbot.startScan({
|
|
306
|
-
model: 'd',
|
|
307
|
-
});
|
|
308
|
-
// Set an event handler
|
|
309
|
-
switchbot.onadvertisement = (ad) => {
|
|
310
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
|
|
311
|
-
};
|
|
312
|
-
await switchbot.wait(10000);
|
|
313
|
-
// Stop to monitor
|
|
314
|
-
switchbot.stopScan();
|
|
315
|
-
})();
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
async BLEconnection(switchbot) {
|
|
319
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
|
|
320
|
-
if (this.platform.config.credentials?.token) {
|
|
321
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
322
|
-
this.SwitchToOpenAPI = true;
|
|
323
|
-
await this.openAPIRefreshStatus();
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
300
|
async openAPIRefreshStatus() {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
355
|
-
});
|
|
356
|
-
res.on('end', () => {
|
|
357
|
-
try {
|
|
358
|
-
this.deviceStatus = JSON.parse(rawData);
|
|
359
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
|
|
360
|
-
this.openState = this.deviceStatus.body.openState;
|
|
361
|
-
this.moveDetected = this.deviceStatus.body.moveDetected;
|
|
362
|
-
this.brightness = this.deviceStatus.body.brightness;
|
|
363
|
-
this.parseStatus();
|
|
364
|
-
this.updateHomeKitCharacteristics();
|
|
365
|
-
}
|
|
366
|
-
catch (e) {
|
|
367
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
368
|
-
}
|
|
369
|
-
});
|
|
301
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
302
|
+
try {
|
|
303
|
+
const t = Date.now();
|
|
304
|
+
const nonce = 'requestID';
|
|
305
|
+
const data = this.platform.config.credentials?.token + t + nonce;
|
|
306
|
+
const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
|
|
307
|
+
const sign = signTerm.toString('base64');
|
|
308
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
|
|
309
|
+
const options = {
|
|
310
|
+
hostname: settings_1.HostDomain,
|
|
311
|
+
port: 443,
|
|
312
|
+
path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
|
|
313
|
+
method: 'GET',
|
|
314
|
+
headers: {
|
|
315
|
+
Authorization: this.platform.config.credentials?.token,
|
|
316
|
+
sign: sign,
|
|
317
|
+
nonce: nonce,
|
|
318
|
+
t: t,
|
|
319
|
+
'Content-Type': 'application/json',
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
const req = https_1.default.request(options, (res) => {
|
|
323
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
|
|
324
|
+
let rawData = '';
|
|
325
|
+
res.on('data', (d) => {
|
|
326
|
+
rawData += d;
|
|
327
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
|
|
370
328
|
});
|
|
371
|
-
|
|
372
|
-
|
|
329
|
+
res.on('end', () => {
|
|
330
|
+
try {
|
|
331
|
+
this.deviceStatus = JSON.parse(rawData);
|
|
332
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
|
|
333
|
+
this.openState = this.deviceStatus.body.openState;
|
|
334
|
+
this.moveDetected = this.deviceStatus.body.moveDetected;
|
|
335
|
+
this.brightness = this.deviceStatus.body.brightness;
|
|
336
|
+
this.parseStatus();
|
|
337
|
+
this.updateHomeKitCharacteristics();
|
|
338
|
+
}
|
|
339
|
+
catch (e) {
|
|
340
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
341
|
+
}
|
|
373
342
|
});
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
|
|
380
|
-
` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
381
|
-
}
|
|
382
|
-
this.apiError(e);
|
|
383
|
-
}
|
|
343
|
+
});
|
|
344
|
+
req.on('error', (e) => {
|
|
345
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
|
|
346
|
+
});
|
|
347
|
+
req.end();
|
|
384
348
|
}
|
|
385
|
-
|
|
386
|
-
this.
|
|
349
|
+
catch (e) {
|
|
350
|
+
this.apiError(e);
|
|
351
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
|
|
352
|
+
+ ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
|
|
387
353
|
}
|
|
388
354
|
}
|
|
389
355
|
/**
|
|
@@ -394,6 +360,7 @@ class Contact {
|
|
|
394
360
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
|
|
395
361
|
}
|
|
396
362
|
else {
|
|
363
|
+
this.accessory.context.ContactSensorState = this.ContactSensorState;
|
|
397
364
|
this.contactSensorservice.updateCharacteristic(this.platform.Characteristic.ContactSensorState, this.ContactSensorState);
|
|
398
365
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic ContactSensorState: ${this.ContactSensorState}`);
|
|
399
366
|
}
|
|
@@ -402,6 +369,7 @@ class Contact {
|
|
|
402
369
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
|
|
403
370
|
}
|
|
404
371
|
else {
|
|
372
|
+
this.accessory.context.MotionDetected = this.MotionDetected;
|
|
405
373
|
this.motionService?.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.MotionDetected);
|
|
406
374
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
|
|
407
375
|
}
|
|
@@ -411,16 +379,18 @@ class Contact {
|
|
|
411
379
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
|
|
412
380
|
}
|
|
413
381
|
else {
|
|
382
|
+
this.accessory.context.CurrentAmbientLightLevel = this.CurrentAmbientLightLevel;
|
|
414
383
|
this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
|
|
415
384
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
416
385
|
+ ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
|
|
417
386
|
}
|
|
418
387
|
}
|
|
419
|
-
if (this.
|
|
388
|
+
if (this.BLE) {
|
|
420
389
|
if (this.BatteryLevel === undefined) {
|
|
421
390
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
|
|
422
391
|
}
|
|
423
392
|
else {
|
|
393
|
+
this.accessory.context.BatteryLevel = this.BatteryLevel;
|
|
424
394
|
this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
|
|
425
395
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
|
|
426
396
|
}
|
|
@@ -428,98 +398,121 @@ class Contact {
|
|
|
428
398
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
|
|
429
399
|
}
|
|
430
400
|
else {
|
|
401
|
+
this.accessory.context.StatusLowBattery = this.StatusLowBattery;
|
|
431
402
|
this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
|
|
432
403
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
|
|
433
404
|
}
|
|
434
405
|
}
|
|
435
406
|
}
|
|
436
|
-
async
|
|
437
|
-
this.
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
407
|
+
async getCustomBLEAddress(switchbot) {
|
|
408
|
+
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
409
|
+
(async () => {
|
|
410
|
+
// Start to monitor advertisement packets
|
|
411
|
+
await switchbot.startScan({
|
|
412
|
+
model: 'd',
|
|
413
|
+
});
|
|
414
|
+
// Set an event handler
|
|
415
|
+
switchbot.onadvertisement = (ad) => {
|
|
416
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
|
|
417
|
+
};
|
|
418
|
+
await switchbot.wait(10000);
|
|
419
|
+
// Stop to monitor
|
|
420
|
+
switchbot.stopScan();
|
|
421
|
+
})();
|
|
447
422
|
}
|
|
448
|
-
//throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
|
|
449
423
|
}
|
|
450
|
-
async
|
|
451
|
-
|
|
452
|
-
if (device.
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
config['ble'] = device.ble;
|
|
457
|
-
}
|
|
458
|
-
if (device.openAPI !== undefined) {
|
|
459
|
-
config['openAPI'] = device.openAPI;
|
|
460
|
-
}
|
|
461
|
-
if (device.logging !== undefined) {
|
|
462
|
-
config['logging'] = device.logging;
|
|
463
|
-
}
|
|
464
|
-
if (device.refreshRate !== undefined) {
|
|
465
|
-
config['refreshRate'] = device.refreshRate;
|
|
466
|
-
}
|
|
467
|
-
if (device.scanDuration !== undefined) {
|
|
468
|
-
config['scanDuration'] = device.scanDuration;
|
|
469
|
-
}
|
|
470
|
-
if (Object.entries(config).length !== 0) {
|
|
471
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
|
|
424
|
+
async BLEconnection(switchbot) {
|
|
425
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
|
|
426
|
+
if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
|
|
427
|
+
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
|
|
428
|
+
this.SwitchToOpenAPI = true;
|
|
429
|
+
await this.openAPIRefreshStatus();
|
|
472
430
|
}
|
|
473
431
|
}
|
|
474
|
-
|
|
475
|
-
if (device.
|
|
476
|
-
this.
|
|
477
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
|
|
432
|
+
minLux() {
|
|
433
|
+
if (this.device.contact?.set_minLux) {
|
|
434
|
+
this.set_minLux = this.device.contact.set_minLux;
|
|
478
435
|
}
|
|
479
|
-
else
|
|
480
|
-
this.
|
|
481
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
436
|
+
else {
|
|
437
|
+
this.set_minLux = 1;
|
|
482
438
|
}
|
|
439
|
+
return this.set_minLux;
|
|
483
440
|
}
|
|
484
|
-
|
|
485
|
-
if (
|
|
486
|
-
this.
|
|
441
|
+
maxLux() {
|
|
442
|
+
if (this.device.contact?.set_maxLux) {
|
|
443
|
+
this.set_maxLux = this.device.contact.set_maxLux;
|
|
487
444
|
}
|
|
488
445
|
else {
|
|
489
|
-
this.
|
|
446
|
+
this.set_maxLux = 6001;
|
|
490
447
|
}
|
|
491
|
-
|
|
448
|
+
return this.set_maxLux;
|
|
492
449
|
}
|
|
493
450
|
async scan(device) {
|
|
494
451
|
if (device.scanDuration) {
|
|
495
452
|
this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
|
|
496
|
-
if (
|
|
453
|
+
if (this.BLE) {
|
|
497
454
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
498
455
|
}
|
|
499
456
|
}
|
|
500
457
|
else {
|
|
501
458
|
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
502
|
-
if (this.
|
|
459
|
+
if (this.BLE) {
|
|
503
460
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
504
461
|
}
|
|
505
462
|
}
|
|
506
463
|
}
|
|
507
|
-
async
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
464
|
+
async statusCode({ res }) {
|
|
465
|
+
switch (res.statusCode) {
|
|
466
|
+
case 151:
|
|
467
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
|
|
468
|
+
break;
|
|
469
|
+
case 152:
|
|
470
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
|
|
471
|
+
break;
|
|
472
|
+
case 160:
|
|
473
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
|
|
474
|
+
break;
|
|
475
|
+
case 161:
|
|
476
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
|
|
477
|
+
this.offlineOff();
|
|
478
|
+
break;
|
|
479
|
+
case 171:
|
|
480
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
|
|
481
|
+
this.offlineOff();
|
|
482
|
+
break;
|
|
483
|
+
case 190:
|
|
484
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
|
|
485
|
+
` Or command: ${(0, super_stringify_1.default)(res)} format is invalid`);
|
|
486
|
+
break;
|
|
487
|
+
case 100:
|
|
488
|
+
if (this.platform.debugMode) {
|
|
489
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
|
|
490
|
+
}
|
|
491
|
+
break;
|
|
492
|
+
default:
|
|
493
|
+
if (this.platform.debugMode) {
|
|
494
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
|
|
495
|
+
}
|
|
511
496
|
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
497
|
+
}
|
|
498
|
+
async offlineOff() {
|
|
499
|
+
if (this.device.offline) {
|
|
500
|
+
this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
|
|
501
|
+
this.contactSensorservice.getCharacteristic(this.platform.Characteristic.ContactSensorState).updateValue(this.ContactSensorState);
|
|
502
|
+
await this.updateHomeKitCharacteristics();
|
|
515
503
|
}
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
504
|
+
}
|
|
505
|
+
async apiError(e) {
|
|
506
|
+
this.contactSensorservice.updateCharacteristic(this.platform.Characteristic.ContactSensorState, e);
|
|
507
|
+
if (!this.device.contact?.hide_motionsensor) {
|
|
508
|
+
this.motionService?.updateCharacteristic(this.platform.Characteristic.MotionDetected, e);
|
|
519
509
|
}
|
|
520
|
-
|
|
521
|
-
this.
|
|
522
|
-
|
|
510
|
+
if (!this.device.contact?.hide_lightsensor) {
|
|
511
|
+
this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
|
|
512
|
+
}
|
|
513
|
+
if (this.BLE) {
|
|
514
|
+
this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
|
|
515
|
+
this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
|
|
523
516
|
}
|
|
524
517
|
}
|
|
525
518
|
FirmwareRevision(accessory, device) {
|
|
@@ -539,23 +532,68 @@ class Contact {
|
|
|
539
532
|
}
|
|
540
533
|
return FirmwareRevision;
|
|
541
534
|
}
|
|
542
|
-
|
|
543
|
-
if (this.
|
|
544
|
-
this.
|
|
535
|
+
async context() {
|
|
536
|
+
if (this.MotionDetected === undefined) {
|
|
537
|
+
this.MotionDetected = false;
|
|
545
538
|
}
|
|
546
539
|
else {
|
|
547
|
-
this.
|
|
540
|
+
this.MotionDetected = this.accessory.context.MotionDetected;
|
|
541
|
+
}
|
|
542
|
+
if (this.ContactSensorState === undefined) {
|
|
543
|
+
this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
this.ContactSensorState = this.accessory.context.ContactSensorState;
|
|
548
547
|
}
|
|
549
|
-
return this.set_minLux;
|
|
550
548
|
}
|
|
551
|
-
|
|
552
|
-
if (
|
|
553
|
-
this.
|
|
549
|
+
async refreshRate(device) {
|
|
550
|
+
if (device.refreshRate) {
|
|
551
|
+
this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
|
|
552
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
|
|
553
|
+
}
|
|
554
|
+
else if (this.platform.config.options.refreshRate) {
|
|
555
|
+
this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
|
|
556
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
async config(device) {
|
|
560
|
+
let config = {};
|
|
561
|
+
if (device.contact) {
|
|
562
|
+
config = device.contact;
|
|
563
|
+
}
|
|
564
|
+
if (device.connectionType !== undefined) {
|
|
565
|
+
config['connectionType'] = device.connectionType;
|
|
566
|
+
}
|
|
567
|
+
if (device.logging !== undefined) {
|
|
568
|
+
config['logging'] = device.logging;
|
|
569
|
+
}
|
|
570
|
+
if (device.refreshRate !== undefined) {
|
|
571
|
+
config['refreshRate'] = device.refreshRate;
|
|
572
|
+
}
|
|
573
|
+
if (device.scanDuration !== undefined) {
|
|
574
|
+
config['scanDuration'] = device.scanDuration;
|
|
575
|
+
}
|
|
576
|
+
if (Object.entries(config).length !== 0) {
|
|
577
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
async logs(device) {
|
|
581
|
+
if (this.platform.debugMode) {
|
|
582
|
+
this.deviceLogging = this.accessory.context.logging = 'debugMode';
|
|
583
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
|
|
584
|
+
}
|
|
585
|
+
else if (device.logging) {
|
|
586
|
+
this.deviceLogging = this.accessory.context.logging = device.logging;
|
|
587
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
|
|
588
|
+
}
|
|
589
|
+
else if (this.platform.config.options?.logging) {
|
|
590
|
+
this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
|
|
591
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
|
|
554
592
|
}
|
|
555
593
|
else {
|
|
556
|
-
this.
|
|
594
|
+
this.deviceLogging = this.accessory.context.logging = 'standard';
|
|
595
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
|
|
557
596
|
}
|
|
558
|
-
return this.set_maxLux;
|
|
559
597
|
}
|
|
560
598
|
/**
|
|
561
599
|
* Logging for Device
|