@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.
Files changed (112) hide show
  1. package/CHANGELOG.md +6 -1
  2. package/README.md +1 -2
  3. package/config.schema.json +51 -32
  4. package/dist/device/bot.d.ts +32 -29
  5. package/dist/device/bot.d.ts.map +1 -1
  6. package/dist/device/bot.js +549 -575
  7. package/dist/device/bot.js.map +1 -1
  8. package/dist/device/colorbulb.d.ts +33 -15
  9. package/dist/device/colorbulb.d.ts.map +1 -1
  10. package/dist/device/colorbulb.js +426 -341
  11. package/dist/device/colorbulb.js.map +1 -1
  12. package/dist/device/contact.d.ts +16 -9
  13. package/dist/device/contact.d.ts.map +1 -1
  14. package/dist/device/contact.js +246 -208
  15. package/dist/device/contact.js.map +1 -1
  16. package/dist/device/curtain.d.ts +21 -18
  17. package/dist/device/curtain.d.ts.map +1 -1
  18. package/dist/device/curtain.js +449 -449
  19. package/dist/device/curtain.js.map +1 -1
  20. package/dist/device/humidifier.d.ts +22 -20
  21. package/dist/device/humidifier.d.ts.map +1 -1
  22. package/dist/device/humidifier.js +346 -324
  23. package/dist/device/humidifier.js.map +1 -1
  24. package/dist/device/lock.d.ts +22 -10
  25. package/dist/device/lock.d.ts.map +1 -1
  26. package/dist/device/lock.js +257 -178
  27. package/dist/device/lock.js.map +1 -1
  28. package/dist/device/meter.d.ts +21 -14
  29. package/dist/device/meter.d.ts.map +1 -1
  30. package/dist/device/meter.js +287 -239
  31. package/dist/device/meter.js.map +1 -1
  32. package/dist/device/meterplus.d.ts +23 -10
  33. package/dist/device/meterplus.d.ts.map +1 -1
  34. package/dist/device/meterplus.js +308 -190
  35. package/dist/device/meterplus.js.map +1 -1
  36. package/dist/device/motion.d.ts +17 -10
  37. package/dist/device/motion.d.ts.map +1 -1
  38. package/dist/device/motion.js +225 -193
  39. package/dist/device/motion.js.map +1 -1
  40. package/dist/device/plug.d.ts +24 -17
  41. package/dist/device/plug.d.ts.map +1 -1
  42. package/dist/device/plug.js +270 -267
  43. package/dist/device/plug.js.map +1 -1
  44. package/dist/device/striplight.d.ts +36 -14
  45. package/dist/device/striplight.d.ts.map +1 -1
  46. package/dist/device/striplight.js +447 -299
  47. package/dist/device/striplight.js.map +1 -1
  48. package/dist/irdevice/airconditioner.d.ts +7 -14
  49. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  50. package/dist/irdevice/airconditioner.js +177 -169
  51. package/dist/irdevice/airconditioner.js.map +1 -1
  52. package/dist/irdevice/airpurifier.d.ts +3 -5
  53. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  54. package/dist/irdevice/airpurifier.js +72 -57
  55. package/dist/irdevice/airpurifier.js.map +1 -1
  56. package/dist/irdevice/camera.d.ts +3 -4
  57. package/dist/irdevice/camera.d.ts.map +1 -1
  58. package/dist/irdevice/camera.js +47 -33
  59. package/dist/irdevice/camera.js.map +1 -1
  60. package/dist/irdevice/fan.d.ts +3 -4
  61. package/dist/irdevice/fan.d.ts.map +1 -1
  62. package/dist/irdevice/fan.js +66 -48
  63. package/dist/irdevice/fan.js.map +1 -1
  64. package/dist/irdevice/light.d.ts +3 -4
  65. package/dist/irdevice/light.d.ts.map +1 -1
  66. package/dist/irdevice/light.js +48 -47
  67. package/dist/irdevice/light.js.map +1 -1
  68. package/dist/irdevice/other.d.ts +3 -4
  69. package/dist/irdevice/other.d.ts.map +1 -1
  70. package/dist/irdevice/other.js +47 -33
  71. package/dist/irdevice/other.js.map +1 -1
  72. package/dist/irdevice/tv.d.ts +10 -11
  73. package/dist/irdevice/tv.d.ts.map +1 -1
  74. package/dist/irdevice/tv.js +64 -48
  75. package/dist/irdevice/tv.js.map +1 -1
  76. package/dist/irdevice/vacuumcleaner.d.ts +7 -12
  77. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  78. package/dist/irdevice/vacuumcleaner.js +52 -41
  79. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  80. package/dist/irdevice/waterheater.d.ts +6 -11
  81. package/dist/irdevice/waterheater.d.ts.map +1 -1
  82. package/dist/irdevice/waterheater.js +52 -42
  83. package/dist/irdevice/waterheater.js.map +1 -1
  84. package/dist/platform.d.ts +0 -4
  85. package/dist/platform.d.ts.map +1 -1
  86. package/dist/platform.js +44 -66
  87. package/dist/platform.js.map +1 -1
  88. package/dist/settings.d.ts +3 -3
  89. package/dist/settings.d.ts.map +1 -1
  90. package/package.json +7 -6
  91. package/src/device/bot.ts +551 -589
  92. package/src/device/colorbulb.ts +441 -375
  93. package/src/device/contact.ts +249 -213
  94. package/src/device/curtain.ts +452 -461
  95. package/src/device/humidifier.ts +358 -365
  96. package/src/device/lock.ts +263 -179
  97. package/src/device/meter.ts +302 -253
  98. package/src/device/meterplus.ts +319 -194
  99. package/src/device/motion.ts +230 -199
  100. package/src/device/plug.ts +273 -274
  101. package/src/device/striplight.ts +460 -316
  102. package/src/irdevice/airconditioner.ts +181 -183
  103. package/src/irdevice/airpurifier.ts +70 -60
  104. package/src/irdevice/camera.ts +46 -36
  105. package/src/irdevice/fan.ts +64 -49
  106. package/src/irdevice/light.ts +47 -51
  107. package/src/irdevice/other.ts +46 -36
  108. package/src/irdevice/tv.ts +64 -51
  109. package/src/irdevice/vacuumcleaner.ts +52 -45
  110. package/src/irdevice/waterheater.ts +50 -45
  111. package/src/platform.ts +44 -68
  112. package/src/settings.ts +3 -3
@@ -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.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
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 (!device.ble) {
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 (device.ble && !this.batteryService) {
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.SwitchToOpenAPI || !this.device.ble) {
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
- await this.BLEparseStatus();
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} BLE parseStatus`);
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
- if (this.device.ble) {
175
- this.SwitchToOpenAPI = false;
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.platform.config.credentials?.token) {
178
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI parseStatus`);
179
- // Contact State
180
- if (this.openState === 'open') {
181
- this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED;
182
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
183
- }
184
- else if (this.openState === 'close') {
185
- this.ContactSensorState = this.platform.Characteristic.ContactSensorState.CONTACT_DETECTED;
186
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensorState}`);
187
- }
188
- else {
189
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openState: ${this.openState}`);
190
- }
191
- // Motion State
192
- if (!this.device.contact?.hide_motionsensor) {
193
- this.MotionDetected = this.moveDetected;
194
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
195
- }
196
- // Light Level
197
- if (!this.device.contact?.hide_lightsensor) {
198
- this.set_minLux = this.minLux();
199
- this.set_maxLux = this.maxLux();
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.device.ble) {
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} BLE refreshStatus`);
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
- if (this.platform.config.credentials?.token && this.OpenAPI) {
328
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI refreshStatus`);
329
- try {
330
- const t = Date.now();
331
- const nonce = 'requestID';
332
- const data = this.platform.config.credentials?.token + t + nonce;
333
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
334
- const sign = signTerm.toString('base64');
335
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
336
- const options = {
337
- hostname: settings_1.HostDomain,
338
- port: 443,
339
- path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
340
- method: 'GET',
341
- headers: {
342
- Authorization: this.platform.config.credentials?.token,
343
- sign: sign,
344
- nonce: nonce,
345
- t: t,
346
- 'Content-Type': 'application/json',
347
- },
348
- };
349
- const req = https_1.default.request(options, (res) => {
350
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
351
- let rawData = '';
352
- res.on('data', (d) => {
353
- rawData += d;
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
- req.on('error', (e) => {
372
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
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
- req.end();
375
- }
376
- catch (e) {
377
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
378
- if (this.deviceLogging.includes('debug')) {
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
- else {
386
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI is disabled, commands will not be sent.`);
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.device.ble) {
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 apiError(e) {
437
- this.contactSensorservice.updateCharacteristic(this.platform.Characteristic.ContactSensorState, e);
438
- if (!this.device.contact?.hide_motionsensor) {
439
- this.motionService?.updateCharacteristic(this.platform.Characteristic.MotionDetected, e);
440
- }
441
- if (!this.device.contact?.hide_lightsensor) {
442
- this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
443
- }
444
- if (this.device.ble) {
445
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
446
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
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 config(device) {
451
- let config = {};
452
- if (device.contact) {
453
- config = device.contact;
454
- }
455
- if (device.ble) {
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
- async refreshRate(device) {
475
- if (device.refreshRate) {
476
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
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 if (this.platform.config.options.refreshRate) {
480
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
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
- async openAPI() {
485
- if (!this.device.openAPI) {
486
- this.OpenAPI = true;
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.OpenAPI = this.device.openAPI;
446
+ this.set_maxLux = 6001;
490
447
  }
491
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI: ${this.OpenAPI}`);
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 (device.ble) {
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.device.ble) {
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 logs(device) {
508
- if (this.platform.debugMode) {
509
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
510
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
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
- else if (device.logging) {
513
- this.deviceLogging = this.accessory.context.logging = device.logging;
514
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
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
- else if (this.platform.config.options?.logging) {
517
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
518
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
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
- else {
521
- this.deviceLogging = this.accessory.context.logging = 'standard';
522
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
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
- minLux() {
543
- if (this.device.contact?.set_minLux) {
544
- this.set_minLux = this.device.contact.set_minLux;
535
+ async context() {
536
+ if (this.MotionDetected === undefined) {
537
+ this.MotionDetected = false;
545
538
  }
546
539
  else {
547
- this.set_minLux = 1;
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
- maxLux() {
552
- if (this.device.contact?.set_maxLux) {
553
- this.set_maxLux = this.device.contact.set_maxLux;
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.set_maxLux = 6001;
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