@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,30 +20,18 @@ class Bot {
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.PressOrSwitch(device);
29
31
  this.allowPushChanges(device);
30
32
  this.config(device);
31
- if (this.On === undefined) {
32
- this.On = false;
33
- this.accessory.context.On = this.On;
34
- }
35
- else {
36
- this.On = this.accessory.context.On;
37
- }
38
- if (device.bot?.doublePress) {
39
- this.doublePress = device.bot?.doublePress;
40
- this.accessory.context.doublePress = this.doublePress;
41
- }
42
- else {
43
- this.doublePress = 1;
44
- }
45
- this.BatteryLevel = 100;
46
- this.StatusLowBattery = 0;
33
+ this.context();
34
+ this.DoublePress(device);
47
35
  // this is subject we use to track when we need to POST changes to the SwitchBot API
48
36
  this.doBotUpdate = new rxjs_1.Subject();
49
37
  this.botUpdateInProgress = false;
@@ -75,7 +63,7 @@ class Bot {
75
63
  `${accessory.displayName} Switch`;
76
64
  this.infoLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Switch`);
77
65
  this.switchService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
78
- this.switchService.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
66
+ this.switchService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
79
67
  }
80
68
  else if (device.bot?.deviceType === 'garagedoor') {
81
69
  this.removeFanService(accessory);
@@ -93,7 +81,7 @@ class Bot {
93
81
  `${accessory.displayName} Garage Door Opener`;
94
82
  this.infoLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Garage Door Opener`);
95
83
  this.garageDoorService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
96
- this.garageDoorService.getCharacteristic(this.platform.Characteristic.TargetDoorState).onSet(this.handleOnSet.bind(this));
84
+ this.garageDoorService.getCharacteristic(this.platform.Characteristic.TargetDoorState).onSet(this.OnSet.bind(this));
97
85
  this.garageDoorService.setCharacteristic(this.platform.Characteristic.ObstructionDetected, false);
98
86
  }
99
87
  else if (device.bot?.deviceType === 'door') {
@@ -119,7 +107,7 @@ class Bot {
119
107
  maxValue: 100,
120
108
  minStep: 100,
121
109
  })
122
- .onSet(this.handleOnSet.bind(this));
110
+ .onSet(this.OnSet.bind(this));
123
111
  this.doorService.setCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
124
112
  }
125
113
  else if (device.bot?.deviceType === 'window') {
@@ -145,7 +133,7 @@ class Bot {
145
133
  maxValue: 100,
146
134
  minStep: 100,
147
135
  })
148
- .onSet(this.handleOnSet.bind(this));
136
+ .onSet(this.OnSet.bind(this));
149
137
  this.windowService.setCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
150
138
  }
151
139
  else if (device.bot?.deviceType === 'windowcovering') {
@@ -172,7 +160,7 @@ class Bot {
172
160
  maxValue: 100,
173
161
  minStep: 100,
174
162
  })
175
- .onSet(this.handleOnSet.bind(this));
163
+ .onSet(this.OnSet.bind(this));
176
164
  this.windowCoveringService.setCharacteristic(this.platform.Characteristic.PositionState, this.platform.Characteristic.PositionState.STOPPED);
177
165
  }
178
166
  else if (device.bot?.deviceType === 'lock') {
@@ -190,7 +178,7 @@ class Bot {
190
178
  `${accessory.displayName} Lock`;
191
179
  this.infoLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Lock`);
192
180
  this.lockService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
193
- this.lockService.getCharacteristic(this.platform.Characteristic.LockTargetState).onSet(this.handleOnSet.bind(this));
181
+ this.lockService.getCharacteristic(this.platform.Characteristic.LockTargetState).onSet(this.OnSet.bind(this));
194
182
  }
195
183
  else if (device.bot?.deviceType === 'faucet') {
196
184
  this.removeFanService(accessory);
@@ -207,7 +195,7 @@ class Bot {
207
195
  `${accessory.displayName} Faucet`;
208
196
  this.infoLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Faucet`);
209
197
  this.faucetService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
210
- this.faucetService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.handleOnSet.bind(this));
198
+ this.faucetService.getCharacteristic(this.platform.Characteristic.Active).onSet(this.OnSet.bind(this));
211
199
  }
212
200
  else if (device.bot?.deviceType === 'fan') {
213
201
  this.removeLockService(accessory);
@@ -224,7 +212,7 @@ class Bot {
224
212
  `${accessory.displayName} Fan`;
225
213
  this.infoLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Fan`);
226
214
  this.fanService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
227
- this.fanService.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
215
+ this.fanService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
228
216
  }
229
217
  else if (device.bot?.deviceType === 'stateful') {
230
218
  this.removeFanService(accessory);
@@ -245,7 +233,7 @@ class Bot {
245
233
  this.statefulProgrammableSwitchService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
246
234
  this.statefulProgrammableSwitchService
247
235
  .getCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState)
248
- .onSet(this.handleOnSet.bind(this));
236
+ .onSet(this.OnSet.bind(this));
249
237
  }
250
238
  else {
251
239
  this.removeFanService(accessory);
@@ -262,15 +250,15 @@ class Bot {
262
250
  `${accessory.displayName} Outlet`;
263
251
  this.infoLog(`${this.device.deviceType}: ${accessory.displayName} Displaying as Outlet`);
264
252
  this.outletService.setCharacteristic(this.platform.Characteristic.Name, accessory.displayName);
265
- this.outletService.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
253
+ this.outletService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
266
254
  }
267
255
  // Battery Service
268
- if (!device.ble) {
256
+ if (!this.BLE) {
269
257
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Battery Service`);
270
258
  this.batteryService = this.accessory.getService(this.platform.Service.Battery);
271
259
  accessory.removeService(this.batteryService);
272
260
  }
273
- else if (device.ble && !this.batteryService) {
261
+ else if (this.BLE && !this.batteryService) {
274
262
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
275
263
  (this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
276
264
  `${accessory.displayName} Battery`;
@@ -288,28 +276,23 @@ class Bot {
288
276
  await this.refreshStatus();
289
277
  });
290
278
  // Watch for Bot change events
291
- // We put in a debounce of 100ms so we don't make duplicate calls
279
+ // We put in a debounce of 1000ms so we don't make duplicate calls
292
280
  this.doBotUpdate
293
281
  .pipe((0, operators_1.tap)(() => {
294
282
  this.botUpdateInProgress = true;
295
- }), (0, operators_1.debounceTime)(100))
283
+ }), (0, operators_1.debounceTime)(this.platform.config.options.pushRate * 1000))
296
284
  .subscribe(async () => {
297
285
  try {
298
- (0, rxjs_1.interval)(100)
286
+ (0, rxjs_1.interval)(15000)
299
287
  .pipe((0, operators_1.take)(this.doublePress))
300
288
  .subscribe(async () => {
301
289
  await this.pushChanges();
302
290
  });
303
291
  }
304
292
  catch (e) {
305
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges`);
306
- if (this.deviceLogging.includes('debug')) {
307
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges,`
308
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
309
- }
310
- if (this.platform.debugMode) {
311
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges,` + ` Error: ${(0, super_stringify_1.default)(e)}`);
312
- }
293
+ this.apiError(e);
294
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
295
+ + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
313
296
  }
314
297
  this.botUpdateInProgress = false;
315
298
  });
@@ -318,15 +301,20 @@ class Bot {
318
301
  * Parse the device status from the SwitchBot api
319
302
  */
320
303
  async parseStatus() {
321
- if (this.SwitchToOpenAPI || !this.device.ble) {
304
+ if (this.BLE && !this.SwitchToOpenAPI) {
305
+ await this.BLEparseStatus();
306
+ }
307
+ else if (this.OpenAPI && this.platform.config.credentials?.token) {
308
+ this.SwitchToOpenAPI = false;
322
309
  await this.openAPIparseStatus();
323
310
  }
324
311
  else {
325
- await this.BLEparseStatus();
312
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
313
+ + ` ${this.device.connectionType}, parseStatus will not happen.`);
326
314
  }
327
315
  }
328
316
  async BLEparseStatus() {
329
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE parseStatus`);
317
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
330
318
  // BLEmode (true if Switch Mode) | (false if Press Mode)
331
319
  if (this.mode) {
332
320
  this.accessory.context.On = this.On;
@@ -353,37 +341,36 @@ class Bot {
353
341
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
354
342
  }
355
343
  async openAPIparseStatus() {
356
- if (this.device.ble) {
357
- this.SwitchToOpenAPI = false;
344
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
345
+ if (this.botMode === 'press') {
346
+ this.On = false;
347
+ this.accessory.context.On = this.On;
358
348
  }
359
- if (this.platform.config.credentials?.token) {
360
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI parseStatus`);
361
- if (this.botMode === 'press') {
349
+ else {
350
+ this.accessory.context.On = this.On;
351
+ if (this.On === undefined) {
362
352
  this.On = false;
363
- this.accessory.context.On = this.On;
364
- }
365
- else {
366
- this.accessory.context.On = this.On;
367
- if (this.On === undefined) {
368
- this.On = false;
369
- }
370
353
  }
371
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
372
354
  }
355
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
373
356
  }
374
357
  /**
375
358
  * Asks the SwitchBot API for the latest device information
376
359
  */
377
360
  async refreshStatus() {
378
- if (this.device.ble) {
361
+ if (this.BLE) {
379
362
  await this.BLERefreshStatus();
380
363
  }
381
- else {
364
+ else if (this.OpenAPI && this.platform.config.credentials?.token) {
382
365
  await this.openAPIRefreshStatus();
383
366
  }
367
+ else {
368
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
369
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
370
+ }
384
371
  }
385
372
  async BLERefreshStatus() {
386
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE refreshStatus`);
373
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
387
374
  const switchbot = await this.platform.connectBLE();
388
375
  // Convert to BLE Address
389
376
  this.device.bleMac = this.device
@@ -393,153 +380,114 @@ class Bot {
393
380
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
394
381
  this.getCustomBLEAddress(switchbot);
395
382
  // Start to monitor advertisement packets
396
- if (switchbot === false) {
397
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection: ${switchbot}`);
398
- }
399
- switchbot
400
- .startScan({
401
- model: 'H',
402
- id: this.device.bleMac,
403
- })
404
- .then(() => {
405
- // Set an event hander
406
- switchbot.onadvertisement = (ad) => {
407
- this.address = ad.address;
408
- if (this.deviceLogging.includes('debug')) {
409
- this.infoLog(this.address);
410
- this.infoLog(this.device.bleMac);
411
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
412
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
413
- }
414
- this.serviceData = ad.serviceData;
415
- this.mode = ad.serviceData.mode;
416
- this.state = ad.serviceData.state;
417
- this.battery = ad.serviceData.battery;
418
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
419
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},` +
420
- ` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
421
- if (this.serviceData) {
422
- this.connected = true;
423
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
424
- }
425
- else {
426
- this.connected = false;
427
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
428
- }
429
- };
430
- // Wait 2 seconds
431
- return switchbot.wait(this.scanDuration * 1000);
432
- })
433
- .then(async () => {
434
- // Stop to monitor
435
- switchbot.stopScan();
436
- if (this.connected) {
437
- this.parseStatus();
438
- this.updateHomeKitCharacteristics();
439
- }
440
- else {
441
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection`);
442
- if (this.platform.config.credentials?.token) {
443
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
444
- this.SwitchToOpenAPI = true;
445
- await this.openAPIRefreshStatus();
446
- }
447
- }
448
- })
449
- .catch(async (e) => {
450
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
451
- if (this.deviceLogging.includes('debug')) {
452
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
453
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
454
- }
455
- if (this.platform.config.credentials?.token) {
456
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
457
- this.SwitchToOpenAPI = true;
458
- await this.openAPIRefreshStatus();
459
- }
460
- this.apiError(e);
461
- });
462
- }
463
- async getCustomBLEAddress(switchbot) {
464
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
465
- (async () => {
466
- // Start to monitor advertisement packets
467
- await switchbot.startScan({
468
- model: 'H',
469
- });
470
- // Set an event handler
383
+ if (switchbot !== false) {
384
+ switchbot
385
+ .startScan({
386
+ model: 'H',
387
+ id: this.device.bleMac,
388
+ })
389
+ .then(() => {
390
+ // Set an event hander
471
391
  switchbot.onadvertisement = (ad) => {
472
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
392
+ this.address = ad.address;
393
+ if (this.deviceLogging.includes('debug')) {
394
+ this.infoLog(this.address);
395
+ this.infoLog(this.device.bleMac);
396
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address Found: ${this.address}`);
397
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac}`);
398
+ }
399
+ this.serviceData = ad.serviceData;
400
+ this.mode = ad.serviceData.mode;
401
+ this.state = ad.serviceData.state;
402
+ this.battery = ad.serviceData.battery;
403
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${(0, super_stringify_1.default)(ad.serviceData)}`);
404
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}, model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName},` +
405
+ ` mode: ${ad.serviceData.mode}, state: ${ad.serviceData.state}, battery: ${ad.serviceData.battery}`);
406
+ if (this.serviceData) {
407
+ this.connected = true;
408
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
409
+ }
410
+ else {
411
+ this.connected = false;
412
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.connected}`);
413
+ }
473
414
  };
474
- await switchbot.wait(10000);
415
+ // Wait 2 seconds
416
+ return switchbot.wait(this.scanDuration * 1000);
417
+ })
418
+ .then(async () => {
475
419
  // Stop to monitor
476
420
  switchbot.stopScan();
477
- })();
421
+ if (this.connected) {
422
+ this.parseStatus();
423
+ this.updateHomeKitCharacteristics();
424
+ }
425
+ else {
426
+ await this.BLEconnection(switchbot);
427
+ }
428
+ })
429
+ .catch(async (e) => {
430
+ this.apiError(e);
431
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
432
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
433
+ await this.BLEconnection(switchbot);
434
+ });
435
+ }
436
+ else {
437
+ await this.BLEconnection(switchbot);
478
438
  }
479
439
  }
480
440
  async openAPIRefreshStatus() {
481
- if (this.platform.config.credentials?.token && this.OpenAPI) {
482
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI refreshStatus`);
483
- try {
484
- const t = Date.now();
485
- const nonce = 'requestID';
486
- const data = this.platform.config.credentials?.token + t + nonce;
487
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
488
- const sign = signTerm.toString('base64');
489
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
490
- const options = {
491
- hostname: settings_1.HostDomain,
492
- port: 443,
493
- path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
494
- method: 'GET',
495
- headers: {
496
- Authorization: this.platform.config.credentials?.token,
497
- sign: sign,
498
- nonce: nonce,
499
- t: t,
500
- 'Content-Type': 'application/json',
501
- },
502
- };
503
- const req = https_1.default.request(options, (res) => {
504
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Refresh statusCode: ${res.statusCode}`);
505
- let rawData = '';
506
- res.on('data', (d) => {
507
- rawData += d;
508
- this.debugLog(`d: ${d}`);
509
- });
510
- res.on('end', () => {
511
- try {
512
- this.deviceStatus = JSON.parse(rawData);
513
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
514
- this.power = this.deviceStatus.body.power;
515
- this.parseStatus();
516
- this.updateHomeKitCharacteristics();
517
- }
518
- catch (e) {
519
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
520
- }
521
- });
441
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
442
+ try {
443
+ const t = Date.now();
444
+ const nonce = 'requestID';
445
+ const data = this.platform.config.credentials?.token + t + nonce;
446
+ const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
447
+ const sign = signTerm.toString('base64');
448
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
449
+ const options = {
450
+ hostname: settings_1.HostDomain,
451
+ port: 443,
452
+ path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
453
+ method: 'GET',
454
+ headers: {
455
+ Authorization: this.platform.config.credentials?.token,
456
+ sign: sign,
457
+ nonce: nonce,
458
+ t: t,
459
+ 'Content-Type': 'application/json',
460
+ },
461
+ };
462
+ const req = https_1.default.request(options, (res) => {
463
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
464
+ let rawData = '';
465
+ res.on('data', (d) => {
466
+ rawData += d;
467
+ this.debugLog(`d: ${d}`);
522
468
  });
523
- req.on('error', (e) => {
524
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
469
+ res.on('end', () => {
470
+ try {
471
+ this.deviceStatus = JSON.parse(rawData);
472
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
473
+ this.power = this.deviceStatus.body.power;
474
+ this.parseStatus();
475
+ this.updateHomeKitCharacteristics();
476
+ }
477
+ catch (e) {
478
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
479
+ }
525
480
  });
526
- req.end();
527
- }
528
- catch (e) {
529
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
530
- if (this.deviceLogging.includes('debug')) {
531
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
532
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
533
- }
534
- if (this.platform.debugMode) {
535
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
536
- + ` Error: ${(0, super_stringify_1.default)(e)}`);
537
- }
538
- this.apiError(e);
539
- }
481
+ });
482
+ req.on('error', (e) => {
483
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
484
+ });
485
+ req.end();
540
486
  }
541
- else {
542
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI is disabled, commands will not be sent.`);
487
+ catch (e) {
488
+ this.apiError(e);
489
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
490
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
543
491
  }
544
492
  }
545
493
  /**
@@ -550,12 +498,16 @@ class Bot {
550
498
  * Bot - "command" "press" "default" = trigger press
551
499
  */
552
500
  async pushChanges() {
553
- if (this.device.ble) {
554
- await this.BLEpushChanges();
501
+ if (this.BLE) {
502
+ await this.BLERefreshStatus();
555
503
  }
556
- else {
504
+ else if (this.OpenAPI) {
557
505
  await this.openAPIpushChanges();
558
506
  }
507
+ else {
508
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
509
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
510
+ }
559
511
  (0, rxjs_1.interval)(5000)
560
512
  .pipe((0, operators_1.take)(1))
561
513
  .subscribe(async () => {
@@ -563,9 +515,9 @@ class Bot {
563
515
  });
564
516
  }
565
517
  async BLEpushChanges() {
566
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE pushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
518
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
567
519
  if (this.On !== this.accessory.context.On || this.allowPush) {
568
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE pushChanges`);
520
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
569
521
  const switchbot = await this.platform.connectBLE();
570
522
  // Convert to BLE Address
571
523
  this.device.bleMac = this.device
@@ -596,20 +548,13 @@ class Bot {
596
548
  }, 500);
597
549
  })
598
550
  .catch(async (e) => {
599
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
600
- if (this.deviceLogging.includes('debug')) {
601
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
602
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
603
- }
604
- if (this.platform.debugMode) {
605
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
606
- + ` Error: ${(0, super_stringify_1.default)(e)}`);
607
- }
608
- if (this.platform.config.credentials?.token) {
551
+ this.apiError(e);
552
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
553
+ + ` Connection & botMode: ${this.botMode}, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
554
+ if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
609
555
  this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
610
556
  await this.openAPIpushChanges();
611
557
  }
612
- this.apiError(e);
613
558
  });
614
559
  }
615
560
  else if (this.botMode === 'switch') {
@@ -625,128 +570,157 @@ class Bot {
625
570
  this.accessory.context.On = this.On;
626
571
  })
627
572
  .catch(async (e) => {
628
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
629
- if (this.deviceLogging.includes('debug')) {
630
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
631
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
632
- }
633
- if (this.platform.debugMode) {
634
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
635
- + ` Error: ${(0, super_stringify_1.default)(e)}`);
636
- }
637
- if (this.platform.config.credentials?.token) {
573
+ this.apiError(e);
574
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
575
+ + ` Connection & botMode: ${this.botMode}, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
576
+ if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
638
577
  this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
639
578
  await this.openAPIpushChanges();
640
579
  }
641
- this.apiError(e);
642
580
  });
643
581
  }
644
582
  else {
645
- throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
583
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
646
584
  }
647
585
  }
648
- this.accessory.context.On = this.On;
586
+ else {
587
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, `
588
+ + `OnCached: ${this.accessory.context.On}`);
589
+ }
649
590
  }
650
- async turnOnOff(device_list) {
651
- return await this.retry({
652
- max: await this.maxRetry(),
653
- fn: () => {
654
- if (this.On) {
655
- return device_list[0].turnOn({ id: this.device.bleMac });
591
+ async openAPIpushChanges() {
592
+ try {
593
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
594
+ if (this.On !== this.accessory.context.On || this.allowPush) {
595
+ // Make Push On request to the API
596
+ const t = Date.now();
597
+ const nonce = 'requestID';
598
+ const data = this.platform.config.credentials?.token + t + nonce;
599
+ const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
600
+ .update(Buffer.from(data, 'utf-8'))
601
+ .digest();
602
+ const sign = signTerm.toString('base64');
603
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
604
+ let command = '';
605
+ if (this.botMode === 'switch' && this.On) {
606
+ command = 'turnOn';
607
+ this.On = true;
608
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
609
+ }
610
+ else if (this.botMode === 'switch' && !this.On) {
611
+ command = 'turnOff';
612
+ this.On = false;
613
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
614
+ }
615
+ else if (this.botMode === 'press') {
616
+ command = 'press';
617
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Press Mode`);
618
+ this.On = false;
656
619
  }
657
620
  else {
658
- return device_list[0].turnOff({ id: this.device.bleMac });
621
+ throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Device Paramters not set for this Bot.`);
659
622
  }
660
- },
661
- });
662
- }
663
- async openAPIpushChanges() {
664
- if (this.platform.config.credentials?.token && this.OpenAPI) {
665
- try {
666
- if (this.On !== this.accessory.context.On || this.allowPush) {
667
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI pushChanges`);
668
- // Make Push On request to the API
669
- const t = Date.now();
670
- const nonce = 'requestID';
671
- const data = this.platform.config.credentials?.token + t + nonce;
672
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret)
673
- .update(Buffer.from(data, 'utf-8'))
674
- .digest();
675
- const sign = signTerm.toString('base64');
676
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
677
- let command = '';
678
- if (this.botMode === 'switch' && this.On) {
679
- command = 'turnOn';
680
- this.On = true;
681
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
682
- }
683
- else if (this.botMode === 'switch' && !this.On) {
684
- command = 'turnOff';
685
- this.On = false;
686
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Switch Mode, Turning ${this.On}`);
687
- }
688
- else if (this.botMode === 'press') {
689
- command = 'press';
690
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Press Mode`);
691
- this.On = false;
692
- }
693
- else {
694
- throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Device Paramters not set for this Bot.`);
695
- }
696
- const body = (0, super_stringify_1.default)({
697
- 'command': `${command}`,
698
- 'parameter': 'default',
699
- 'commandType': 'command',
700
- });
701
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
702
- const options = {
703
- hostname: settings_1.HostDomain,
704
- port: 443,
705
- path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
706
- method: 'POST',
707
- headers: {
708
- 'Authorization': this.platform.config.credentials?.token,
709
- 'sign': sign,
710
- 'nonce': nonce,
711
- 't': t,
712
- 'Content-Type': 'application/json',
713
- 'Content-Length': body.length,
714
- },
715
- };
716
- const req = https_1.default.request(options, res => {
717
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
718
- this.statusCode({ res });
719
- res.on('data', d => {
720
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
721
- });
722
- });
723
- req.on('error', (e) => {
724
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
623
+ const body = (0, super_stringify_1.default)({
624
+ 'command': `${command}`,
625
+ 'parameter': 'default',
626
+ 'commandType': 'command',
627
+ });
628
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API. body: ${body},`);
629
+ const options = {
630
+ hostname: settings_1.HostDomain,
631
+ port: 443,
632
+ path: `${settings_1.DevicePath}/${this.device.deviceId}/commands`,
633
+ method: 'POST',
634
+ headers: {
635
+ 'Authorization': this.platform.config.credentials?.token,
636
+ 'sign': sign,
637
+ 'nonce': nonce,
638
+ 't': t,
639
+ 'Content-Type': 'application/json',
640
+ 'Content-Length': body.length,
641
+ },
642
+ };
643
+ const req = https_1.default.request(options, res => {
644
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
645
+ this.statusCode({ res });
646
+ res.on('data', d => {
647
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
725
648
  });
726
- req.write(body);
727
- req.end();
728
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${(0, super_stringify_1.default)(req)}`);
729
- this.accessory.context.On = this.On;
730
- }
649
+ });
650
+ req.on('error', (e) => {
651
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
652
+ });
653
+ req.write(body);
654
+ req.end();
655
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${(0, super_stringify_1.default)(req)}`);
731
656
  }
732
- catch (e) {
733
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection`);
734
- if (this.deviceLogging.includes('debug')) {
735
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
736
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
737
- }
738
- if (this.platform.debugMode) {
739
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with OpenAPI Connection,`
740
- + ` Error: ${(0, super_stringify_1.default)(e)}`);
741
- }
742
- this.apiError(e);
657
+ else {
658
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
659
+ + `OnCached: ${this.accessory.context.On}`);
743
660
  }
744
661
  }
745
- else {
746
- if (this.deviceLogging.includes('debug')) {
747
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI is disabled, device status will not be sent.`);
662
+ catch (e) {
663
+ this.apiError(e);
664
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
665
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
666
+ }
667
+ }
668
+ /**
669
+ * Handle requests to set the "On" characteristic
670
+ */
671
+ async OnSet(value) {
672
+ if (this.device.bot?.deviceType === 'garagedoor') {
673
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetDoorState: ${value}`);
674
+ if (value === this.platform.Characteristic.TargetDoorState.CLOSED) {
675
+ this.On = false;
676
+ }
677
+ else {
678
+ this.On = true;
679
+ }
680
+ }
681
+ else if (this.device.bot?.deviceType === 'door' ||
682
+ this.device.bot?.deviceType === 'window' ||
683
+ this.device.bot?.deviceType === 'windowcovering') {
684
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set TargetPosition: ${value}`);
685
+ if (value === 0) {
686
+ this.On = false;
687
+ }
688
+ else {
689
+ this.On = true;
690
+ }
691
+ }
692
+ else if (this.device.bot?.deviceType === 'lock') {
693
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set LockTargetState: ${value}`);
694
+ if (value === this.platform.Characteristic.LockTargetState.SECURED) {
695
+ this.On = false;
696
+ }
697
+ else {
698
+ this.On = true;
699
+ }
700
+ }
701
+ else if (this.device.bot?.deviceType === 'faucet') {
702
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set Active: ${value}`);
703
+ if (value === this.platform.Characteristic.Active.INACTIVE) {
704
+ this.On = false;
705
+ }
706
+ else {
707
+ this.On = true;
708
+ }
709
+ }
710
+ else if (this.device.bot?.deviceType === 'stateful') {
711
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set ProgrammableSwitchOutputState: ${value}`);
712
+ if (value === 0) {
713
+ this.On = false;
714
+ }
715
+ else {
716
+ this.On = true;
748
717
  }
749
718
  }
719
+ else {
720
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
721
+ this.On = value;
722
+ }
723
+ this.doBotUpdate.next();
750
724
  }
751
725
  /**
752
726
  * Updates the status for each of the HomeKit Characteristics
@@ -923,7 +897,7 @@ class Bot {
923
897
  }
924
898
  }
925
899
  this.accessory.context.On = this.On;
926
- if (this.device.ble) {
900
+ if (this.BLE) {
927
901
  if (this.BatteryLevel === undefined) {
928
902
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
929
903
  }
@@ -942,52 +916,194 @@ class Bot {
942
916
  }
943
917
  }
944
918
  }
945
- async apiError(e) {
946
- if (this.device.bot?.deviceType === 'garagedoor') {
947
- this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.TargetDoorState, e);
948
- this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.CurrentDoorState, e);
949
- this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.ObstructionDetected, e);
950
- }
951
- else if (this.device.bot?.deviceType === 'door') {
952
- this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
953
- this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
954
- this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
919
+ async removeOutletService(accessory) {
920
+ // If outletService still pressent, then remove first
921
+ this.outletService = this.accessory.getService(this.platform.Service.Outlet);
922
+ if (this.outletService) {
923
+ this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Outlet Service`);
955
924
  }
956
- else if (this.device.bot?.deviceType === 'window') {
957
- this.windowService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
958
- this.windowService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
959
- this.windowService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
925
+ accessory.removeService(this.outletService);
926
+ }
927
+ async removeGarageDoorService(accessory) {
928
+ // If garageDoorService still pressent, then remove first
929
+ this.garageDoorService = this.accessory.getService(this.platform.Service.GarageDoorOpener);
930
+ if (this.garageDoorService) {
931
+ this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Garage Door Service`);
960
932
  }
961
- else if (this.device.bot?.deviceType === 'windowcovering') {
962
- this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
963
- this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
964
- this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
933
+ accessory.removeService(this.garageDoorService);
934
+ }
935
+ async removeDoorService(accessory) {
936
+ // If doorService still pressent, then remove first
937
+ this.doorService = this.accessory.getService(this.platform.Service.Door);
938
+ if (this.doorService) {
939
+ this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Door Service`);
965
940
  }
966
- else if (this.device.bot?.deviceType === 'lock') {
967
- this.doorService?.updateCharacteristic(this.platform.Characteristic.LockTargetState, e);
968
- this.doorService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState, e);
941
+ accessory.removeService(this.doorService);
942
+ }
943
+ async removeLockService(accessory) {
944
+ // If lockService still pressent, then remove first
945
+ this.lockService = this.accessory.getService(this.platform.Service.LockMechanism);
946
+ if (this.lockService) {
947
+ this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Lock Service`);
969
948
  }
970
- else if (this.device.bot?.deviceType === 'faucet') {
971
- this.faucetService?.updateCharacteristic(this.platform.Characteristic.Active, e);
949
+ accessory.removeService(this.lockService);
950
+ }
951
+ async removeFaucetService(accessory) {
952
+ // If faucetService still pressent, then remove first
953
+ this.faucetService = this.accessory.getService(this.platform.Service.Faucet);
954
+ if (this.faucetService) {
955
+ this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Faucet Service`);
972
956
  }
973
- else if (this.device.bot?.deviceType === 'fan') {
974
- this.fanService?.updateCharacteristic(this.platform.Characteristic.On, e);
957
+ accessory.removeService(this.faucetService);
958
+ }
959
+ async removeFanService(accessory) {
960
+ // If fanService still pressent, then remove first
961
+ this.fanService = this.accessory.getService(this.platform.Service.Fan);
962
+ if (this.fanService) {
963
+ this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Fan Service`);
975
964
  }
976
- else if (this.device.bot?.deviceType === 'stateful') {
977
- this.statefulProgrammableSwitchService?.updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent, e);
978
- this.statefulProgrammableSwitchService?.updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState, e);
965
+ accessory.removeService(this.fanService);
966
+ }
967
+ async removeWindowService(accessory) {
968
+ // If windowService still pressent, then remove first
969
+ this.windowService = this.accessory.getService(this.platform.Service.Window);
970
+ if (this.windowService) {
971
+ this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Window Service`);
979
972
  }
980
- else if (this.device.bot?.deviceType === 'switch') {
981
- this.switchService?.updateCharacteristic(this.platform.Characteristic.On, e);
973
+ accessory.removeService(this.windowService);
974
+ }
975
+ async removeWindowCoveringService(accessory) {
976
+ // If windowCoveringService still pressent, then remove first
977
+ this.windowCoveringService = this.accessory.getService(this.platform.Service.WindowCovering);
978
+ if (this.windowCoveringService) {
979
+ this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Window Covering Service`);
980
+ }
981
+ accessory.removeService(this.windowCoveringService);
982
+ }
983
+ async removeStatefulProgrammableSwitchService(accessory) {
984
+ // If statefulProgrammableSwitchService still pressent, then remove first
985
+ this.statefulProgrammableSwitchService = this.accessory.getService(this.platform.Service.StatefulProgrammableSwitch);
986
+ if (this.statefulProgrammableSwitchService) {
987
+ this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Stateful Programmable Switch Service`);
988
+ }
989
+ accessory.removeService(this.statefulProgrammableSwitchService);
990
+ }
991
+ async removeSwitchService(accessory) {
992
+ // If switchService still pressent, then remove first
993
+ this.switchService = this.accessory.getService(this.platform.Service.Switch);
994
+ if (this.switchService) {
995
+ this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Switch Service`);
996
+ }
997
+ accessory.removeService(this.switchService);
998
+ }
999
+ DoublePress(device) {
1000
+ if (device.bot?.doublePress) {
1001
+ this.doublePress = device.bot?.doublePress;
1002
+ this.accessory.context.doublePress = this.doublePress;
982
1003
  }
983
1004
  else {
984
- this.outletService?.updateCharacteristic(this.platform.Characteristic.On, e);
1005
+ this.doublePress = 1;
985
1006
  }
986
- if (this.device.ble) {
987
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
988
- this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
1007
+ }
1008
+ async getCustomBLEAddress(switchbot) {
1009
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
1010
+ (async () => {
1011
+ // Start to monitor advertisement packets
1012
+ await switchbot.startScan({
1013
+ model: 'H',
1014
+ });
1015
+ // Set an event handler
1016
+ switchbot.onadvertisement = (ad) => {
1017
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
1018
+ };
1019
+ await switchbot.wait(10000);
1020
+ // Stop to monitor
1021
+ switchbot.stopScan();
1022
+ })();
1023
+ }
1024
+ }
1025
+ async BLEconnection(switchbot) {
1026
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
1027
+ if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
1028
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
1029
+ this.SwitchToOpenAPI = true;
1030
+ await this.openAPIRefreshStatus();
1031
+ }
1032
+ }
1033
+ async turnOnOff(device_list) {
1034
+ return await this.retry({
1035
+ max: await this.maxRetry(),
1036
+ fn: () => {
1037
+ if (this.On) {
1038
+ return device_list[0].turnOn({ id: this.device.bleMac });
1039
+ }
1040
+ else {
1041
+ return device_list[0].turnOff({ id: this.device.bleMac });
1042
+ }
1043
+ },
1044
+ });
1045
+ }
1046
+ async retry({ max, fn }) {
1047
+ return fn().catch(async (err) => {
1048
+ if (max === 0) {
1049
+ throw err;
1050
+ }
1051
+ this.infoLog(err);
1052
+ this.infoLog('Retrying');
1053
+ await this.switchbot.wait(1000);
1054
+ return this.retry({ max: max - 1, fn });
1055
+ });
1056
+ }
1057
+ async maxRetry() {
1058
+ let maxRetry;
1059
+ if (this.device.bot?.maxRetry) {
1060
+ maxRetry = this.device.bot?.maxRetry;
1061
+ }
1062
+ else {
1063
+ maxRetry = 5;
1064
+ }
1065
+ return maxRetry;
1066
+ }
1067
+ async PressOrSwitch(device) {
1068
+ if (!device.bot?.mode) {
1069
+ this.botMode = 'switch';
1070
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} does not have bot mode set in the Plugin's SwitchBot Device Settings,`);
1071
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} is defaulting to "${this.botMode}" mode, you may experience issues.`);
1072
+ }
1073
+ else if (device.bot?.mode === 'switch') {
1074
+ this.botMode = 'switch';
1075
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
1076
+ }
1077
+ else if (device.bot?.mode === 'press') {
1078
+ this.botMode = 'press';
1079
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
1080
+ }
1081
+ else {
1082
+ throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
1083
+ }
1084
+ }
1085
+ async allowPushChanges(device) {
1086
+ if (device.bot?.allowPush) {
1087
+ this.allowPush = true;
1088
+ }
1089
+ else {
1090
+ this.allowPush = false;
1091
+ }
1092
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Allowing Push Changes: ${this.allowPush}`);
1093
+ }
1094
+ async scan(device) {
1095
+ if (device.scanDuration) {
1096
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
1097
+ if (this.BLE) {
1098
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
1099
+ }
1100
+ }
1101
+ else {
1102
+ this.scanDuration = this.accessory.context.scanDuration = 1;
1103
+ if (this.BLE) {
1104
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
1105
+ }
989
1106
  }
990
- //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
991
1107
  }
992
1108
  async statusCode({ res }) {
993
1109
  switch (res.statusCode) {
@@ -1028,96 +1144,111 @@ class Bot {
1028
1144
  else {
1029
1145
  this.outletService?.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
1030
1146
  }
1147
+ await this.updateHomeKitCharacteristics();
1031
1148
  }
1032
1149
  }
1033
- /**
1034
- * Handle requests to set the "On" characteristic
1035
- */
1036
- async handleOnSet(value) {
1150
+ async apiError(e) {
1037
1151
  if (this.device.bot?.deviceType === 'garagedoor') {
1038
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} TargetDoorState: ${value}`);
1039
- if (value === this.platform.Characteristic.TargetDoorState.CLOSED) {
1040
- this.On = false;
1041
- }
1042
- else {
1043
- this.On = true;
1044
- }
1152
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.TargetDoorState, e);
1153
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.CurrentDoorState, e);
1154
+ this.garageDoorService?.updateCharacteristic(this.platform.Characteristic.ObstructionDetected, e);
1045
1155
  }
1046
- else if (this.device.bot?.deviceType === 'door' ||
1047
- this.device.bot?.deviceType === 'window' ||
1048
- this.device.bot?.deviceType === 'windowcovering') {
1049
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} TargetPosition: ${value}`);
1050
- if (value === 0) {
1051
- this.On = false;
1052
- }
1053
- else {
1054
- this.On = true;
1055
- }
1156
+ else if (this.device.bot?.deviceType === 'door') {
1157
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
1158
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
1159
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
1160
+ }
1161
+ else if (this.device.bot?.deviceType === 'window') {
1162
+ this.windowService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
1163
+ this.windowService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
1164
+ this.windowService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
1165
+ }
1166
+ else if (this.device.bot?.deviceType === 'windowcovering') {
1167
+ this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.TargetPosition, e);
1168
+ this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.CurrentPosition, e);
1169
+ this.windowCoveringService?.updateCharacteristic(this.platform.Characteristic.PositionState, e);
1056
1170
  }
1057
1171
  else if (this.device.bot?.deviceType === 'lock') {
1058
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LockTargetState: ${value}`);
1059
- if (value === this.platform.Characteristic.LockTargetState.SECURED) {
1060
- this.On = false;
1061
- }
1062
- else {
1063
- this.On = true;
1064
- }
1172
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.LockTargetState, e);
1173
+ this.doorService?.updateCharacteristic(this.platform.Characteristic.LockCurrentState, e);
1065
1174
  }
1066
1175
  else if (this.device.bot?.deviceType === 'faucet') {
1067
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Active: ${value}`);
1068
- if (value === this.platform.Characteristic.Active.INACTIVE) {
1069
- this.On = false;
1070
- }
1071
- else {
1072
- this.On = true;
1073
- }
1176
+ this.faucetService?.updateCharacteristic(this.platform.Characteristic.Active, e);
1177
+ }
1178
+ else if (this.device.bot?.deviceType === 'fan') {
1179
+ this.fanService?.updateCharacteristic(this.platform.Characteristic.On, e);
1074
1180
  }
1075
1181
  else if (this.device.bot?.deviceType === 'stateful') {
1076
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ProgrammableSwitchOutputState: ${value}`);
1077
- if (value === 0) {
1078
- this.On = false;
1079
- }
1080
- else {
1081
- this.On = true;
1082
- }
1182
+ this.statefulProgrammableSwitchService?.updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent, e);
1183
+ this.statefulProgrammableSwitchService?.updateCharacteristic(this.platform.Characteristic.ProgrammableSwitchOutputState, e);
1184
+ }
1185
+ else if (this.device.bot?.deviceType === 'switch') {
1186
+ this.switchService?.updateCharacteristic(this.platform.Characteristic.On, e);
1083
1187
  }
1084
1188
  else {
1085
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${value}`);
1086
- this.On = value;
1189
+ this.outletService?.updateCharacteristic(this.platform.Characteristic.On, e);
1190
+ }
1191
+ if (this.BLE) {
1192
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
1193
+ this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
1087
1194
  }
1088
- this.doBotUpdate.next();
1089
1195
  }
1090
- async retry({ max, fn }) {
1091
- return fn().catch(async (err) => {
1092
- if (max === 0) {
1093
- throw err;
1094
- }
1095
- this.infoLog(err);
1096
- this.infoLog('Retrying');
1097
- await this.switchbot.wait(1000);
1098
- return this.retry({ max: max - 1, fn });
1099
- });
1196
+ FirmwareRevision(accessory, device) {
1197
+ let FirmwareRevision;
1198
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
1199
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
1200
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
1201
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
1202
+ if (accessory.context.FirmwareRevision) {
1203
+ FirmwareRevision = accessory.context.FirmwareRevision;
1204
+ }
1205
+ else if (device.firmware) {
1206
+ FirmwareRevision = device.firmware;
1207
+ }
1208
+ else {
1209
+ FirmwareRevision = this.platform.version;
1210
+ }
1211
+ return FirmwareRevision;
1100
1212
  }
1101
- async maxRetry() {
1102
- let maxRetry;
1103
- if (this.device.bot?.maxRetry) {
1104
- maxRetry = this.device.bot?.maxRetry;
1213
+ context() {
1214
+ if (this.On === undefined) {
1215
+ this.On = false;
1216
+ this.accessory.context.On = this.On;
1105
1217
  }
1106
1218
  else {
1107
- maxRetry = 5;
1219
+ this.On = this.accessory.context.On;
1220
+ }
1221
+ if (this.BatteryLevel === undefined) {
1222
+ this.BatteryLevel = 100;
1223
+ }
1224
+ else {
1225
+ this.BatteryLevel = this.accessory.context.BatteryLevel;
1226
+ }
1227
+ if (this.StatusLowBattery === undefined) {
1228
+ this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
1229
+ this.accessory.context.StatusLowBattery = this.StatusLowBattery;
1230
+ }
1231
+ else {
1232
+ this.StatusLowBattery = this.accessory.context.StatusLowBattery;
1233
+ }
1234
+ }
1235
+ async refreshRate(device) {
1236
+ if (device.refreshRate) {
1237
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
1238
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
1239
+ }
1240
+ else if (this.platform.config.options.refreshRate) {
1241
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
1242
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
1108
1243
  }
1109
- return maxRetry;
1110
1244
  }
1111
1245
  async config(device) {
1112
1246
  let config = {};
1113
1247
  if (device.bot) {
1114
1248
  config = device.bot;
1115
1249
  }
1116
- if (device.ble) {
1117
- config['ble'] = device.ble;
1118
- }
1119
- if (device.openAPI !== undefined) {
1120
- config['openAPI'] = device.openAPI;
1250
+ if (device.connectionType !== undefined) {
1251
+ config['connectionType'] = device.connectionType;
1121
1252
  }
1122
1253
  if (device.logging !== undefined) {
1123
1254
  config['logging'] = device.logging;
@@ -1135,39 +1266,6 @@ class Bot {
1135
1266
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
1136
1267
  }
1137
1268
  }
1138
- async refreshRate(device) {
1139
- if (device.refreshRate) {
1140
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
1141
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
1142
- }
1143
- else if (this.platform.config.options.refreshRate) {
1144
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
1145
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
1146
- }
1147
- }
1148
- async openAPI() {
1149
- if (!this.device.openAPI) {
1150
- this.OpenAPI = true;
1151
- }
1152
- else {
1153
- this.OpenAPI = this.device.openAPI;
1154
- }
1155
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI: ${this.OpenAPI}`);
1156
- }
1157
- async scan(device) {
1158
- if (device.scanDuration) {
1159
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
1160
- if (device.ble) {
1161
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
1162
- }
1163
- }
1164
- else {
1165
- this.scanDuration = this.accessory.context.scanDuration = 1;
1166
- if (this.device.ble) {
1167
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
1168
- }
1169
- }
1170
- }
1171
1269
  async logs(device) {
1172
1270
  if (this.platform.debugMode) {
1173
1271
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
@@ -1186,130 +1284,6 @@ class Bot {
1186
1284
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
1187
1285
  }
1188
1286
  }
1189
- FirmwareRevision(accessory, device) {
1190
- let FirmwareRevision;
1191
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
1192
- + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
1193
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
1194
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
1195
- if (accessory.context.FirmwareRevision) {
1196
- FirmwareRevision = accessory.context.FirmwareRevision;
1197
- }
1198
- else if (device.firmware) {
1199
- FirmwareRevision = device.firmware;
1200
- }
1201
- else {
1202
- FirmwareRevision = this.platform.version;
1203
- }
1204
- return FirmwareRevision;
1205
- }
1206
- async PressOrSwitch(device) {
1207
- if (!device.bot?.mode) {
1208
- this.botMode = 'switch';
1209
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} does not have bot mode set in the Plugin's SwitchBot Device Settings,`);
1210
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} is defaulting to "${this.botMode}" mode, you may experience issues.`);
1211
- }
1212
- else if (device.bot?.mode === 'switch') {
1213
- this.botMode = 'switch';
1214
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
1215
- }
1216
- else if (device.bot?.mode === 'press') {
1217
- this.botMode = 'press';
1218
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Bot Mode: ${this.botMode}`);
1219
- }
1220
- else {
1221
- throw new Error(`${this.device.deviceType}: ${this.accessory.displayName} Bot Mode: ${this.botMode}`);
1222
- }
1223
- }
1224
- async allowPushChanges(device) {
1225
- if (device.bot?.allowPush) {
1226
- this.allowPush = true;
1227
- }
1228
- else {
1229
- this.allowPush = false;
1230
- }
1231
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Allowing Push Changes: ${this.allowPush}`);
1232
- }
1233
- async removeOutletService(accessory) {
1234
- // If outletService still pressent, then remove first
1235
- this.outletService = this.accessory.getService(this.platform.Service.Outlet);
1236
- if (this.outletService) {
1237
- this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Outlet Service`);
1238
- }
1239
- accessory.removeService(this.outletService);
1240
- }
1241
- async removeGarageDoorService(accessory) {
1242
- // If garageDoorService still pressent, then remove first
1243
- this.garageDoorService = this.accessory.getService(this.platform.Service.GarageDoorOpener);
1244
- if (this.garageDoorService) {
1245
- this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Garage Door Service`);
1246
- }
1247
- accessory.removeService(this.garageDoorService);
1248
- }
1249
- async removeDoorService(accessory) {
1250
- // If doorService still pressent, then remove first
1251
- this.doorService = this.accessory.getService(this.platform.Service.Door);
1252
- if (this.doorService) {
1253
- this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Door Service`);
1254
- }
1255
- accessory.removeService(this.doorService);
1256
- }
1257
- async removeLockService(accessory) {
1258
- // If lockService still pressent, then remove first
1259
- this.lockService = this.accessory.getService(this.platform.Service.LockMechanism);
1260
- if (this.lockService) {
1261
- this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Lock Service`);
1262
- }
1263
- accessory.removeService(this.lockService);
1264
- }
1265
- async removeFaucetService(accessory) {
1266
- // If faucetService still pressent, then remove first
1267
- this.faucetService = this.accessory.getService(this.platform.Service.Faucet);
1268
- if (this.faucetService) {
1269
- this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Faucet Service`);
1270
- }
1271
- accessory.removeService(this.faucetService);
1272
- }
1273
- async removeFanService(accessory) {
1274
- // If fanService still pressent, then remove first
1275
- this.fanService = this.accessory.getService(this.platform.Service.Fan);
1276
- if (this.fanService) {
1277
- this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Fan Service`);
1278
- }
1279
- accessory.removeService(this.fanService);
1280
- }
1281
- async removeWindowService(accessory) {
1282
- // If windowService still pressent, then remove first
1283
- this.windowService = this.accessory.getService(this.platform.Service.Window);
1284
- if (this.windowService) {
1285
- this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Window Service`);
1286
- }
1287
- accessory.removeService(this.windowService);
1288
- }
1289
- async removeWindowCoveringService(accessory) {
1290
- // If windowCoveringService still pressent, then remove first
1291
- this.windowCoveringService = this.accessory.getService(this.platform.Service.WindowCovering);
1292
- if (this.windowCoveringService) {
1293
- this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Window Covering Service`);
1294
- }
1295
- accessory.removeService(this.windowCoveringService);
1296
- }
1297
- async removeStatefulProgrammableSwitchService(accessory) {
1298
- // If statefulProgrammableSwitchService still pressent, then remove first
1299
- this.statefulProgrammableSwitchService = this.accessory.getService(this.platform.Service.StatefulProgrammableSwitch);
1300
- if (this.statefulProgrammableSwitchService) {
1301
- this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Stateful Programmable Switch Service`);
1302
- }
1303
- accessory.removeService(this.statefulProgrammableSwitchService);
1304
- }
1305
- async removeSwitchService(accessory) {
1306
- // If switchService still pressent, then remove first
1307
- this.switchService = this.accessory.getService(this.platform.Service.Switch);
1308
- if (this.switchService) {
1309
- this.warnLog(`${this.device.deviceType}: ${accessory.displayName} Removing Leftover Switch Service`);
1310
- }
1311
- accessory.removeService(this.switchService);
1312
- }
1313
1287
  /**
1314
1288
  * Logging for Device
1315
1289
  */