@switchbot/homebridge-switchbot 2.0.0-beta.2 → 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 (113) hide show
  1. package/CHANGELOG.md +8 -1
  2. package/README.md +1 -2
  3. package/config.schema.json +75 -20
  4. package/dist/device/bot.d.ts +32 -28
  5. package/dist/device/bot.d.ts.map +1 -1
  6. package/dist/device/bot.js +646 -648
  7. package/dist/device/bot.js.map +1 -1
  8. package/dist/device/colorbulb.d.ts +34 -19
  9. package/dist/device/colorbulb.d.ts.map +1 -1
  10. package/dist/device/colorbulb.js +429 -304
  11. package/dist/device/colorbulb.js.map +1 -1
  12. package/dist/device/contact.d.ts +16 -7
  13. package/dist/device/contact.d.ts.map +1 -1
  14. package/dist/device/contact.js +288 -230
  15. package/dist/device/contact.js.map +1 -1
  16. package/dist/device/curtain.d.ts +21 -16
  17. package/dist/device/curtain.d.ts.map +1 -1
  18. package/dist/device/curtain.js +519 -489
  19. package/dist/device/curtain.js.map +1 -1
  20. package/dist/device/humidifier.d.ts +22 -18
  21. package/dist/device/humidifier.d.ts.map +1 -1
  22. package/dist/device/humidifier.js +415 -360
  23. package/dist/device/humidifier.js.map +1 -1
  24. package/dist/device/lock.d.ts +22 -8
  25. package/dist/device/lock.d.ts.map +1 -1
  26. package/dist/device/lock.js +253 -147
  27. package/dist/device/lock.js.map +1 -1
  28. package/dist/device/meter.d.ts +21 -16
  29. package/dist/device/meter.d.ts.map +1 -1
  30. package/dist/device/meter.js +325 -255
  31. package/dist/device/meter.js.map +1 -1
  32. package/dist/device/meterplus.d.ts +23 -12
  33. package/dist/device/meterplus.d.ts.map +1 -1
  34. package/dist/device/meterplus.js +346 -208
  35. package/dist/device/meterplus.js.map +1 -1
  36. package/dist/device/motion.d.ts +17 -8
  37. package/dist/device/motion.d.ts.map +1 -1
  38. package/dist/device/motion.js +261 -209
  39. package/dist/device/motion.js.map +1 -1
  40. package/dist/device/plug.d.ts +24 -16
  41. package/dist/device/plug.d.ts.map +1 -1
  42. package/dist/device/plug.js +267 -242
  43. package/dist/device/plug.js.map +1 -1
  44. package/dist/device/striplight.d.ts +51 -16
  45. package/dist/device/striplight.d.ts.map +1 -1
  46. package/dist/device/striplight.js +527 -185
  47. package/dist/device/striplight.js.map +1 -1
  48. package/dist/irdevice/airconditioner.d.ts +7 -12
  49. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  50. package/dist/irdevice/airconditioner.js +261 -230
  51. package/dist/irdevice/airconditioner.js.map +1 -1
  52. package/dist/irdevice/airpurifier.d.ts +3 -3
  53. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  54. package/dist/irdevice/airpurifier.js +137 -104
  55. package/dist/irdevice/airpurifier.js.map +1 -1
  56. package/dist/irdevice/camera.d.ts +9 -8
  57. package/dist/irdevice/camera.d.ts.map +1 -1
  58. package/dist/irdevice/camera.js +116 -86
  59. package/dist/irdevice/camera.js.map +1 -1
  60. package/dist/irdevice/fan.d.ts +8 -7
  61. package/dist/irdevice/fan.d.ts.map +1 -1
  62. package/dist/irdevice/fan.js +143 -109
  63. package/dist/irdevice/fan.js.map +1 -1
  64. package/dist/irdevice/light.d.ts +9 -8
  65. package/dist/irdevice/light.d.ts.map +1 -1
  66. package/dist/irdevice/light.js +120 -103
  67. package/dist/irdevice/light.js.map +1 -1
  68. package/dist/irdevice/other.d.ts +9 -8
  69. package/dist/irdevice/other.d.ts.map +1 -1
  70. package/dist/irdevice/other.js +128 -98
  71. package/dist/irdevice/other.js.map +1 -1
  72. package/dist/irdevice/tv.d.ts +10 -9
  73. package/dist/irdevice/tv.d.ts.map +1 -1
  74. package/dist/irdevice/tv.js +126 -93
  75. package/dist/irdevice/tv.js.map +1 -1
  76. package/dist/irdevice/vacuumcleaner.d.ts +7 -10
  77. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  78. package/dist/irdevice/vacuumcleaner.js +115 -89
  79. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  80. package/dist/irdevice/waterheater.d.ts +6 -9
  81. package/dist/irdevice/waterheater.d.ts.map +1 -1
  82. package/dist/irdevice/waterheater.js +115 -89
  83. package/dist/irdevice/waterheater.js.map +1 -1
  84. package/dist/platform.d.ts +3 -5
  85. package/dist/platform.d.ts.map +1 -1
  86. package/dist/platform.js +203 -200
  87. package/dist/platform.js.map +1 -1
  88. package/dist/settings.d.ts +15 -11
  89. package/dist/settings.d.ts.map +1 -1
  90. package/dist/settings.js.map +1 -1
  91. package/package.json +12 -10
  92. package/src/device/bot.ts +641 -657
  93. package/src/device/colorbulb.ts +440 -339
  94. package/src/device/contact.ts +289 -233
  95. package/src/device/curtain.ts +523 -503
  96. package/src/device/humidifier.ts +421 -396
  97. package/src/device/lock.ts +257 -147
  98. package/src/device/meter.ts +335 -268
  99. package/src/device/meterplus.ts +354 -213
  100. package/src/device/motion.ts +264 -213
  101. package/src/device/plug.ts +270 -251
  102. package/src/device/striplight.ts +552 -207
  103. package/src/irdevice/airconditioner.ts +263 -241
  104. package/src/irdevice/airpurifier.ts +132 -103
  105. package/src/irdevice/camera.ts +112 -85
  106. package/src/irdevice/fan.ts +138 -106
  107. package/src/irdevice/light.ts +116 -103
  108. package/src/irdevice/other.ts +124 -97
  109. package/src/irdevice/tv.ts +123 -93
  110. package/src/irdevice/vacuumcleaner.ts +112 -89
  111. package/src/irdevice/waterheater.ts +111 -89
  112. package/src/platform.ts +195 -216
  113. package/src/settings.ts +15 -11
package/src/platform.ts CHANGED
@@ -22,6 +22,7 @@ import https from 'https';
22
22
  import crypto from 'crypto';
23
23
  import { Buffer } from 'buffer';
24
24
  import fakegato from 'fakegato-history';
25
+ import superStringify from 'super-stringify';
25
26
  import { readFileSync, writeFileSync } from 'fs';
26
27
  import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, Service, Characteristic } from 'homebridge';
27
28
  import { PLATFORM_NAME, PLUGIN_NAME, irdevice, device, SwitchBotPlatformConfig, devicesConfig, DevicePath, HostDomain } from './settings';
@@ -65,9 +66,9 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
65
66
  this.verifyConfig();
66
67
  this.debugLog('Config OK');
67
68
  } catch (e: any) {
68
- this.errorLog(JSON.stringify(e.message));
69
+ this.errorLog(superStringify(e.message));
69
70
  if (this.platformLogging?.includes('debug')) {
70
- this.errorLog(JSON.stringify(e));
71
+ this.errorLog(superStringify(e));
71
72
  }
72
73
  return;
73
74
  }
@@ -85,13 +86,16 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
85
86
  try {
86
87
  if (this.config.credentials?.openToken && !this.config.credentials.token) {
87
88
  await this.updateToken();
89
+ } else if (!this.config.credentials?.secret) {
90
+ // eslint-disable-next-line no-useless-escape
91
+ this.errorLog('\"secret\" config is not populated, you must populate then please restart Homebridge.');
88
92
  } else {
89
93
  this.discoverDevices();
90
94
  }
91
95
  } catch (e: any) {
92
- this.errorLog('Failed to Discover Devices.', JSON.stringify(e.message));
96
+ this.errorLog('Failed to Discover Devices.', superStringify(e.message));
93
97
  if (this.platformLogging?.includes('debug')) {
94
- this.errorLog(JSON.stringify(e));
98
+ this.errorLog(superStringify(e));
95
99
  }
96
100
  }
97
101
  });
@@ -138,14 +142,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
138
142
  if (this.config.options.logging) {
139
143
  platformConfig['logging'] = this.config.options.logging;
140
144
  }
141
- if (this.config.options.logging) {
145
+ if (this.config.options.logging && this.config.options.refreshRate) {
142
146
  platformConfig['refreshRate'] = this.config.options.refreshRate;
143
147
  }
144
- if (this.config.options.logging) {
148
+ if (this.config.options.logging && this.config.options.pushRate) {
145
149
  platformConfig['pushRate'] = this.config.options.pushRate;
146
150
  }
147
151
  if (Object.entries(platformConfig).length !== 0) {
148
- this.infoLog(`Platform Config: ${JSON.stringify(platformConfig)}`);
152
+ this.infoLog(`Platform Config: ${superStringify(platformConfig)}`);
149
153
  }
150
154
 
151
155
  if (this.config.options) {
@@ -156,7 +160,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
156
160
  if (!deviceConfig.deviceId) {
157
161
  throw new Error('The devices config section is missing the *Device ID* in the config. Please check your config.');
158
162
  }
159
- if (!deviceConfig.configDeviceType && deviceConfig.ble) {
163
+ if (!deviceConfig.configDeviceType && deviceConfig.connectionType) {
160
164
  throw new Error('The devices config section is missing the *Device Type* in the config. Please check your config.');
161
165
  }
162
166
  }
@@ -188,35 +192,34 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
188
192
  if (this.platformLogging?.includes('debug')) {
189
193
  this.debugLog('Using Default Refresh Rate (2 minutes).');
190
194
  }
195
+ }
191
196
 
192
- if (!this.config.options.pushRate) {
197
+ if (!this.config.options.pushRate) {
193
198
  // default 100 milliseconds
194
199
  this.config.options!.pushRate! = 0.1;
195
200
  if (this.platformLogging?.includes('debug')) {
196
201
  this.warnLog('Using Default Push Rate.');
197
202
  }
198
- }
203
+ }
199
204
 
200
- if (!this.config.credentials) {
201
- if (this.platformLogging?.includes('debug')) {
202
- this.debugLog('Missing Credentials');
203
- }
205
+ if (!this.config.credentials) {
206
+ if (this.platformLogging?.includes('debug')) {
207
+ this.debugLog('Missing Credentials');
204
208
  }
205
- if (!this.config.credentials?.token) {
209
+ }
210
+ if (!this.config.credentials?.token) {
211
+ if (this.platformLogging?.includes('debug')) {
212
+ this.errorLog('Missing token');
213
+ this.warnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
214
+ }
215
+ }
216
+ if (this.config.credentials?.token) {
217
+ if (!this.config.credentials?.secret) {
206
218
  if (this.platformLogging?.includes('debug')) {
207
- this.errorLog('Missing token');
219
+ this.errorLog('Missing secret');
208
220
  this.warnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
209
221
  }
210
222
  }
211
- if (this.config.credentials?.token) {
212
- if (!this.config.credentials?.secret) {
213
- if (this.platformLogging?.includes('debug')) {
214
- this.errorLog('Missing secret');
215
- this.warnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
216
- }
217
- throw Error('Missing secret');
218
- }
219
- }
220
223
  }
221
224
  }
222
225
 
@@ -252,8 +255,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
252
255
  throw new Error('pluginConfig.credentials is not an object');
253
256
  }
254
257
  // Move openToken to token
255
- this.warnLog('This plugin has been updated to use OpenAPI v1.1, config is set with openToken.');
256
- this.warnLog('`openToken` config has been moved to the `token` config, Please restart Homebridge.');
258
+ if (!this.config.credentials.secret){
259
+ // eslint-disable-next-line no-useless-escape, max-len
260
+ this.warnLog('This plugin has been updated to use OpenAPI v1.1, config is set with openToken, \"openToken\" cconfig has been moved to the \"token\" config' );
261
+ // eslint-disable-next-line no-useless-escape
262
+ this.errorLog('\"secret\" config is not populated, you must populate then please restart Homebridge.');
263
+ } else {
264
+ // eslint-disable-next-line no-useless-escape, max-len
265
+ this.warnLog('This plugin has been updated to use OpenAPI v1.1, config is set with openToken, \"openToken\" config has been moved to the \"token\" config, please restart Homebridge.');
266
+ }
257
267
 
258
268
  // set the refresh token
259
269
  pluginConfig.credentials.token = this.config.credentials?.openToken;
@@ -297,7 +307,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
297
307
  };
298
308
  const req = https.request(options, (res) => {
299
309
  this.debugLog(`statusCode: ${res.statusCode}`);
300
- this.debugLog(`headers: ${JSON.stringify(res.headers)}`);
310
+ this.debugLog(`headers: ${superStringify(res.headers)}`);
301
311
  let rawData = '';
302
312
  res.on('data', (d) => {
303
313
  rawData += d;
@@ -306,7 +316,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
306
316
  res.on('end', () => {
307
317
  try {
308
318
  const devicesAPI = JSON.parse(rawData);
309
- this.debugLog(`devicesAPI: ${JSON.stringify(devicesAPI.body)}`);
319
+ this.debugLog(`devicesAPI: ${superStringify(devicesAPI.body)}`);
310
320
 
311
321
  // SwitchBot Devices
312
322
  if (devicesAPI.body.deviceList.length !== 0) {
@@ -316,7 +326,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
316
326
  }
317
327
  const deviceLists = devicesAPI.body.deviceList;
318
328
  if (!this.config.options?.devices) {
319
- this.debugLog(`SwitchBot Device Config Not Set: ${JSON.stringify(this.config.options?.devices)}`);
329
+ this.debugLog(`SwitchBot Device Config Not Set: ${superStringify(this.config.options?.devices)}`);
320
330
  const devices = deviceLists.map((v: any) => v);
321
331
  for (const device of devices) {
322
332
  if (device.deviceType) {
@@ -327,7 +337,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
327
337
  }
328
338
  }
329
339
  } else if (this.config.credentials?.token && this.config.options.devices) {
330
- this.debugLog(`SwitchBot Device Config Set: ${JSON.stringify(this.config.options?.devices)}`);
340
+ this.debugLog(`SwitchBot Device Config Set: ${superStringify(this.config.options?.devices)}`);
331
341
  const deviceConfigs = this.config.options?.devices;
332
342
 
333
343
  const mergeBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
@@ -340,7 +350,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
340
350
  }));
341
351
 
342
352
  const devices = mergeBydeviceId(deviceLists, deviceConfigs);
343
- this.debugLog(`SwitchBot Devices: ${JSON.stringify(devices)}`);
353
+ this.debugLog(`SwitchBot Devices: ${superStringify(devices)}`);
344
354
  for (const device of devices) {
345
355
  if (device.deviceType) {
346
356
  if (device.configDeviceName) {
@@ -361,7 +371,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
361
371
  }
362
372
  const irDeviceLists = devicesAPI.body.infraredRemoteList;
363
373
  if (!this.config.options?.irdevices) {
364
- this.debugLog(`IR Device Config Not Set: ${JSON.stringify(this.config.options?.irdevices)}`);
374
+ this.debugLog(`IR Device Config Not Set: ${superStringify(this.config.options?.irdevices)}`);
365
375
  const devices = irDeviceLists.map((v: any) => v);
366
376
  for (const device of devices) {
367
377
  if (device.remoteType) {
@@ -369,7 +379,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
369
379
  }
370
380
  }
371
381
  } else {
372
- this.debugLog(`IR Device Config Set: ${JSON.stringify(this.config.options?.irdevices)}`);
382
+ this.debugLog(`IR Device Config Set: ${superStringify(this.config.options?.irdevices)}`);
373
383
  const irDeviceConfig = this.config.options?.irdevices;
374
384
 
375
385
  const mergeIRBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
@@ -382,7 +392,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
382
392
  }));
383
393
 
384
394
  const devices = mergeIRBydeviceId(irDeviceLists, irDeviceConfig);
385
- this.debugLog(`IR Devices: ${JSON.stringify(devices)}`);
395
+ this.debugLog(`IR Devices: ${superStringify(devices)}`);
386
396
  for (const device of devices) {
387
397
  if (device.remoteType) {
388
398
  this.createIRDevice(device);
@@ -399,7 +409,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
399
409
  });
400
410
  req.end();
401
411
  } else if (!this.config.credentials?.token && this.config.options?.devices) {
402
- this.debugLog(`SwitchBot Device Manual Config Set: ${JSON.stringify(this.config.options?.devices)}`);
412
+ this.debugLog(`SwitchBot Device Manual Config Set: ${superStringify(this.config.options?.devices)}`);
403
413
  const deviceConfigs = this.config.options?.devices;
404
414
  const devices = deviceConfigs.map((v: any) => v);
405
415
  for (const device of devices) {
@@ -452,10 +462,10 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
452
462
  this.errorLog('Failed to Discover Devices');
453
463
  }
454
464
  if (this.platformLogging === 'debug') {
455
- this.errorLog(`Failed to Discover Devices, Error Message: ${JSON.stringify(e.message)}`);
465
+ this.errorLog(`Failed to Discover Devices, Error Message: ${superStringify(e.message)}`);
456
466
  }
457
467
  if (this.platformLogging === 'debugMode') {
458
- this.errorLog(`Failed to Discover Devices, Error: ${JSON.stringify(e)}`);
468
+ this.errorLog(`Failed to Discover Devices, Error: ${superStringify(e)}`);
459
469
  }
460
470
  }
461
471
 
@@ -591,23 +601,6 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
591
601
  }
592
602
  }
593
603
 
594
- private registerDevice(device: device & devicesConfig) {
595
- if (!device.hide_device && device.enableCloudService && device.ble) {
596
- this.registeringDevice = true;
597
- this.debugLog(`Device: ${device.deviceName} Both OpenAPI and BLE Connections Enabled`);
598
- } else if (!device.hide_device && device.deviceId && device.configDeviceType && device.configDeviceName && !device.enableCloudService) {
599
- this.registeringDevice = true;
600
- this.debugLog(`Device: ${device.deviceName} BLE Connection Enabled`);
601
- } else if (!device.hide_device && device.enableCloudService && !device.ble) {
602
- this.registeringDevice = true;
603
- this.debugLog(`Device: ${device.deviceName} OpenAPI Connection Enabled`);
604
- } else {
605
- this.registeringDevice = false;
606
- this.debugLog(`Device: ${device.deviceName} OpenAPI and BLE Are Not Enabled`);
607
- }
608
- return this.registeringDevice;
609
- }
610
-
611
604
  private async createHumidifier(device: device & devicesConfig) {
612
605
  const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
613
606
 
@@ -618,16 +611,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
618
611
  if (existingAccessory) {
619
612
  // the accessory already exists
620
613
  if (this.registerDevice(device)) {
621
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
622
-
623
614
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
624
- //existingAccessory.context.firmwareRevision = firmware;
625
615
  existingAccessory.context.model = device.deviceType;
626
616
  existingAccessory.context.deviceID = device.deviceId;
627
- existingAccessory.displayName = device.deviceName;
617
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
628
618
  existingAccessory.context.firmwareRevision = device.firmware;
629
619
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
630
- await this.connectionTypeExistingAccessory(device, existingAccessory);
620
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
621
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
631
622
  this.api.updatePlatformAccessories([existingAccessory]);
632
623
  // create the accessory handler for the restored accessory
633
624
  // this is imported from `platformAccessory.ts`
@@ -650,14 +641,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
650
641
  accessory.context.deviceID = device.deviceId;
651
642
  accessory.context.firmwareRevision = device.firmware;
652
643
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
653
- await this.connectionTypeNewAccessory(device, accessory);
644
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
654
645
  // create the accessory handler for the newly create accessory
655
646
  // this is imported from `platformAccessory.ts`
656
647
  new Humidifier(this, accessory, device);
657
648
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
658
649
 
659
- // link the accessory to your platform
660
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
650
+ // publish device externally or link the accessory to your platform
651
+ this.externalOrPlatform(device, accessory);
661
652
  this.accessories.push(accessory);
662
653
  } else {
663
654
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
@@ -674,17 +665,16 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
674
665
  if (existingAccessory) {
675
666
  // the accessory already exists
676
667
  if (this.registerDevice(device)) {
677
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
678
-
679
- this.debugLog(JSON.stringify(device.bot?.mode));
680
668
 
669
+ this.debugLog(superStringify(device.bot?.mode));
681
670
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
682
671
  existingAccessory.context.model = device.deviceType;
683
672
  existingAccessory.context.deviceID = device.deviceId;
684
- existingAccessory.displayName = device.deviceName;
673
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
685
674
  existingAccessory.context.firmwareRevision = device.firmware;
686
675
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
687
- await this.connectionTypeExistingAccessory(device, existingAccessory);
676
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
677
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
688
678
  this.api.updatePlatformAccessories([existingAccessory]);
689
679
  // create the accessory handler for the restored accessory
690
680
  // this is imported from `platformAccessory.ts`
@@ -697,7 +687,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
697
687
  // the accessory does not yet exist, so we need to create it
698
688
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
699
689
 
700
- this.debugLog(JSON.stringify(device.bot?.mode));
690
+ this.debugLog(superStringify(device.bot?.mode));
701
691
 
702
692
  // create a new accessory
703
693
  const accessory = new this.api.platformAccessory(device.deviceName, uuid);
@@ -709,15 +699,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
709
699
  accessory.context.deviceID = device.deviceId;
710
700
  accessory.context.firmwareRevision = device.firmware;
711
701
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
712
- await this.connectionTypeNewAccessory(device, accessory);
702
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
713
703
  // accessory.context.firmwareRevision = findaccessories.accessoryAttribute.softwareRevision;
714
704
  // create the accessory handler for the newly create accessory
715
705
  // this is imported from `platformAccessory.ts`
716
706
  new Bot(this, accessory, device);
717
707
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
718
708
 
719
- // link the accessory to your platform
720
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
709
+ // publish device externally or link the accessory to your platform
710
+ this.externalOrPlatform(device, accessory);
721
711
  this.accessories.push(accessory);
722
712
  } else {
723
713
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
@@ -734,15 +724,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
734
724
  if (existingAccessory) {
735
725
  // the accessory already exists
736
726
  if (this.registerDevice(device)) {
737
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
738
-
739
727
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
740
728
  existingAccessory.context.model = device.deviceType;
741
729
  existingAccessory.context.deviceID = device.deviceId;
742
- existingAccessory.displayName = device.deviceName;
730
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
743
731
  existingAccessory.context.firmwareRevision = device.firmware;
744
732
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
745
- await this.connectionTypeExistingAccessory(device, existingAccessory);
733
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
734
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
746
735
  this.api.updatePlatformAccessories([existingAccessory]);
747
736
  // create the accessory handler for the restored accessory
748
737
  // this is imported from `platformAccessory.ts`
@@ -765,14 +754,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
765
754
  accessory.context.deviceID = device.deviceId;
766
755
  accessory.context.firmwareRevision = device.firmware;
767
756
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
768
- await this.connectionTypeNewAccessory(device, accessory);
757
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
769
758
  // create the accessory handler for the newly create accessory
770
759
  // this is imported from `platformAccessory.ts`
771
760
  new Meter(this, accessory, device);
772
761
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
773
762
 
774
- // link the accessory to your platform
775
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
763
+ // publish device externally or link the accessory to your platform
764
+ this.externalOrPlatform(device, accessory);
776
765
  this.accessories.push(accessory);
777
766
  } else {
778
767
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
@@ -789,15 +778,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
789
778
  if (existingAccessory) {
790
779
  // the accessory already exists
791
780
  if (this.registerDevice(device)) {
792
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
793
-
794
781
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
795
782
  existingAccessory.context.model = device.deviceType;
796
783
  existingAccessory.context.deviceID = device.deviceId;
797
- existingAccessory.displayName = device.deviceName;
784
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
798
785
  existingAccessory.context.firmwareRevision = device.firmware;
799
786
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
800
- await this.connectionTypeExistingAccessory(device, existingAccessory);
787
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
788
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
801
789
  this.api.updatePlatformAccessories([existingAccessory]);
802
790
  // create the accessory handler for the restored accessory
803
791
  // this is imported from `platformAccessory.ts`
@@ -820,14 +808,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
820
808
  accessory.context.deviceID = device.deviceId;
821
809
  accessory.context.firmwareRevision = device.firmware;
822
810
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
823
- await this.connectionTypeNewAccessory(device, accessory);
811
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
824
812
  // create the accessory handler for the newly create accessory
825
813
  // this is imported from `platformAccessory.ts`
826
814
  new MeterPlus(this, accessory, device);
827
815
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
828
816
 
829
- // link the accessory to your platform
830
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
817
+ // publish device externally or link the accessory to your platform
818
+ this.externalOrPlatform(device, accessory);
831
819
  this.accessories.push(accessory);
832
820
  } else {
833
821
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
@@ -844,15 +832,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
844
832
  if (existingAccessory) {
845
833
  // the accessory already exists
846
834
  if (this.registerDevice(device)) {
847
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
848
-
849
835
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
850
836
  existingAccessory.context.model = device.deviceType;
851
837
  existingAccessory.context.deviceID = device.deviceId;
852
- existingAccessory.displayName = device.deviceName;
838
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
853
839
  existingAccessory.context.firmwareRevision = device.firmware;
854
840
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
855
- await this.connectionTypeExistingAccessory(device, existingAccessory);
841
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
842
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
856
843
  this.api.updatePlatformAccessories([existingAccessory]);
857
844
  // create the accessory handler for the restored accessory
858
845
  // this is imported from `platformAccessory.ts`
@@ -875,14 +862,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
875
862
  accessory.context.deviceID = device.deviceId;
876
863
  accessory.context.firmwareRevision = device.firmware;
877
864
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
878
- await this.connectionTypeNewAccessory(device, accessory);
865
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
879
866
  // create the accessory handler for the newly create accessory
880
867
  // this is imported from `platformAccessory.ts`
881
868
  new Motion(this, accessory, device);
882
869
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
883
870
 
884
- // link the accessory to your platform
885
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
871
+ // publish device externally or link the accessory to your platform
872
+ this.externalOrPlatform(device, accessory);
886
873
  this.accessories.push(accessory);
887
874
  } else {
888
875
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
@@ -899,15 +886,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
899
886
  if (existingAccessory) {
900
887
  // the accessory already exists
901
888
  if (this.registerDevice(device)) {
902
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
903
-
904
889
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
905
890
  existingAccessory.context.model = device.deviceType;
906
891
  existingAccessory.context.deviceID = device.deviceId;
907
- existingAccessory.displayName = device.deviceName;
892
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
908
893
  existingAccessory.context.firmwareRevision = device.firmware;
909
894
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
910
- await this.connectionTypeExistingAccessory(device, existingAccessory);
895
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
896
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
911
897
  this.api.updatePlatformAccessories([existingAccessory]);
912
898
  // create the accessory handler for the restored accessory
913
899
  // this is imported from `platformAccessory.ts`
@@ -930,14 +916,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
930
916
  accessory.context.deviceID = device.deviceId;
931
917
  accessory.context.firmwareRevision = device.firmware;
932
918
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
933
- await this.connectionTypeNewAccessory(device, accessory);
919
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
934
920
  // create the accessory handler for the newly create accessory
935
921
  // this is imported from `platformAccessory.ts`
936
922
  new Contact(this, accessory, device);
937
923
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
938
924
 
939
- // link the accessory to your platform
940
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
925
+ // publish device externally or link the accessory to your platform
926
+ this.externalOrPlatform(device, accessory);
941
927
  this.accessories.push(accessory);
942
928
  } else {
943
929
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
@@ -954,15 +940,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
954
940
  if (existingAccessory) {
955
941
  // the accessory already exists
956
942
  if (this.isCurtainGrouped(device)) {
957
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
958
-
959
943
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
960
944
  existingAccessory.context.model = device.deviceType;
961
945
  existingAccessory.context.deviceID = device.deviceId;
962
- existingAccessory.displayName = device.deviceName;
946
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
963
947
  existingAccessory.context.firmwareRevision = device.firmware;
964
948
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
965
- await this.connectionTypeExistingAccessory(device, existingAccessory);
949
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
950
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
966
951
  this.api.updatePlatformAccessories([existingAccessory]);
967
952
  // create the accessory handler for the restored accessory
968
953
  // this is imported from `platformAccessory.ts`
@@ -996,14 +981,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
996
981
  accessory.context.deviceID = device.deviceId;
997
982
  accessory.context.firmwareRevision = device.firmware;
998
983
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
999
- await this.connectionTypeNewAccessory(device, accessory);
984
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1000
985
  // create the accessory handler for the newly create accessory
1001
986
  // this is imported from `platformAccessory.ts`
1002
987
  new Curtain(this, accessory, device);
1003
988
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1004
989
 
1005
- // link the accessory to your platform
1006
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
990
+ // publish device externally or link the accessory to your platform
991
+ this.externalOrPlatform(device, accessory);
1007
992
  this.accessories.push(accessory);
1008
993
  } else {
1009
994
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
@@ -1035,15 +1020,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1035
1020
  if (existingAccessory) {
1036
1021
  // the accessory already exists
1037
1022
  if (this.registerDevice(device)) {
1038
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1039
-
1040
1023
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1041
1024
  existingAccessory.context.model = device.deviceType;
1042
1025
  existingAccessory.context.deviceID = device.deviceId;
1043
- existingAccessory.displayName = device.deviceName;
1026
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1044
1027
  existingAccessory.context.firmwareRevision = device.firmware;
1045
1028
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1046
- await this.connectionTypeExistingAccessory(device, existingAccessory);
1029
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1030
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1047
1031
  this.api.updatePlatformAccessories([existingAccessory]);
1048
1032
  // create the accessory handler for the restored accessory
1049
1033
  // this is imported from `platformAccessory.ts`
@@ -1066,14 +1050,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1066
1050
  accessory.context.deviceID = device.deviceId;
1067
1051
  accessory.context.firmwareRevision = device.firmware;
1068
1052
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1069
- await this.connectionTypeNewAccessory(device, accessory);
1053
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1070
1054
  // create the accessory handler for the newly create accessory
1071
1055
  // this is imported from `platformAccessory.ts`
1072
1056
  new Plug(this, accessory, device);
1073
1057
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1074
1058
 
1075
- // link the accessory to your platform
1076
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1059
+ // publish device externally or link the accessory to your platform
1060
+ this.externalOrPlatform(device, accessory);
1077
1061
  this.accessories.push(accessory);
1078
1062
  } else {
1079
1063
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
@@ -1090,15 +1074,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1090
1074
  if (existingAccessory) {
1091
1075
  // the accessory already exists
1092
1076
  if (this.registerDevice(device)) {
1093
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1094
-
1095
1077
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1096
1078
  existingAccessory.context.model = device.deviceType;
1097
1079
  existingAccessory.context.deviceID = device.deviceId;
1098
- existingAccessory.displayName = device.deviceName;
1080
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1099
1081
  existingAccessory.context.firmwareRevision = device.firmware;
1100
1082
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1101
- await this.connectionTypeExistingAccessory(device, existingAccessory);
1083
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1084
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1102
1085
  this.api.updatePlatformAccessories([existingAccessory]);
1103
1086
  // create the accessory handler for the restored accessory
1104
1087
  // this is imported from `platformAccessory.ts`
@@ -1121,14 +1104,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1121
1104
  accessory.context.deviceID = device.deviceId;
1122
1105
  accessory.context.firmwareRevision = device.firmware;
1123
1106
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1124
- await this.connectionTypeNewAccessory(device, accessory);
1107
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1125
1108
  // create the accessory handler for the newly create accessory
1126
1109
  // this is imported from `platformAccessory.ts`
1127
1110
  new Lock(this, accessory, device);
1128
1111
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1129
1112
 
1130
- // link the accessory to your platform
1131
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1113
+ // publish device externally or link the accessory to your platform
1114
+ this.externalOrPlatform(device, accessory);
1132
1115
  this.accessories.push(accessory);
1133
1116
  } else {
1134
1117
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
@@ -1145,15 +1128,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1145
1128
  if (existingAccessory) {
1146
1129
  // the accessory already exists
1147
1130
  if (this.registerDevice(device)) {
1148
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1149
-
1150
1131
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1151
1132
  existingAccessory.context.model = device.deviceType;
1152
1133
  existingAccessory.context.deviceID = device.deviceId;
1153
- existingAccessory.displayName = device.deviceName;
1134
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1154
1135
  existingAccessory.context.firmwareRevision = device.firmware;
1155
1136
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1156
- await this.connectionTypeExistingAccessory(device, existingAccessory);
1137
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1138
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1157
1139
  this.api.updatePlatformAccessories([existingAccessory]);
1158
1140
  // create the accessory handler for the restored accessory
1159
1141
  // this is imported from `platformAccessory.ts`
@@ -1176,14 +1158,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1176
1158
  accessory.context.deviceID = device.deviceId;
1177
1159
  accessory.context.firmwareRevision = device.firmware;
1178
1160
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1179
- await this.connectionTypeNewAccessory(device, accessory);
1161
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1180
1162
  // create the accessory handler for the newly create accessory
1181
1163
  // this is imported from `platformAccessory.ts`
1182
1164
  new ColorBulb(this, accessory, device);
1183
1165
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1184
1166
 
1185
- // link the accessory to your platform
1186
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1167
+ // publish device externally or link the accessory to your platform
1168
+ this.externalOrPlatform(device, accessory);
1187
1169
  this.accessories.push(accessory);
1188
1170
  } else {
1189
1171
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
@@ -1200,15 +1182,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1200
1182
  if (existingAccessory) {
1201
1183
  // the accessory already exists
1202
1184
  if (this.registerDevice(device)) {
1203
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1204
-
1205
1185
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1206
1186
  existingAccessory.context.model = device.deviceType;
1207
1187
  existingAccessory.context.deviceID = device.deviceId;
1208
- existingAccessory.displayName = device.deviceName;
1188
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1209
1189
  existingAccessory.context.firmwareRevision = device.firmware;
1210
1190
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1211
- await this.connectionTypeExistingAccessory(device, existingAccessory);
1191
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1192
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1212
1193
  this.api.updatePlatformAccessories([existingAccessory]);
1213
1194
  // create the accessory handler for the restored accessory
1214
1195
  // this is imported from `platformAccessory.ts`
@@ -1231,14 +1212,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1231
1212
  accessory.context.deviceID = device.deviceId;
1232
1213
  accessory.context.firmwareRevision = device.firmware;
1233
1214
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1234
- await this.connectionTypeNewAccessory(device, accessory);
1215
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1235
1216
  // create the accessory handler for the newly create accessory
1236
1217
  // this is imported from `platformAccessory.ts`
1237
1218
  new StripLight(this, accessory, device);
1238
1219
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1239
1220
 
1240
- // link the accessory to your platform
1241
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1221
+ // publish device externally or link the accessory to your platform
1222
+ this.externalOrPlatform(device, accessory);
1242
1223
  this.accessories.push(accessory);
1243
1224
  } else {
1244
1225
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
@@ -1253,15 +1234,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1253
1234
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1254
1235
 
1255
1236
  if (!device.hide_device && existingAccessory) {
1256
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1257
-
1258
1237
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1259
1238
  existingAccessory.context.model = device.remoteType;
1260
1239
  existingAccessory.context.deviceID = device.deviceId;
1261
- existingAccessory.displayName = device.deviceName;
1240
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1262
1241
  existingAccessory.context.firmwareRevision = device.firmware;
1263
1242
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1264
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1243
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1244
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1265
1245
  this.api.updatePlatformAccessories([existingAccessory]);
1266
1246
  // create the accessory handler for the restored accessory
1267
1247
  // this is imported from `platformAccessory.ts`
@@ -1281,7 +1261,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1281
1261
  accessory.context.deviceID = device.deviceId;
1282
1262
  accessory.context.firmwareRevision = device.firmware;
1283
1263
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1284
- await this.connectionTypeNewIRAccessory(accessory);
1264
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1285
1265
  // create the accessory handler for the newly create accessory
1286
1266
  // this is imported from `platformAccessory.ts`
1287
1267
  new TV(this, accessory, device);
@@ -1292,7 +1272,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1292
1272
  * Only one TV can exist per bridge, to bypass this limitation, you should
1293
1273
  * publish your TV as an external accessory.
1294
1274
  */
1295
- this.api.publishExternalAccessories(PLUGIN_NAME, [accessory]);
1275
+ this.externalAccessory(accessory);
1296
1276
  this.accessories.push(accessory);
1297
1277
  } else {
1298
1278
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
@@ -1309,16 +1289,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1309
1289
  if (existingAccessory) {
1310
1290
  // the accessory already exists
1311
1291
  if (!device.hide_device && device.hubDeviceId) {
1312
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1313
-
1314
1292
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1315
-
1316
1293
  existingAccessory.context.model = device.remoteType;
1317
1294
  existingAccessory.context.deviceID = device.deviceId;
1318
- existingAccessory.displayName = device.deviceName;
1295
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1319
1296
  existingAccessory.context.firmwareRevision = device.firmware;
1320
1297
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1321
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1298
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1299
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1322
1300
  this.api.updatePlatformAccessories([existingAccessory]);
1323
1301
  // create the accessory handler for the restored accessory
1324
1302
  // this is imported from `platformAccessory.ts`
@@ -1341,14 +1319,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1341
1319
  accessory.context.deviceID = device.deviceId;
1342
1320
  accessory.context.firmwareRevision = device.firmware;
1343
1321
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1344
- await this.connectionTypeNewIRAccessory(accessory);
1322
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1345
1323
  // create the accessory handler for the newly create accessory
1346
1324
  // this is imported from `platformAccessory.ts`
1347
1325
  new Fan(this, accessory, device);
1348
1326
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1349
1327
 
1350
- // link the accessory to your platform
1351
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1328
+ // publish device externally or link the accessory to your platform
1329
+ this.externalOrPlatform(device, accessory);
1352
1330
  this.accessories.push(accessory);
1353
1331
  } else {
1354
1332
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
@@ -1365,15 +1343,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1365
1343
  if (existingAccessory) {
1366
1344
  // the accessory already exists
1367
1345
  if (!device.hide_device && device.hubDeviceId) {
1368
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1369
-
1370
1346
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1371
1347
  existingAccessory.context.model = device.remoteType;
1372
1348
  existingAccessory.context.deviceID = device.deviceId;
1373
- existingAccessory.displayName = device.deviceName;
1349
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1374
1350
  existingAccessory.context.firmwareRevision = device.firmware;
1375
1351
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1376
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1352
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1353
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1377
1354
  this.api.updatePlatformAccessories([existingAccessory]);
1378
1355
  // create the accessory handler for the restored accessory
1379
1356
  // this is imported from `platformAccessory.ts`
@@ -1396,14 +1373,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1396
1373
  accessory.context.deviceID = device.deviceId;
1397
1374
  accessory.context.firmwareRevision = device.firmware;
1398
1375
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1399
- await this.connectionTypeNewIRAccessory(accessory);
1376
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1400
1377
  // create the accessory handler for the newly create accessory
1401
1378
  // this is imported from `platformAccessory.ts`
1402
1379
  new Light(this, accessory, device);
1403
1380
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1404
1381
 
1405
- // link the accessory to your platform
1406
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1382
+ // publish device externally or link the accessory to your platform
1383
+ this.externalOrPlatform(device, accessory);
1407
1384
  this.accessories.push(accessory);
1408
1385
  } else {
1409
1386
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
@@ -1420,15 +1397,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1420
1397
  if (existingAccessory) {
1421
1398
  // the accessory already exists
1422
1399
  if (!device.hide_device && device.hubDeviceId) {
1423
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1424
-
1425
1400
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1426
1401
  existingAccessory.context.model = device.remoteType;
1427
1402
  existingAccessory.context.deviceID = device.deviceId;
1428
- existingAccessory.displayName = device.deviceName;
1403
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1429
1404
  existingAccessory.context.firmwareRevision = device.firmware;
1430
1405
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1431
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1406
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1407
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1432
1408
  this.api.updatePlatformAccessories([existingAccessory]);
1433
1409
  // create the accessory handler for the restored accessory
1434
1410
  // this is imported from `platformAccessory.ts`
@@ -1451,14 +1427,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1451
1427
  accessory.context.deviceID = device.deviceId;
1452
1428
  accessory.context.firmwareRevision = device.firmware;
1453
1429
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1454
- await this.connectionTypeNewIRAccessory(accessory);
1430
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1455
1431
  // create the accessory handler for the newly create accessory
1456
1432
  // this is imported from `platformAccessory.ts`
1457
1433
  new AirConditioner(this, accessory, device);
1458
1434
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1459
1435
 
1460
- // link the accessory to your platform
1461
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1436
+ // publish device externally or link the accessory to your platform
1437
+ this.externalOrPlatform(device, accessory);
1462
1438
  this.accessories.push(accessory);
1463
1439
  } else {
1464
1440
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
@@ -1475,15 +1451,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1475
1451
  if (existingAccessory) {
1476
1452
  // the accessory already exists
1477
1453
  if (!device.hide_device && device.hubDeviceId) {
1478
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1479
-
1480
1454
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1481
1455
  existingAccessory.context.model = device.remoteType;
1482
1456
  existingAccessory.context.deviceID = device.deviceId;
1483
- existingAccessory.displayName = device.deviceName;
1457
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1484
1458
  existingAccessory.context.firmwareRevision = device.firmware;
1485
1459
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1486
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1460
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1461
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1487
1462
  this.api.updatePlatformAccessories([existingAccessory]);
1488
1463
  // create the accessory handler for the restored accessory
1489
1464
  // this is imported from `platformAccessory.ts`
@@ -1506,14 +1481,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1506
1481
  accessory.context.deviceID = device.deviceId;
1507
1482
  accessory.context.firmwareRevision = device.firmware;
1508
1483
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1509
- await this.connectionTypeNewIRAccessory(accessory);
1484
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1510
1485
  // create the accessory handler for the newly create accessory
1511
1486
  // this is imported from `platformAccessory.ts`
1512
1487
  new AirPurifier(this, accessory, device);
1513
1488
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1514
1489
 
1515
- // link the accessory to your platform
1516
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1490
+ // publish device externally or link the accessory to your platform
1491
+ this.externalOrPlatform(device, accessory);
1517
1492
  this.accessories.push(accessory);
1518
1493
  } else {
1519
1494
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
@@ -1530,15 +1505,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1530
1505
  if (existingAccessory) {
1531
1506
  // the accessory already exists
1532
1507
  if (!device.hide_device && device.hubDeviceId) {
1533
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1534
-
1535
1508
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1536
1509
  existingAccessory.context.model = device.remoteType;
1537
1510
  existingAccessory.context.deviceID = device.deviceId;
1538
- existingAccessory.displayName = device.deviceName;
1511
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1539
1512
  existingAccessory.context.firmwareRevision = device.firmware;
1540
1513
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1541
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1514
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1515
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1542
1516
  this.api.updatePlatformAccessories([existingAccessory]);
1543
1517
  // create the accessory handler for the restored accessory
1544
1518
  // this is imported from `platformAccessory.ts`
@@ -1561,14 +1535,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1561
1535
  accessory.context.deviceID = device.deviceId;
1562
1536
  accessory.context.firmwareRevision = device.firmware;
1563
1537
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1564
- await this.connectionTypeNewIRAccessory(accessory);
1538
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1565
1539
  // create the accessory handler for the newly create accessory
1566
1540
  // this is imported from `platformAccessory.ts`
1567
1541
  new WaterHeater(this, accessory, device);
1568
1542
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1569
1543
 
1570
- // link the accessory to your platform
1571
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1544
+ // publish device externally or link the accessory to your platform
1545
+ this.externalOrPlatform(device, accessory);
1572
1546
  this.accessories.push(accessory);
1573
1547
  } else {
1574
1548
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
@@ -1585,15 +1559,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1585
1559
  if (existingAccessory) {
1586
1560
  // the accessory already exists
1587
1561
  if (!device.hide_device && device.hubDeviceId) {
1588
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1589
-
1590
1562
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1591
1563
  existingAccessory.context.model = device.remoteType;
1592
1564
  existingAccessory.context.deviceID = device.deviceId;
1593
- existingAccessory.displayName = device.deviceName;
1565
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1594
1566
  existingAccessory.context.firmwareRevision = device.firmware;
1595
1567
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1596
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1568
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1569
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1597
1570
  this.api.updatePlatformAccessories([existingAccessory]);
1598
1571
  // create the accessory handler for the restored accessory
1599
1572
  // this is imported from `platformAccessory.ts`
@@ -1616,14 +1589,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1616
1589
  accessory.context.deviceID = device.deviceId;
1617
1590
  accessory.context.firmwareRevision = device.firmware;
1618
1591
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1619
- await this.connectionTypeNewIRAccessory(accessory);
1592
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1620
1593
  // create the accessory handler for the newly create accessory
1621
1594
  // this is imported from `platformAccessory.ts`
1622
1595
  new VacuumCleaner(this, accessory, device);
1623
1596
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1624
1597
 
1625
- // link the accessory to your platform
1626
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1598
+ // publish device externally or link the accessory to your platform
1599
+ this.externalOrPlatform(device, accessory);
1627
1600
  this.accessories.push(accessory);
1628
1601
  } else {
1629
1602
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
@@ -1640,15 +1613,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1640
1613
  if (existingAccessory) {
1641
1614
  // the accessory already exists
1642
1615
  if (!device.hide_device && device.hubDeviceId) {
1643
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1644
-
1645
1616
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1646
1617
  existingAccessory.context.model = device.remoteType;
1647
1618
  existingAccessory.context.deviceID = device.deviceId;
1648
- existingAccessory.displayName = device.deviceName;
1619
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1649
1620
  existingAccessory.context.firmwareRevision = device.firmware;
1650
1621
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1651
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1622
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1623
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1652
1624
  this.api.updatePlatformAccessories([existingAccessory]);
1653
1625
  // create the accessory handler for the restored accessory
1654
1626
  // this is imported from `platformAccessory.ts`
@@ -1671,14 +1643,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1671
1643
  accessory.context.deviceID = device.deviceId;
1672
1644
  accessory.context.firmwareRevision = device.firmware;
1673
1645
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1674
- await this.connectionTypeNewIRAccessory(accessory);
1646
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1675
1647
  // create the accessory handler for the newly create accessory
1676
1648
  // this is imported from `platformAccessory.ts`
1677
1649
  new Camera(this, accessory, device);
1678
1650
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1679
1651
 
1680
- // link the accessory to your platform
1681
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1652
+ // publish device externally or link the accessory to your platform
1653
+ this.externalOrPlatform(device, accessory);
1682
1654
  this.accessories.push(accessory);
1683
1655
  } else {
1684
1656
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
@@ -1695,15 +1667,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1695
1667
  if (existingAccessory) {
1696
1668
  // the accessory already exists
1697
1669
  if (!device.hide_device && device.hubDeviceId) {
1698
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1699
-
1700
1670
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1701
1671
  existingAccessory.context.model = device.remoteType;
1702
1672
  existingAccessory.context.deviceID = device.deviceId;
1703
- existingAccessory.displayName = device.deviceName;
1673
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1704
1674
  existingAccessory.context.firmwareRevision = device.firmware;
1705
1675
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1706
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1676
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1677
+ existingAccessory.context.connectionType = device.connectionType || 'OpenAPI';
1707
1678
  this.api.updatePlatformAccessories([existingAccessory]);
1708
1679
  // create the accessory handler for the restored accessory
1709
1680
  // this is imported from `platformAccessory.ts`
@@ -1726,42 +1697,50 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1726
1697
  accessory.context.deviceID = device.deviceId;
1727
1698
  accessory.context.firmwareRevision = device.firmware;
1728
1699
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1729
- await this.connectionTypeNewIRAccessory(accessory);
1700
+ accessory.context.connectionType = device.connectionType || 'OpenAPI';
1730
1701
  // create the accessory handler for the newly create accessory
1731
1702
  // this is imported from `platformAccessory.ts`
1732
1703
  new Others(this, accessory, device);
1733
1704
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1734
1705
 
1735
- // link the accessory to your platform
1736
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1706
+ // publish device externally or link the accessory to your platform
1707
+ this.externalOrPlatform(device, accessory);
1737
1708
  this.accessories.push(accessory);
1738
1709
  } else {
1739
1710
  this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1740
1711
  }
1741
1712
  }
1742
1713
 
1743
- public async connectionTypeNewAccessory(device: device & devicesConfig, accessory: PlatformAccessory) {
1744
- if (device.ble) {
1745
- accessory.context.connectionType = 'BLE';
1714
+ private registerDevice(device: device & devicesConfig) {
1715
+ if (!device.hide_device && device.enableCloudService && device.connectionType === 'Both') {
1716
+ this.registeringDevice = true;
1717
+ this.debugLog(`Device: ${device.deviceName} Both OpenAPI and BLE Connections Enabled`);
1718
+ } else if (!device.hide_device && device.deviceId && device.configDeviceType && device.configDeviceName
1719
+ && !device.enableCloudService && device.connectionType === 'BLE') {
1720
+ this.registeringDevice = true;
1721
+ this.debugLog(`Device: ${device.deviceName} BLE Connection Enabled`);
1722
+ } else if (!device.hide_device && device.enableCloudService && device.connectionType === 'OpenAPI') {
1723
+ this.registeringDevice = true;
1724
+ this.debugLog(`Device: ${device.deviceName} OpenAPI Connection Enabled`);
1746
1725
  } else {
1747
- accessory.context.connectionType = 'OpenAPI';
1726
+ this.registeringDevice = false;
1727
+ this.debugLog(`Device: ${device.deviceName} OpenAPI and BLE Are Not Enabled`);
1748
1728
  }
1729
+ return this.registeringDevice;
1749
1730
  }
1750
1731
 
1751
- public async connectionTypeExistingAccessory(device: device & devicesConfig, existingAccessory: PlatformAccessory) {
1752
- if (device.ble) {
1753
- existingAccessory.context.connectionType = 'BLE';
1732
+ public async externalOrPlatform(device: device & devicesConfig, accessory: PlatformAccessory) {
1733
+ this.debugLog(`${accessory.displayName} External Accessory Mode: ${device.external}`);
1734
+ if (device.external) {
1735
+ this.warnLog(`${accessory.displayName} External Accessory Mode`);
1736
+ this.externalAccessory(accessory);
1754
1737
  } else {
1755
- existingAccessory.context.connectionType = 'OpenAPI';
1738
+ this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1756
1739
  }
1757
1740
  }
1758
1741
 
1759
- public async connectionTypeNewIRAccessory(accessory: PlatformAccessory) {
1760
- accessory.context.connectionType = 'IR wirth OpenAPI';
1761
- }
1762
-
1763
- public async connectionTypeExistingIRAccessory(existingAccessory: PlatformAccessory) {
1764
- existingAccessory.context.connectionType = 'IR with OpenAPI';
1742
+ public async externalAccessory(accessory: PlatformAccessory) {
1743
+ this.api.publishExternalAccessories(PLUGIN_NAME, [accessory]);
1765
1744
  }
1766
1745
 
1767
1746
  public unregisterPlatformAccessories(existingAccessory: PlatformAccessory) {
@@ -1772,13 +1751,13 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1772
1751
 
1773
1752
  public deviceListInfo(devices) {
1774
1753
  if (this.platformLogging?.includes('debug')) {
1775
- this.warnLog(`deviceListInfoStatus: ${JSON.stringify(devices)}`);
1754
+ this.warnLog(`deviceListInfoStatus: ${superStringify(devices)}`);
1776
1755
  }
1777
1756
  }
1778
1757
 
1779
1758
  public async deviceInfo(device: (irdevice & devicesConfig) | (device & devicesConfig)) {
1780
1759
  if (this.platformLogging?.includes('debug')) {
1781
- this.warnLog(JSON.stringify(device));
1760
+ this.warnLog(superStringify(device));
1782
1761
 
1783
1762
  const t = Date.now();
1784
1763
  const nonce = 'requestID';
@@ -1812,9 +1791,9 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1812
1791
 
1813
1792
  this.deviceStatus = req;
1814
1793
  if (this.deviceStatus.message === 'success') {
1815
- this.warnLog(`${device.deviceName} deviceInfoStatus: ${JSON.stringify(this.deviceStatus)}`);
1794
+ this.warnLog(`${device.deviceName} deviceInfoStatus: ${superStringify(this.deviceStatus)}`);
1816
1795
  } else {
1817
- this.warnLog(`${device.deviceName} deviceInfoStatus: ${JSON.stringify(this.deviceStatus.message)}`);
1796
+ this.warnLog(`${device.deviceName} deviceInfoStatus: ${superStringify(this.deviceStatus.message)}`);
1818
1797
  this.warnLog('Unable to retreive deviceInfoStatus.');
1819
1798
  }
1820
1799
  }