@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
@@ -15,17 +15,15 @@ class Plug {
15
15
  this.platform = platform;
16
16
  this.accessory = accessory;
17
17
  this.device = device;
18
+ // Connection
19
+ this.BLE = (this.device.connectionType === 'BLE' || this.device.connectionType === 'Both');
20
+ this.OpenAPI = (this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'Both');
18
21
  // default placeholders
19
22
  this.logs(device);
20
23
  this.scan(device);
21
24
  this.refreshRate(device);
22
25
  this.config(device);
23
- if (this.On === undefined) {
24
- this.On = false;
25
- }
26
- else {
27
- this.On = this.accessory.context.On;
28
- }
26
+ this.context();
29
27
  // this is subject we use to track when we need to POST changes to the SwitchBot API
30
28
  this.doPlugUpdate = new rxjs_1.Subject();
31
29
  this.plugUpdateInProgress = false;
@@ -53,7 +51,7 @@ class Plug {
53
51
  // each service must implement at-minimum the "required characteristics" for the given service type
54
52
  // see https://developers.homebridge.io/#/service/Outlet
55
53
  // create handlers for required characteristics
56
- this.outletService.getCharacteristic(this.platform.Characteristic.On).onSet(this.handleOnSet.bind(this));
54
+ this.outletService.getCharacteristic(this.platform.Characteristic.On).onSet(this.OnSet.bind(this));
57
55
  // Update Homekit
58
56
  this.updateHomeKitCharacteristics();
59
57
  // Start an update interval
@@ -73,26 +71,31 @@ class Plug {
73
71
  await this.pushChanges();
74
72
  }
75
73
  catch (e) {
76
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges`);
77
- if (this.deviceLogging.includes('debug')) {
78
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges,`
79
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
80
- }
81
74
  this.apiError(e);
75
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${this.device.connectionType} Connection,`
76
+ + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
82
77
  }
83
78
  this.plugUpdateInProgress = false;
84
79
  });
85
80
  }
81
+ /**
82
+ * Parse the device status from the SwitchBot api
83
+ */
86
84
  async parseStatus() {
87
- if (this.SwitchToOpenAPI || !this.device.ble) {
85
+ if (this.BLE && !this.SwitchToOpenAPI) {
86
+ await this.BLEparseStatus();
87
+ }
88
+ else if (this.OpenAPI && this.platform.config.credentials?.token) {
89
+ this.SwitchToOpenAPI = false;
88
90
  await this.openAPIparseStatus();
89
91
  }
90
92
  else {
91
- await this.BLEparseStatus();
93
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
94
+ + ` ${this.device.connectionType}, parseStatus will not happen.`);
92
95
  }
93
96
  }
94
97
  async BLEparseStatus() {
95
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE parseStatus`);
98
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
96
99
  // State
97
100
  switch (this.state) {
98
101
  case 'on':
@@ -104,6 +107,7 @@ class Plug {
104
107
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
105
108
  }
106
109
  async openAPIparseStatus() {
110
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
107
111
  switch (this.power) {
108
112
  case 'on':
109
113
  this.On = true;
@@ -113,16 +117,23 @@ class Plug {
113
117
  }
114
118
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
115
119
  }
120
+ /**
121
+ * Asks the SwitchBot API for the latest device information
122
+ */
116
123
  async refreshStatus() {
117
- if (this.device.ble) {
124
+ if (this.BLE) {
118
125
  await this.BLERefreshStatus();
119
126
  }
120
- else {
127
+ else if (this.OpenAPI && this.platform.config.credentials?.token) {
121
128
  await this.openAPIRefreshStatus();
122
129
  }
130
+ else {
131
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
132
+ + ` ${this.device.connectionType}, refreshStatus will not happen.`);
133
+ }
123
134
  }
124
135
  async BLERefreshStatus() {
125
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE refreshStatus`);
136
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
126
137
  const switchbot = await this.platform.connectBLE();
127
138
  // Convert to BLE Address
128
139
  this.device.bleMac = this.device
@@ -184,114 +195,68 @@ class Plug {
184
195
  }
185
196
  })
186
197
  .catch(async (e) => {
187
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection`);
188
- if (this.deviceLogging.includes('debug')) {
189
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with BLE Connection,`
190
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
191
- }
192
- if (this.platform.config.credentials?.token) {
193
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
194
- this.SwitchToOpenAPI = true;
195
- await this.openAPIRefreshStatus();
196
- }
197
198
  this.apiError(e);
199
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}`
200
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
201
+ await this.BLEconnection(switchbot);
198
202
  });
199
203
  }
200
204
  else {
201
205
  await this.BLEconnection(switchbot);
202
206
  }
203
207
  }
204
- BLEmodel() {
205
- if (this.device.deviceType === 'Plug Mini (US)') {
206
- return 'g';
207
- }
208
- else {
209
- return 'j';
210
- }
211
- }
212
- async getCustomBLEAddress(switchbot) {
213
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
214
- (async () => {
215
- // Start to monitor advertisement packets
216
- await switchbot.startScan({
217
- model: this.BLEmodel(),
218
- });
219
- // Set an event handler
220
- switchbot.onadvertisement = (ad) => {
221
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
222
- };
223
- await switchbot.wait(10000);
224
- // Stop to monitor
225
- switchbot.stopScan();
226
- })();
227
- }
228
- }
229
- async BLEconnection(switchbot) {
230
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
231
- if (this.platform.config.credentials?.token) {
232
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
233
- this.SwitchToOpenAPI = true;
234
- await this.openAPIRefreshStatus();
235
- }
236
- }
237
208
  async openAPIRefreshStatus() {
238
- if (this.OpenAPI) {
239
- try {
240
- const t = Date.now();
241
- const nonce = 'requestID';
242
- const data = this.platform.config.credentials?.token + t + nonce;
243
- const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
244
- const sign = signTerm.toString('base64');
245
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
246
- const options = {
247
- hostname: settings_1.HostDomain,
248
- port: 443,
249
- path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
250
- method: 'GET',
251
- headers: {
252
- Authorization: this.platform.config.credentials?.token,
253
- sign: sign,
254
- nonce: nonce,
255
- t: t,
256
- 'Content-Type': 'application/json',
257
- },
258
- };
259
- const req = https_1.default.request(options, (res) => {
260
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
261
- let rawData = '';
262
- res.on('data', (d) => {
263
- rawData += d;
264
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
265
- });
266
- res.on('end', () => {
267
- try {
268
- this.deviceStatus = JSON.parse(rawData);
269
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
270
- this.power = this.deviceStatus.body.power;
271
- this.parseStatus();
272
- this.updateHomeKitCharacteristics();
273
- }
274
- catch (e) {
275
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
276
- }
277
- });
209
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
210
+ try {
211
+ const t = Date.now();
212
+ const nonce = 'requestID';
213
+ const data = this.platform.config.credentials?.token + t + nonce;
214
+ const signTerm = crypto_1.default.createHmac('sha256', this.platform.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
215
+ const sign = signTerm.toString('base64');
216
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} sign: ${sign}`);
217
+ const options = {
218
+ hostname: settings_1.HostDomain,
219
+ port: 443,
220
+ path: `${settings_1.DevicePath}/${this.device.deviceId}/status`,
221
+ method: 'GET',
222
+ headers: {
223
+ Authorization: this.platform.config.credentials?.token,
224
+ sign: sign,
225
+ nonce: nonce,
226
+ t: t,
227
+ 'Content-Type': 'application/json',
228
+ },
229
+ };
230
+ const req = https_1.default.request(options, (res) => {
231
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus statusCode: ${res.statusCode}`);
232
+ this.statusCode({ res });
233
+ let rawData = '';
234
+ res.on('data', (d) => {
235
+ rawData += d;
236
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
278
237
  });
279
- req.on('error', (e) => {
280
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
238
+ res.on('end', () => {
239
+ try {
240
+ this.deviceStatus = JSON.parse(rawData);
241
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus: ${(0, super_stringify_1.default)(this.deviceStatus)}`);
242
+ this.power = this.deviceStatus.body.power;
243
+ this.parseStatus();
244
+ this.updateHomeKitCharacteristics();
245
+ }
246
+ catch (e) {
247
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
248
+ }
281
249
  });
282
- req.end();
283
- }
284
- catch (e) {
285
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection`);
286
- if (this.deviceLogging.includes('debug')) {
287
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed refreshStatus with OpenAPI Connection,`
288
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
289
- }
290
- this.apiError(e);
291
- }
250
+ });
251
+ req.on('error', (e) => {
252
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} error message: ${e.message}`);
253
+ });
254
+ req.end();
292
255
  }
293
- else {
294
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI is disabled, commands will not be sent.`);
256
+ catch (e) {
257
+ this.apiError(e);
258
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
259
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
295
260
  }
296
261
  }
297
262
  /**
@@ -304,71 +269,66 @@ class Plug {
304
269
  * Plug Mini (US/JP) - "command" toggle default = toggle state
305
270
  */
306
271
  async pushChanges() {
307
- if (this.device.ble) {
272
+ if (this.BLE) {
308
273
  await this.BLEpushChanges();
309
274
  }
310
- else {
275
+ else if (this.OpenAPI && this.platform.config.credentials?.token) {
311
276
  await this.openAPIpushChanges();
312
277
  }
313
- }
314
- async BLEpushChanges() {
315
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE pushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
316
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE pushChanges`);
317
- const switchbot = await this.platform.connectBLE();
318
- // Convert to BLE Address
319
- this.device.bleMac = this.device
320
- .deviceId.match(/.{1,2}/g)
321
- .join(':')
322
- .toLowerCase();
323
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
324
- switchbot
325
- .discover({
326
- model: this.BLEmodel(),
327
- id: this.device.bleMac,
328
- })
329
- .then((device_list) => {
330
- this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
331
- return this.turnOnOff(device_list);
332
- })
333
- .then(() => {
334
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
335
- this.On = false;
336
- })
337
- .catch(async (e) => {
338
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection`);
339
- if (this.deviceLogging.includes('debug')) {
340
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
341
- + ` Error Message: ${(0, super_stringify_1.default)(e.message)}`);
342
- }
343
- if (this.platform.debugMode) {
344
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed pushChanges with BLE Connection,`
345
- + ` Error: ${(0, super_stringify_1.default)(e)}`);
346
- }
347
- if (this.platform.config.credentials?.token) {
348
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
349
- await this.openAPIpushChanges();
350
- }
351
- this.apiError(e);
278
+ else {
279
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
280
+ + ` ${this.device.connectionType}, pushChanges will not happen.`);
281
+ }
282
+ (0, rxjs_1.interval)(5000)
283
+ .pipe((0, operators_1.take)(1))
284
+ .subscribe(async () => {
285
+ await this.refreshStatus();
352
286
  });
353
287
  }
354
- async turnOnOff(device_list) {
355
- return await this.retry({
356
- max: await this.maxRetry(),
357
- fn: () => {
358
- if (this.On) {
359
- return device_list[0].turnOn({ id: this.device.bleMac });
360
- }
361
- else {
362
- return device_list[0].turnOff({ id: this.device.bleMac });
288
+ async BLEpushChanges() {
289
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
290
+ if (this.On !== this.accessory.context.On) {
291
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges On: ${this.On} OnCached: ${this.accessory.context.On}`);
292
+ const switchbot = await this.platform.connectBLE();
293
+ // Convert to BLE Address
294
+ this.device.bleMac = this.device
295
+ .deviceId.match(/.{1,2}/g)
296
+ .join(':')
297
+ .toLowerCase();
298
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
299
+ switchbot
300
+ .discover({
301
+ model: this.BLEmodel(),
302
+ id: this.device.bleMac,
303
+ })
304
+ .then((device_list) => {
305
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
306
+ return this.turnOnOff(device_list);
307
+ })
308
+ .then(() => {
309
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
310
+ this.On = false;
311
+ })
312
+ .catch(async (e) => {
313
+ this.apiError(e);
314
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
315
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
316
+ if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
317
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
318
+ await this.openAPIpushChanges();
363
319
  }
364
- },
365
- });
320
+ });
321
+ }
322
+ else {
323
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges.` + `On: ${this.On}, `
324
+ + `OnCached: ${this.accessory.context.On}`);
325
+ }
366
326
  }
367
327
  async openAPIpushChanges() {
368
- if (this.platform.config.credentials?.token && this.OpenAPI) {
328
+ try {
369
329
  if (this.On !== this.accessory.context.On) {
370
330
  // Make Push On request to the API
371
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI pushChanges`);
331
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
372
332
  const t = Date.now();
373
333
  const nonce = 'requestID';
374
334
  const data = this.platform.config.credentials?.token + t + nonce;
@@ -405,7 +365,7 @@ class Plug {
405
365
  },
406
366
  };
407
367
  const req = https_1.default.request(options, res => {
408
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${res.statusCode}`);
368
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges statusCode: ${res.statusCode}`);
409
369
  this.statusCode({ res });
410
370
  res.on('data', d => {
411
371
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} d: ${d}`);
@@ -418,16 +378,25 @@ class Plug {
418
378
  req.end();
419
379
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges: ${(0, super_stringify_1.default)(req)}`);
420
380
  }
421
- (0, rxjs_1.interval)(5000)
422
- .pipe((0, operators_1.take)(1))
423
- .subscribe(async () => {
424
- await this.refreshStatus();
425
- });
381
+ else {
382
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.` + `On: ${this.On}, `
383
+ + `OnCached: ${this.accessory.context.On}`);
384
+ }
426
385
  }
427
- else {
428
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} OpenAPI is disabled, commands will not be sent.`);
386
+ catch (e) {
387
+ this.apiError(e);
388
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
389
+ + ` Connection, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
429
390
  }
430
391
  }
392
+ /**
393
+ * Handle requests to set the value of the "On" characteristic
394
+ */
395
+ async OnSet(value) {
396
+ this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
397
+ this.On = value;
398
+ this.doPlugUpdate.next();
399
+ }
431
400
  async updateHomeKitCharacteristics() {
432
401
  if (this.On === undefined) {
433
402
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
@@ -438,9 +407,99 @@ class Plug {
438
407
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
439
408
  }
440
409
  }
441
- async apiError(e) {
442
- this.outletService.updateCharacteristic(this.platform.Characteristic.On, e);
443
- //throw new this.platform.api.hap.HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
410
+ BLEmodel() {
411
+ if (this.device.deviceType === 'Plug Mini (US)') {
412
+ return 'g';
413
+ }
414
+ else {
415
+ return 'j';
416
+ }
417
+ }
418
+ async getCustomBLEAddress(switchbot) {
419
+ if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
420
+ (async () => {
421
+ // Start to monitor advertisement packets
422
+ await switchbot.startScan({
423
+ model: this.BLEmodel(),
424
+ });
425
+ // Set an event handler
426
+ switchbot.onadvertisement = (ad) => {
427
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${(0, super_stringify_1.default)(ad, null, ' ')}`);
428
+ };
429
+ await switchbot.wait(10000);
430
+ // Stop to monitor
431
+ switchbot.stopScan();
432
+ })();
433
+ }
434
+ }
435
+ async BLEconnection(switchbot) {
436
+ this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
437
+ if (this.platform.config.credentials?.token && this.device.connectionType !== 'Both') {
438
+ this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI Connection`);
439
+ this.SwitchToOpenAPI = true;
440
+ await this.openAPIRefreshStatus();
441
+ }
442
+ }
443
+ async turnOnOff(device_list) {
444
+ return await this.retry({
445
+ max: await this.maxRetry(),
446
+ fn: () => {
447
+ if (this.On) {
448
+ return device_list[0].turnOn({ id: this.device.bleMac });
449
+ }
450
+ else {
451
+ return device_list[0].turnOff({ id: this.device.bleMac });
452
+ }
453
+ },
454
+ });
455
+ }
456
+ async retry({ max, fn }) {
457
+ return fn().catch(async (err) => {
458
+ if (max === 0) {
459
+ throw err;
460
+ }
461
+ this.infoLog(err);
462
+ this.infoLog('Retrying');
463
+ await this.switchbot.wait(1000);
464
+ return this.retry({ max: max - 1, fn });
465
+ });
466
+ }
467
+ async maxRetry() {
468
+ let maxRetry;
469
+ if (this.device.bot?.maxRetry) {
470
+ maxRetry = this.device.bot?.maxRetry;
471
+ }
472
+ else {
473
+ maxRetry = 5;
474
+ }
475
+ return maxRetry;
476
+ }
477
+ model(device) {
478
+ let model;
479
+ if (device.deviceType === 'Plug Mini (US)') {
480
+ model = 'W1901400';
481
+ }
482
+ else if (device.deviceType === 'Plug Mini (JP)') {
483
+ model = 'W2001400';
484
+ }
485
+ else {
486
+ model = 'SP11';
487
+ }
488
+ return model;
489
+ }
490
+ async scan(device) {
491
+ if (device.scanDuration) {
492
+ this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
493
+ if (this.BLE) {
494
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
495
+ }
496
+ }
497
+ else {
498
+ this.scanDuration = this.accessory.context.scanDuration = 1;
499
+ if (this.BLE) {
500
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
501
+ }
502
+ }
444
503
  }
445
504
  async statusCode({ res }) {
446
505
  switch (res.statusCode) {
@@ -476,60 +535,54 @@ class Plug {
476
535
  if (this.device.offline) {
477
536
  this.On = false;
478
537
  this.outletService.getCharacteristic(this.platform.Characteristic.On).updateValue(this.On);
538
+ await this.updateHomeKitCharacteristics();
479
539
  }
480
540
  }
481
- /**
482
- * Handle requests to set the value of the "On" characteristic
483
- */
484
- async handleOnSet(value) {
485
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} - Set On: ${value}`);
486
- this.On = value;
487
- this.doPlugUpdate.next();
541
+ async apiError(e) {
542
+ this.outletService.updateCharacteristic(this.platform.Characteristic.On, e);
488
543
  }
489
- model(device) {
490
- let model;
491
- if (device.deviceType === 'Plug Mini (US)') {
492
- model = 'W1901400';
544
+ FirmwareRevision(accessory, device) {
545
+ let FirmwareRevision;
546
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
547
+ + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
548
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
549
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
550
+ if (accessory.context.FirmwareRevision) {
551
+ FirmwareRevision = accessory.context.FirmwareRevision;
493
552
  }
494
- else if (device.deviceType === 'Plug Mini (JP)') {
495
- model = 'W2001400';
553
+ else if (device.firmware) {
554
+ FirmwareRevision = device.firmware;
496
555
  }
497
556
  else {
498
- model = 'SP11';
557
+ FirmwareRevision = this.platform.version;
499
558
  }
500
- return model;
501
- }
502
- async retry({ max, fn }) {
503
- return fn().catch(async (err) => {
504
- if (max === 0) {
505
- throw err;
506
- }
507
- this.infoLog(err);
508
- this.infoLog('Retrying');
509
- await this.switchbot.wait(1000);
510
- return this.retry({ max: max - 1, fn });
511
- });
559
+ return FirmwareRevision;
512
560
  }
513
- async maxRetry() {
514
- let maxRetry;
515
- if (this.device.bot?.maxRetry) {
516
- maxRetry = this.device.bot?.maxRetry;
561
+ async context() {
562
+ if (this.On === undefined) {
563
+ this.On = false;
517
564
  }
518
565
  else {
519
- maxRetry = 5;
566
+ this.On = this.accessory.context.On;
567
+ }
568
+ }
569
+ async refreshRate(device) {
570
+ if (device.refreshRate) {
571
+ this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
572
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
573
+ }
574
+ else if (this.platform.config.options.refreshRate) {
575
+ this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
576
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
520
577
  }
521
- return maxRetry;
522
578
  }
523
579
  async config(device) {
524
580
  let config = {};
525
581
  if (device.plug) {
526
582
  config = device.plug;
527
583
  }
528
- if (device.ble !== undefined) {
529
- config['ble'] = device.ble;
530
- }
531
- if (device.openAPI !== undefined) {
532
- config['openAPI'] = device.openAPI;
584
+ if (device.connectionType !== undefined) {
585
+ config['connectionType'] = device.connectionType;
533
586
  }
534
587
  if (device.logging !== undefined) {
535
588
  config['logging'] = device.logging;
@@ -547,39 +600,6 @@ class Plug {
547
600
  this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${(0, super_stringify_1.default)(config)}`);
548
601
  }
549
602
  }
550
- async refreshRate(device) {
551
- if (device.refreshRate) {
552
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
553
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
554
- }
555
- else if (this.platform.config.options.refreshRate) {
556
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
557
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
558
- }
559
- }
560
- async openAPI() {
561
- if (!this.device.openAPI) {
562
- this.OpenAPI = true;
563
- }
564
- else {
565
- this.OpenAPI = this.device.openAPI;
566
- }
567
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using OpenAPI: ${this.OpenAPI}`);
568
- }
569
- async scan(device) {
570
- if (device.scanDuration) {
571
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
572
- if (device.ble) {
573
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
574
- }
575
- }
576
- else {
577
- this.scanDuration = this.accessory.context.scanDuration = 1;
578
- if (this.device.ble) {
579
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
580
- }
581
- }
582
- }
583
603
  async logs(device) {
584
604
  if (this.platform.debugMode) {
585
605
  this.deviceLogging = this.accessory.context.logging = 'debugMode';
@@ -598,23 +618,6 @@ class Plug {
598
618
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
599
619
  }
600
620
  }
601
- FirmwareRevision(accessory, device) {
602
- let FirmwareRevision;
603
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
604
- + ` accessory.context.FirmwareRevision: ${accessory.context.FirmwareRevision}`);
605
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} device.firmware: ${device.firmware}`);
606
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} this.platform.version: ${this.platform.version}`);
607
- if (accessory.context.FirmwareRevision) {
608
- FirmwareRevision = accessory.context.FirmwareRevision;
609
- }
610
- else if (device.firmware) {
611
- FirmwareRevision = device.firmware;
612
- }
613
- else {
614
- FirmwareRevision = this.platform.version;
615
- }
616
- return FirmwareRevision;
617
- }
618
621
  /**
619
622
  * Logging for Device
620
623
  */