@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 Motion {
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.MotionDetected = false;
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.doMotionUpdate = new rxjs_1.Subject();
32
34
  this.motionUbpdateInProgress = false;
@@ -70,12 +72,12 @@ class Motion {
70
72
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
71
73
  }
72
74
  // Battery Service
73
- if (!device.ble) {
75
+ if (!this.BLE) {
74
76
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Battery Service`);
75
77
  this.batteryService = this.accessory.getService(this.platform.Service.Battery);
76
78
  accessory.removeService(this.batteryService);
77
79
  }
78
- else if (device.ble && !this.batteryService) {
80
+ else if (this.BLE && !this.batteryService) {
79
81
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
80
82
  (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
81
83
  `${accessory.displayName} Battery`;
@@ -97,15 +99,20 @@ class Motion {
97
99
  * Parse the device status from the SwitchBot api
98
100
  */
99
101
  async parseStatus() {
100
- if (this.SwitchToOpenAPI || !this.device.ble) {
102
+ if (this.BLE && !this.SwitchToOpenAPI) {
103
+ await this.BLEparseStatus();
104
+ }
105
+ else if (this.OpenAPI && this.platform.config.credentials?.token) {
106
+ this.SwitchToOpenAPI = false;
101
107
  await this.openAPIparseStatus();
102
108
  }
103
109
  else {
104
- await this.BLEparseStatus();
110
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
111
+ + ` ${this.device.connectionType}, parseStatus will not happen.`);
105
112
  }
106
113
  }
107
114
  async BLEparseStatus() {
108
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE parseStatus`);
115
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
109
116
  // Movement
110
117
  this.MotionDetected = this.movement;
111
118
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
@@ -139,43 +146,42 @@ class Motion {
139
146
  + ` StatusLowBattery: ${this.StatusLowBattery}`);
140
147
  }
141
148
  async openAPIparseStatus() {
142
- if (this.device.ble) {
143
- this.SwitchToOpenAPI = false;
144
- }
145
- if (this.platform.config.credentials?.token) {
146
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI parseStatus`);
147
- // Motion State
148
- this.MotionDetected = this.moveDetected;
149
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
150
- // Light Level
151
- if (!this.device.motion?.hide_lightsensor) {
152
- this.set_minLux = this.minLux();
153
- this.set_maxLux = this.maxLux();
154
- switch (this.brightness) {
155
- case 'dim':
156
- this.CurrentAmbientLightLevel = this.set_minLux;
157
- break;
158
- case 'bright':
159
- default:
160
- this.CurrentAmbientLightLevel = this.set_maxLux;
161
- }
162
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
149
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
150
+ // Motion State
151
+ this.MotionDetected = this.moveDetected;
152
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
153
+ // Light Level
154
+ if (!this.device.motion?.hide_lightsensor) {
155
+ this.set_minLux = this.minLux();
156
+ this.set_maxLux = this.maxLux();
157
+ switch (this.brightness) {
158
+ case 'dim':
159
+ this.CurrentAmbientLightLevel = this.set_minLux;
160
+ break;
161
+ case 'bright':
162
+ default:
163
+ this.CurrentAmbientLightLevel = this.set_maxLux;
163
164
  }
165
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
164
166
  }
165
167
  }
166
168
  /**
167
169
  * Asks the SwitchBot API for the latest device information
168
170
  */
169
171
  async refreshStatus() {
170
- if (this.device.ble) {
172
+ if (this.BLE) {
171
173
  await this.BLERefreshStatus();
172
174
  }
173
- else {
175
+ else if (this.OpenAPI && this.platform.config.credentials?.token) {
174
176
  await this.openAPIRefreshStatus();
175
177
  }
178
+ else {
179
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
180
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
181
+ }
176
182
  }
177
183
  async BLERefreshStatus() {
178
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE RefreshStatus`);
184
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
179
185
  const switchbot = await this.platform.connectBLE();
180
186
  // Convert to BLE Address
181
187
  this.device.bleMac =
@@ -234,108 +240,69 @@ class Motion {
234
240
  }
235
241
  })
236
242
  .catch(async (e) => {
237
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
238
- if (this.deviceLogging.includes('debug')) {
239
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection,` +
240
- ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
241
- }
242
- if (this.platform.config.credentials?.token) {
243
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
244
- this.SwitchToOpenAPI = true;
245
- await this.openAPIRefreshStatus();
246
- }
247
243
  this.apiError(e);
244
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
245
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
246
+ await this.BLEconnection(switchbot);
248
247
  });
249
248
  }
250
249
  else {
251
250
  await this.BLEconnection(switchbot);
252
251
  }
253
252
  }
254
- async getCustomBLEAddress(switchbot) {
255
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
256
- (async () => {
257
- // Start to monitor advertisement packets
258
- await switchbot.startScan({
259
- model: 's',
260
- });
261
- // Set an event handler
262
- switchbot.onadvertisement = (ad) => {
263
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
264
- };
265
- await switchbot.wait(10000);
266
- // Stop to monitor
267
- switchbot.stopScan();
268
- })();
269
- }
270
- }
271
- async BLEconnection(switchbot) {
272
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
273
- if (this.platform.config.credentials?.token) {
274
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
275
- this.SwitchToOpenAPI = true;
276
- await this.openAPIRefreshStatus();
277
- }
278
- }
279
253
  async openAPIRefreshStatus() {
280
- if (this.platform.config.credentials?.token && this.OpenAPI) {
281
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI RefreshStatus`);
282
- try {
283
- const t = Date.now();
284
- const nonce = 'requestID';
285
- const data = this.platform.config.credentials?.token + t + nonce;
286
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
287
- const sign = signTerm.toString('base64');
288
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
289
- const options = {
290
- hostname: settings_1.HostDomain,
291
- port: 443,
292
- path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
293
- method: 'GET',
294
- headers: {
295
- Authorization: this.platform.config.credentials?.token,
296
- sign: sign,
297
- nonce: nonce,
298
- t: t,
299
- 'Content-Type': 'application/json',
300
- },
301
- };
302
- const req = https_1.default.request(options, (res) => {
303
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
304
- let rawData = '';
305
- res.on('data', (d) => {
306
- rawData += d;
307
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
308
- });
309
- res.on('end', () => {
310
- try {
311
- this.deviceStatus = JSON.parse(rawData);
312
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
313
- this.moveDetected = this.deviceStatus.body.moveDetected;
314
- this.brightness = this.deviceStatus.body.brightness;
315
- this.parseStatus();
316
- this.updateHomeKitCharacteristics();
317
- }
318
- catch (e) {
319
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
320
- }
321
- });
254
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
255
+ try {
256
+ const t = Date.now();
257
+ const nonce = 'requestID';
258
+ const data = this.platform.config.credentials?.token + t + nonce;
259
+ const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
260
+ const sign = signTerm.toString('base64');
261
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
262
+ const options = {
263
+ hostname: settings_1.HostDomain,
264
+ port: 443,
265
+ path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
266
+ method: 'GET',
267
+ headers: {
268
+ Authorization: this.platform.config.credentials?.token,
269
+ sign: sign,
270
+ nonce: nonce,
271
+ t: t,
272
+ 'Content-Type': 'application/json',
273
+ },
274
+ };
275
+ const req = https_1.default.request(options, (res) => {
276
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
277
+ this.statusCode({ res });
278
+ let rawData = '';
279
+ res.on('data', (d) => {
280
+ rawData += d;
281
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
322
282
  });
323
- req.on('error', (e) => {
324
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
283
+ res.on('end', () => {
284
+ try {
285
+ this.deviceStatus = JSON.parse(rawData);
286
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
287
+ this.moveDetected = this.deviceStatus.body.moveDetected;
288
+ this.brightness = this.deviceStatus.body.brightness;
289
+ this.parseStatus();
290
+ this.updateHomeKitCharacteristics();
291
+ }
292
+ catch (e) {
293
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
294
+ }
325
295
  });
326
- req.end();
327
- }
328
- catch (e) {
329
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
330
- if (this.deviceLogging.includes('debug')) {
331
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,` +
332
- ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
333
- }
334
- this.apiError(e);
335
- }
296
+ });
297
+ req.on('error', (e) => {
298
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
299
+ });
300
+ req.end();
336
301
  }
337
- else {
338
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI is disabled, commands will not be sent.`);
302
+ catch (e) {
303
+ this.apiError(e);
304
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
305
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
339
306
  }
340
307
  }
341
308
  /**
@@ -346,14 +313,16 @@ class Motion {
346
313
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
347
314
  }
348
315
  else {
316
+ this.accessory.context.MotionDetected = this.MotionDetected;
349
317
  this.motionSensorService.updateCharacteristic(this.platform.Characteristic.MotionDetected, this.MotionDetected);
350
318
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
351
319
  }
352
- if (this.device.ble) {
320
+ if (this.BLE) {
353
321
  if (this.BatteryLevel === undefined) {
354
322
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
355
323
  }
356
324
  else {
325
+ this.accessory.context.BatteryLevel = this.BatteryLevel;
357
326
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
358
327
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
359
328
  }
@@ -361,6 +330,7 @@ class Motion {
361
330
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
362
331
  }
363
332
  else {
333
+ this.accessory.context.StatusLowBattery = this.StatusLowBattery;
364
334
  this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
365
335
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
366
336
  }
@@ -368,97 +338,117 @@ class Motion {
368
338
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
369
339
  }
370
340
  else {
341
+ this.accessory.context.CurrentAmbientLightLevel = this.CurrentAmbientLightLevel;
371
342
  this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
372
343
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
373
344
  + ` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
374
345
  }
375
346
  }
376
347
  }
377
- async apiError(e) {
378
- this.motionSensorService.updateCharacteristic(this.platform.Characteristic.MotionDetected, e);
379
- if (this.device.ble) {
380
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
381
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
382
- this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
348
+ async getCustomBLEAddress(switchbot) {
349
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
350
+ (async () => {
351
+ // Start to monitor advertisement packets
352
+ await switchbot.startScan({
353
+ model: 's',
354
+ });
355
+ // Set an event handler
356
+ switchbot.onadvertisement = (ad) => {
357
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
358
+ };
359
+ await switchbot.wait(10000);
360
+ // Stop to monitor
361
+ switchbot.stopScan();
362
+ })();
383
363
  }
384
- //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
385
364
  }
386
- async config(device) {
387
- let config = {};
388
- if (device.motion) {
389
- config = device.motion;
390
- }
391
- if (device.ble !== undefined) {
392
- config['ble'] = device.ble;
393
- }
394
- if (device.openAPI !== undefined) {
395
- config['openAPI'] = device.openAPI;
396
- }
397
- if (device.logging !== undefined) {
398
- config['logging'] = device.logging;
399
- }
400
- if (device.refreshRate !== undefined) {
401
- config['refreshRate'] = device.refreshRate;
402
- }
403
- if (device.scanDuration !== undefined) {
404
- config['scanDuration'] = device.scanDuration;
405
- }
406
- if (device.offline !== undefined) {
407
- config['offline'] = device.offline;
408
- }
409
- if (Object.entries(config).length !== 0) {
410
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
365
+ async BLEconnection(switchbot) {
366
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
367
+ if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
368
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
369
+ this.SwitchToOpenAPI = true;
370
+ await this.openAPIRefreshStatus();
411
371
  }
412
372
  }
413
- async refreshRate(device) {
414
- if (device.refreshRate) {
415
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
416
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
373
+ minLux() {
374
+ if (this.device.motion?.set_minLux) {
375
+ this.set_minLux = this.device.motion.set_minLux;
417
376
  }
418
- else if (this.platform.config.options.refreshRate) {
419
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
420
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
377
+ else {
378
+ this.set_minLux = 1;
421
379
  }
380
+ return this.set_minLux;
422
381
  }
423
- async openAPI() {
424
- if (!this.device.openAPI) {
425
- this.OpenAPI = true;
382
+ maxLux() {
383
+ if (this.device.motion?.set_maxLux) {
384
+ this.set_maxLux = this.device.motion.set_maxLux;
426
385
  }
427
386
  else {
428
- this.OpenAPI = this.device.openAPI;
387
+ this.set_maxLux = 6001;
429
388
  }
430
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI: ${this.OpenAPI}`);
389
+ return this.set_maxLux;
431
390
  }
432
391
  async scan(device) {
433
392
  if (device.scanDuration) {
434
393
  this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
435
- if (device.ble) {
394
+ if (this.BLE) {
436
395
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
437
396
  }
438
397
  }
439
398
  else {
440
399
  this.scanDuration = this.accessory.context.scanDuration = 1;
441
- if (this.device.ble) {
400
+ if (this.BLE) {
442
401
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
443
402
  }
444
403
  }
445
404
  }
446
- async logs(device) {
447
- if (this.platform.debugMode) {
448
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
449
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
450
- }
451
- else if (device.logging) {
452
- this.deviceLogging = this.accessory.context.logging = device.logging;
453
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
405
+ async statusCode({ res }) {
406
+ switch (res.statusCode) {
407
+ case 151:
408
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this device type.`);
409
+ break;
410
+ case 152:
411
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found.`);
412
+ break;
413
+ case 160:
414
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported.`);
415
+ break;
416
+ case 161:
417
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline.`);
418
+ this.offlineOff();
419
+ break;
420
+ case 171:
421
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} is offline. Hub: ${this.device.hubDeviceId}`);
422
+ this.offlineOff();
423
+ break;
424
+ case 190:
425
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
426
+ ` Or command: ${(0, super_stringify_1.default)(res)} format is invalid`);
427
+ break;
428
+ case 100:
429
+ if (this.platform.debugMode) {
430
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent.`);
431
+ }
432
+ break;
433
+ default:
434
+ if (this.platform.debugMode) {
435
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode.`);
436
+ }
454
437
  }
455
- else if (this.platform.config.options?.logging) {
456
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
457
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
438
+ }
439
+ async offlineOff() {
440
+ if (this.device.offline) {
441
+ this.MotionDetected = false;
442
+ this.motionSensorService.getCharacteristic(this.platform.Characteristic.MotionDetected).updateValue(this.MotionDetected);
443
+ await this.updateHomeKitCharacteristics();
458
444
  }
459
- else {
460
- this.deviceLogging = this.accessory.context.logging = 'standard';
461
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
445
+ }
446
+ async apiError(e) {
447
+ this.motionSensorService.updateCharacteristic(this.platform.Characteristic.MotionDetected, e);
448
+ if (this.BLE) {
449
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
450
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
451
+ this.lightSensorService?.updateCharacteristic(this.platform.Characteristic.CurrentAmbientLightLevel, e);
462
452
  }
463
453
  }
464
454
  FirmwareRevision(accessory, device) {
@@ -478,23 +468,65 @@ class Motion {
478
468
  }
479
469
  return FirmwareRevision;
480
470
  }
481
- minLux() {
482
- if (this.device.motion?.set_minLux) {
483
- this.set_minLux = this.device.motion.set_minLux;
471
+ async context() {
472
+ if (this.MotionDetected === undefined) {
473
+ this.MotionDetected = false;
484
474
  }
485
475
  else {
486
- this.set_minLux = 1;
476
+ this.MotionDetected = this.accessory.context.MotionDetected;
487
477
  }
488
- return this.set_minLux;
489
478
  }
490
- maxLux() {
491
- if (this.device.motion?.set_maxLux) {
492
- this.set_maxLux = this.device.motion.set_maxLux;
479
+ async refreshRate(device) {
480
+ if (device.refreshRate) {
481
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
482
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
483
+ }
484
+ else if (this.platform.config.options.refreshRate) {
485
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
486
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
487
+ }
488
+ }
489
+ async config(device) {
490
+ let config = {};
491
+ if (device.motion) {
492
+ config = device.motion;
493
+ }
494
+ if (device.connectionType !== undefined) {
495
+ config['connectionType'] = device.connectionType;
496
+ }
497
+ if (device.logging !== undefined) {
498
+ config['logging'] = device.logging;
499
+ }
500
+ if (device.refreshRate !== undefined) {
501
+ config['refreshRate'] = device.refreshRate;
502
+ }
503
+ if (device.scanDuration !== undefined) {
504
+ config['scanDuration'] = device.scanDuration;
505
+ }
506
+ if (device.offline !== undefined) {
507
+ config['offline'] = device.offline;
508
+ }
509
+ if (Object.entries(config).length !== 0) {
510
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
511
+ }
512
+ }
513
+ async logs(device) {
514
+ if (this.platform.debugMode) {
515
+ this.deviceLogging = this.accessory.context.logging = 'debugMode';
516
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
517
+ }
518
+ else if (device.logging) {
519
+ this.deviceLogging = this.accessory.context.logging = device.logging;
520
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
521
+ }
522
+ else if (this.platform.config.options?.logging) {
523
+ this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
524
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
493
525
  }
494
526
  else {
495
- this.set_maxLux = 6001;
527
+ this.deviceLogging = this.accessory.context.logging = 'standard';
528
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
496
529
  }
497
- return this.set_maxLux;
498
530
  }
499
531
  /**
500
532
  * Logging for Device