@switchbot/homebridge-switchbot 2.0.0-beta.3 → 2.0.0-beta.30

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 +10 -1
  2. package/README.md +1 -2
  3. package/config.schema.json +102 -20
  4. package/dist/device/bot.d.ts +34 -28
  5. package/dist/device/bot.d.ts.map +1 -1
  6. package/dist/device/bot.js +655 -649
  7. package/dist/device/bot.js.map +1 -1
  8. package/dist/device/colorbulb.d.ts +36 -19
  9. package/dist/device/colorbulb.d.ts.map +1 -1
  10. package/dist/device/colorbulb.js +483 -309
  11. package/dist/device/colorbulb.js.map +1 -1
  12. package/dist/device/contact.d.ts +18 -8
  13. package/dist/device/contact.d.ts.map +1 -1
  14. package/dist/device/contact.js +303 -237
  15. package/dist/device/contact.js.map +1 -1
  16. package/dist/device/curtain.d.ts +23 -16
  17. package/dist/device/curtain.d.ts.map +1 -1
  18. package/dist/device/curtain.js +541 -498
  19. package/dist/device/curtain.js.map +1 -1
  20. package/dist/device/humidifier.d.ts +24 -18
  21. package/dist/device/humidifier.d.ts.map +1 -1
  22. package/dist/device/humidifier.js +450 -369
  23. package/dist/device/humidifier.js.map +1 -1
  24. package/dist/device/lock.d.ts +27 -8
  25. package/dist/device/lock.d.ts.map +1 -1
  26. package/dist/device/lock.js +302 -148
  27. package/dist/device/lock.js.map +1 -1
  28. package/dist/device/meter.d.ts +22 -16
  29. package/dist/device/meter.d.ts.map +1 -1
  30. package/dist/device/meter.js +340 -262
  31. package/dist/device/meter.js.map +1 -1
  32. package/dist/device/meterplus.d.ts +24 -12
  33. package/dist/device/meterplus.d.ts.map +1 -1
  34. package/dist/device/meterplus.js +359 -213
  35. package/dist/device/meterplus.js.map +1 -1
  36. package/dist/device/motion.d.ts +18 -8
  37. package/dist/device/motion.d.ts.map +1 -1
  38. package/dist/device/motion.js +276 -216
  39. package/dist/device/motion.js.map +1 -1
  40. package/dist/device/plug.d.ts +27 -17
  41. package/dist/device/plug.d.ts.map +1 -1
  42. package/dist/device/plug.js +292 -251
  43. package/dist/device/plug.js.map +1 -1
  44. package/dist/device/striplight.d.ts +43 -19
  45. package/dist/device/striplight.d.ts.map +1 -1
  46. package/dist/device/striplight.js +503 -262
  47. package/dist/device/striplight.js.map +1 -1
  48. package/dist/irdevice/airconditioner.d.ts +9 -12
  49. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  50. package/dist/irdevice/airconditioner.js +275 -230
  51. package/dist/irdevice/airconditioner.js.map +1 -1
  52. package/dist/irdevice/airpurifier.d.ts +5 -3
  53. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  54. package/dist/irdevice/airpurifier.js +151 -104
  55. package/dist/irdevice/airpurifier.js.map +1 -1
  56. package/dist/irdevice/camera.d.ts +11 -8
  57. package/dist/irdevice/camera.d.ts.map +1 -1
  58. package/dist/irdevice/camera.js +130 -86
  59. package/dist/irdevice/camera.js.map +1 -1
  60. package/dist/irdevice/fan.d.ts +10 -7
  61. package/dist/irdevice/fan.d.ts.map +1 -1
  62. package/dist/irdevice/fan.js +157 -109
  63. package/dist/irdevice/fan.js.map +1 -1
  64. package/dist/irdevice/light.d.ts +11 -8
  65. package/dist/irdevice/light.d.ts.map +1 -1
  66. package/dist/irdevice/light.js +134 -103
  67. package/dist/irdevice/light.js.map +1 -1
  68. package/dist/irdevice/other.d.ts +11 -8
  69. package/dist/irdevice/other.d.ts.map +1 -1
  70. package/dist/irdevice/other.js +142 -98
  71. package/dist/irdevice/other.js.map +1 -1
  72. package/dist/irdevice/tv.d.ts +12 -9
  73. package/dist/irdevice/tv.d.ts.map +1 -1
  74. package/dist/irdevice/tv.js +140 -93
  75. package/dist/irdevice/tv.js.map +1 -1
  76. package/dist/irdevice/vacuumcleaner.d.ts +9 -10
  77. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  78. package/dist/irdevice/vacuumcleaner.js +129 -89
  79. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  80. package/dist/irdevice/waterheater.d.ts +8 -9
  81. package/dist/irdevice/waterheater.d.ts.map +1 -1
  82. package/dist/irdevice/waterheater.js +129 -89
  83. package/dist/irdevice/waterheater.js.map +1 -1
  84. package/dist/platform.d.ts +14 -12
  85. package/dist/platform.d.ts.map +1 -1
  86. package/dist/platform.js +479 -434
  87. package/dist/platform.js.map +1 -1
  88. package/dist/settings.d.ts +18 -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 +654 -660
  93. package/src/device/colorbulb.ts +487 -344
  94. package/src/device/contact.ts +306 -241
  95. package/src/device/curtain.ts +546 -512
  96. package/src/device/humidifier.ts +453 -403
  97. package/src/device/lock.ts +311 -149
  98. package/src/device/meter.ts +351 -275
  99. package/src/device/meterplus.ts +369 -219
  100. package/src/device/motion.ts +282 -222
  101. package/src/device/plug.ts +296 -260
  102. package/src/device/striplight.ts +519 -289
  103. package/src/irdevice/airconditioner.ts +279 -241
  104. package/src/irdevice/airpurifier.ts +148 -103
  105. package/src/irdevice/camera.ts +128 -85
  106. package/src/irdevice/fan.ts +154 -106
  107. package/src/irdevice/light.ts +132 -103
  108. package/src/irdevice/other.ts +140 -97
  109. package/src/irdevice/tv.ts +139 -93
  110. package/src/irdevice/vacuumcleaner.ts +128 -89
  111. package/src/irdevice/waterheater.ts +127 -89
  112. package/src/platform.ts +469 -456
  113. package/src/settings.ts +18 -11
package/src/platform.ts CHANGED
@@ -22,9 +22,12 @@ 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';
29
+ import { IncomingMessage } from 'http';
30
+ import { interval, take } from 'rxjs';
28
31
 
29
32
  /**
30
33
  * HomebridgePlatform
@@ -40,7 +43,6 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
40
43
 
41
44
  version = require('../package.json').version || '1.12.8'; // eslint-disable-line @typescript-eslint/no-var-requires
42
45
  deviceStatus!: any;
43
- registeringDevice!: boolean;
44
46
  debugMode!: boolean;
45
47
  platformLogging?: string;
46
48
 
@@ -48,7 +50,6 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
48
50
 
49
51
  constructor(public readonly log: Logger, public readonly config: SwitchBotPlatformConfig, public readonly api: API) {
50
52
  this.logs();
51
-
52
53
  this.debugLog('Finished initializing platform:', this.config.name);
53
54
  // only load if configured
54
55
  if (!this.config) {
@@ -65,10 +66,8 @@ 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
- if (this.platformLogging?.includes('debug')) {
70
- this.errorLog(JSON.stringify(e));
71
- }
69
+ this.errorLog(`Verify Config, Error Message: ${e.message}`);
70
+ this.debugErrorLog(`Verify Config, Error: ${e}`);
72
71
  return;
73
72
  }
74
73
 
@@ -85,38 +84,19 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
85
84
  try {
86
85
  if (this.config.credentials?.openToken && !this.config.credentials.token) {
87
86
  await this.updateToken();
87
+ } else if (this.config.credentials?.token && !this.config.credentials?.secret) {
88
+ // eslint-disable-next-line no-useless-escape
89
+ this.errorLog('\"secret\" config is not populated, you must populate then please restart Homebridge.');
88
90
  } else {
89
91
  this.discoverDevices();
90
92
  }
91
93
  } catch (e: any) {
92
- this.errorLog('Failed to Discover Devices.', JSON.stringify(e.message));
93
- if (this.platformLogging?.includes('debug')) {
94
- this.errorLog(JSON.stringify(e));
95
- }
94
+ this.errorLog(`Failed to Discover, Error Message: ${e.message}`);
95
+ this.debugErrorLog(`Failed to Discover, Error: ${e}`);
96
96
  }
97
97
  });
98
98
  }
99
99
 
100
- logs() {
101
- this.debugMode = process.argv.includes('-D') || process.argv.includes('--debug');
102
- if (this.config.options?.logging === 'debug' || this.config.options?.logging === 'standard' || this.config.options?.logging === 'none') {
103
- this.platformLogging = this.config.options!.logging;
104
- if (this.platformLogging.includes('debug')) {
105
- this.log.warn(`Using Config Logging: ${this.platformLogging}`);
106
- }
107
- } else if (this.debugMode) {
108
- this.platformLogging = 'debugMode';
109
- if (this.platformLogging?.includes('debug')) {
110
- this.log.warn(`Using ${this.platformLogging} Logging`);
111
- }
112
- } else {
113
- this.platformLogging = 'standard';
114
- if (this.platformLogging?.includes('debug')) {
115
- this.log.warn(`Using ${this.platformLogging} Logging`);
116
- }
117
- }
118
- }
119
-
120
100
  /**
121
101
  * This function is invoked when homebridge restores cached accessories from disk at startup.
122
102
  * It should be used to setup event handlers for characteristics and update respective values.
@@ -138,14 +118,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
138
118
  if (this.config.options.logging) {
139
119
  platformConfig['logging'] = this.config.options.logging;
140
120
  }
141
- if (this.config.options.logging) {
121
+ if (this.config.options.logging && this.config.options.refreshRate) {
142
122
  platformConfig['refreshRate'] = this.config.options.refreshRate;
143
123
  }
144
- if (this.config.options.logging) {
124
+ if (this.config.options.logging && this.config.options.pushRate) {
145
125
  platformConfig['pushRate'] = this.config.options.pushRate;
146
126
  }
147
127
  if (Object.entries(platformConfig).length !== 0) {
148
- this.infoLog(`Platform Config: ${JSON.stringify(platformConfig)}`);
128
+ this.infoLog(`Platform Config: ${superStringify(platformConfig)}`);
149
129
  }
150
130
 
151
131
  if (this.config.options) {
@@ -156,7 +136,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
156
136
  if (!deviceConfig.deviceId) {
157
137
  throw new Error('The devices config section is missing the *Device ID* in the config. Please check your config.');
158
138
  }
159
- if (!deviceConfig.configDeviceType && deviceConfig.ble) {
139
+ if (!deviceConfig.configDeviceType && deviceConfig.connectionType) {
160
140
  throw new Error('The devices config section is missing the *Device Type* in the config. Please check your config.');
161
141
  }
162
142
  }
@@ -185,39 +165,39 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
185
165
  if (!this.config.options.refreshRate) {
186
166
  // default 120 seconds (2 minutes)
187
167
  this.config.options!.refreshRate! = 120;
188
- if (this.platformLogging?.includes('debug')) {
189
- this.debugLog('Using Default Refresh Rate (2 minutes).');
190
- }
168
+ this.debugWarnLog('Using Default Refresh Rate (2 minutes).');
169
+ }
191
170
 
192
- if (!this.config.options.pushRate) {
171
+ if (!this.config.options.pushRate) {
193
172
  // default 100 milliseconds
194
173
  this.config.options!.pushRate! = 0.1;
195
- if (this.platformLogging?.includes('debug')) {
196
- this.warnLog('Using Default Push Rate.');
197
- }
198
- }
174
+ this.debugWarnLog('Using Default Push Rate.');
175
+ }
199
176
 
200
- if (!this.config.credentials) {
201
- if (this.platformLogging?.includes('debug')) {
202
- this.debugLog('Missing Credentials');
203
- }
204
- }
177
+ if (!this.config.credentials && !this.config.options) {
178
+ this.debugWarnLog('Missing Credentials');
179
+ } else if (this.config.credentials && !this.config.credentials.notice) {
205
180
  if (!this.config.credentials?.token) {
206
- if (this.platformLogging?.includes('debug')) {
207
- this.errorLog('Missing token');
208
- this.warnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
209
- }
181
+ this.debugErrorLog('Missing token');
182
+ this.debugWarnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
210
183
  }
211
184
  if (this.config.credentials?.token) {
212
185
  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');
186
+ this.debugErrorLog('Missing secret');
187
+ this.debugWarnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
218
188
  }
219
189
  }
220
190
  }
191
+
192
+ // Rediscover Devices
193
+ if (!this.config.options.reDiscoverDevices) {
194
+ this.config.options.reDiscoverDevices = false;
195
+ this.debugWarnLog(`Rediscover Devices disabled, discoverDevices: ${this.config.options.reDiscoverDevices}`);
196
+ }
197
+ if (!this.config.options.reDiscoverDevicesInterval && this.config.options.reDiscoverDevices) {
198
+ this.config.options.reDiscoverDevicesInterval = 1;
199
+ this.debugWarnLog(`Default Rediscover Devices Interval, discoverDevicesInterval: ${this.config.options.reDiscoverDevicesInterval} Day`);
200
+ }
221
201
  }
222
202
 
223
203
  /**
@@ -252,21 +232,27 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
252
232
  throw new Error('pluginConfig.credentials is not an object');
253
233
  }
254
234
  // 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.');
235
+ if (!this.config.credentials.secret){
236
+ // eslint-disable-next-line no-useless-escape, max-len
237
+ 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' );
238
+ // eslint-disable-next-line no-useless-escape
239
+ this.errorLog('\"secret\" config is not populated, you must populate then please restart Homebridge.');
240
+ } else {
241
+ // eslint-disable-next-line no-useless-escape, max-len
242
+ 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.');
243
+ }
257
244
 
258
245
  // set the refresh token
259
246
  pluginConfig.credentials.token = this.config.credentials?.openToken;
260
247
  pluginConfig.credentials.openToken = 'Delete';
261
248
 
262
- if (this.platformLogging?.includes('debug')) {
263
- this.warnLog(`Token: ${pluginConfig.credentials.token}`);
264
- }
249
+ this.debugWarnLog(`Token: ${pluginConfig.credentials.token}`);
250
+
265
251
  // save the config, ensuring we maintain pretty json
266
252
  writeFileSync(this.api.user.configPath(), JSON.stringify(currentConfig, null, 4));
267
253
  this.verifyConfig();
268
254
  } catch (e: any) {
269
- this.errorLog(e);
255
+ this.errorLog(`Update Token: ${e}`);
270
256
  }
271
257
  }
272
258
 
@@ -295,111 +281,136 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
295
281
  'Content-Type': 'application/json',
296
282
  },
297
283
  };
298
- const req = https.request(options, (res) => {
284
+ const req = https.request(options, async (res) => {
299
285
  this.debugLog(`statusCode: ${res.statusCode}`);
300
- this.debugLog(`headers: ${JSON.stringify(res.headers)}`);
286
+ await this.statusCode({ res });
287
+ this.debugLog(`headers: ${superStringify(res.headers)}`);
301
288
  let rawData = '';
302
289
  res.on('data', (d) => {
303
290
  rawData += d;
291
+ this.deviceStatus = d;
304
292
  this.debugLog(`d: ${d}`);
305
293
  });
306
294
  res.on('end', () => {
307
295
  try {
308
296
  const devicesAPI = JSON.parse(rawData);
309
- this.debugLog(`devicesAPI: ${JSON.stringify(devicesAPI.body)}`);
297
+ this.debugLog(`devicesAPI: ${superStringify(devicesAPI.body)}`);
310
298
 
311
299
  // SwitchBot Devices
312
- if (devicesAPI.body.deviceList.length !== 0) {
300
+ /*if (devicesAPI.body.deviceList.length !== 0) {
313
301
  this.infoLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
314
302
  } else {
315
303
  this.debugLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
316
- }
317
- const deviceLists = devicesAPI.body.deviceList;
318
- if (!this.config.options?.devices) {
319
- this.debugLog(`SwitchBot Device Config Not Set: ${JSON.stringify(this.config.options?.devices)}`);
320
- const devices = deviceLists.map((v: any) => v);
321
- for (const device of devices) {
322
- if (device.deviceType) {
323
- if (device.configDeviceName) {
324
- device.deviceName = device.configDeviceName;
304
+ }*/
305
+ const deviceBodyList = devicesAPI.body.deviceList;
306
+ let deviceLists: any[];
307
+ if (deviceBodyList) {
308
+ if (deviceBodyList.length > 1) {
309
+ deviceLists = deviceBodyList;
310
+ this.infoLog(`Total SwitchBot Devices Found: ${deviceLists.length}`);
311
+ } else {
312
+ deviceLists = [deviceBodyList];
313
+ this.infoLog(`Total SwitchBot Devices Found: ${deviceLists.length}`);
314
+ }
315
+ if (!this.config.options?.devices) {
316
+ this.debugLog(`SwitchBot Device Config Not Set: ${superStringify(this.config.options?.devices)}`);
317
+ const devices = deviceLists.map((v: any) => v);
318
+ for (const device of devices) {
319
+ if (device.deviceType) {
320
+ if (device.configDeviceName) {
321
+ device.deviceName = device.configDeviceName;
322
+ }
323
+ this.createDevice(device);
325
324
  }
326
- this.createDevice(device);
327
325
  }
328
- }
329
- } else if (this.config.credentials?.token && this.config.options.devices) {
330
- this.debugLog(`SwitchBot Device Config Set: ${JSON.stringify(this.config.options?.devices)}`);
331
- const deviceConfigs = this.config.options?.devices;
332
-
333
- const mergeBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
334
- a1.map((itm: { deviceId: string }) => ({
335
- ...a2.find(
336
- (item: { deviceId: string }) =>
337
- item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item,
338
- ),
339
- ...itm,
340
- }));
341
-
342
- const devices = mergeBydeviceId(deviceLists, deviceConfigs);
343
- this.debugLog(`SwitchBot Devices: ${JSON.stringify(devices)}`);
344
- for (const device of devices) {
345
- if (device.deviceType) {
346
- if (device.configDeviceName) {
347
- device.deviceName = device.configDeviceName;
326
+ } else if (this.config.credentials?.token && this.config.options.devices) {
327
+ this.debugLog(`SwitchBot Device Config Set: ${superStringify(this.config.options?.devices)}`);
328
+ const deviceConfigs = this.config.options?.devices;
329
+
330
+ const mergeBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
331
+ a1.map((itm: { deviceId: string }) => ({
332
+ ...a2.find(
333
+ (item: { deviceId: string }) =>
334
+ item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item,
335
+ ),
336
+ ...itm,
337
+ }));
338
+
339
+ const devices = mergeBydeviceId(deviceLists, deviceConfigs);
340
+ this.debugLog(`SwitchBot Devices: ${superStringify(devices)}`);
341
+ for (const device of devices) {
342
+ if (device.deviceType) {
343
+ if (device.configDeviceName) {
344
+ device.deviceName = device.configDeviceName;
345
+ }
346
+ this.createDevice(device);
348
347
  }
349
- this.createDevice(device);
350
348
  }
349
+ } else {
350
+ this.errorLog('SwitchBot Token Supplied, Issue with Auth.');
351
351
  }
352
352
  } else {
353
- this.errorLog('SwitchBot Token Supplied, Issue with Auth.');
353
+ this.errorLog(`SwitchBot Device Request Issue: ${this.deviceStatus}`);
354
354
  }
355
-
356
355
  // IR Devices
357
- if (devicesAPI.body.infraredRemoteList.length !== 0) {
356
+ /*if (devicesAPI.body.infraredRemoteList.length !== 0) {
358
357
  this.infoLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
359
358
  } else {
360
359
  this.debugLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
361
- }
362
- const irDeviceLists = devicesAPI.body.infraredRemoteList;
363
- if (!this.config.options?.irdevices) {
364
- this.debugLog(`IR Device Config Not Set: ${JSON.stringify(this.config.options?.irdevices)}`);
365
- const devices = irDeviceLists.map((v: any) => v);
366
- for (const device of devices) {
367
- if (device.remoteType) {
368
- this.createIRDevice(device);
369
- }
360
+ }*/
361
+ const irDeviceBodyList = devicesAPI.body.infraredRemoteList;
362
+ let irDeviceLists: any[];
363
+ if (irDeviceBodyList) {
364
+ if (irDeviceBodyList.length > 1) {
365
+ irDeviceLists = irDeviceBodyList;
366
+ this.infoLog(`Total IR Devices Found: ${irDeviceLists.length}`);
367
+ } else {
368
+ irDeviceLists = [irDeviceBodyList];
369
+ this.infoLog(`Total IR Devices Found: ${irDeviceLists.length}`);
370
370
  }
371
- } else {
372
- this.debugLog(`IR Device Config Set: ${JSON.stringify(this.config.options?.irdevices)}`);
373
- const irDeviceConfig = this.config.options?.irdevices;
374
-
375
- const mergeIRBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
376
- a1.map((itm: { deviceId: string }) => ({
377
- ...a2.find(
378
- (item: { deviceId: string }) =>
379
- item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item,
380
- ),
381
- ...itm,
382
- }));
383
-
384
- const devices = mergeIRBydeviceId(irDeviceLists, irDeviceConfig);
385
- this.debugLog(`IR Devices: ${JSON.stringify(devices)}`);
386
- for (const device of devices) {
387
- if (device.remoteType) {
388
- this.createIRDevice(device);
371
+ if (!this.config.options?.irdevices) {
372
+ this.debugLog(`IR Device Config Not Set: ${superStringify(this.config.options?.irdevices)}`);
373
+ const devices = irDeviceLists.map((v: any) => v);
374
+ for (const device of devices) {
375
+ if (device.remoteType) {
376
+ this.createIRDevice(device);
377
+ }
378
+ }
379
+ } else {
380
+ this.debugLog(`IR Device Config Set: ${superStringify(this.config.options?.irdevices)}`);
381
+ const irDeviceConfig = this.config.options?.irdevices;
382
+
383
+ const mergeIRBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
384
+ a1.map((itm: { deviceId: string }) => ({
385
+ ...a2.find(
386
+ (item: { deviceId: string }) =>
387
+ item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item,
388
+ ),
389
+ ...itm,
390
+ }));
391
+
392
+ const devices = mergeIRBydeviceId(irDeviceLists, irDeviceConfig);
393
+ this.debugLog(`IR Devices: ${superStringify(devices)}`);
394
+ for (const device of devices) {
395
+ if (device.remoteType) {
396
+ this.createIRDevice(device);
397
+ }
389
398
  }
390
399
  }
400
+ } else {
401
+ this.errorLog(`SwitchBot IR Device Request Issue: ${this.deviceStatus}`);
391
402
  }
392
403
  } catch (e:any) {
393
- this.errorLog(e.message);
404
+ this.errorLog(`API Request: ${e}`);
394
405
  }
395
406
  });
396
407
  });
397
- req.on('error', (error) => {
398
- this.errorLog(error);
408
+ req.on('error', (e: any) => {
409
+ this.errorLog(`req: ${e}`);
399
410
  });
400
411
  req.end();
401
412
  } else if (!this.config.credentials?.token && this.config.options?.devices) {
402
- this.debugLog(`SwitchBot Device Manual Config Set: ${JSON.stringify(this.config.options?.devices)}`);
413
+ this.debugLog(`SwitchBot Device Manual Config Set: ${superStringify(this.config.options?.devices)}`);
403
414
  const deviceConfigs = this.config.options?.devices;
404
415
  const devices = deviceConfigs.map((v: any) => v);
405
416
  for (const device of devices) {
@@ -413,49 +424,20 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
413
424
  this.errorLog('Neither SwitchBot Token or Device Config are not set.');
414
425
  }
415
426
  } catch (e: any) {
416
- this.deviceError(e);
417
- }
418
- }
419
-
420
- deviceError(e: any) {
421
- if (e.message.includes('400')) {
422
- this.errorLog('Failed to Discover Devices: Bad Request');
423
- this.debugLog('The client has issued an invalid request. This is commonly used to specify validation errors in a request payload.');
424
- } else if (e.message.includes('401')) {
425
- this.errorLog('Failed to Discover Devices: Unauthorized Request');
426
- this.debugLog('Authorization for the API is required, but the request has not been authenticated.');
427
- } else if (e.message.includes('403')) {
428
- this.errorLog('Failed to Discover Devices: Forbidden Request');
429
- this.debugLog('The request has been authenticated but does not have appropriate permissions, or a requested resource is not found.');
430
- } else if (e.message.includes('404')) {
431
- this.errorLog('Failed to Discover Devices: Requst Not Found');
432
- this.debugLog('Specifies the requested path does not exist.');
433
- } else if (e.message.includes('406')) {
434
- this.errorLog('Failed to Discover Devices: Request Not Acceptable');
435
- this.debugLog('The client has requested a MIME type via the Accept header for a value not supported by the server.');
436
- } else if (e.message.includes('415')) {
437
- this.errorLog('Failed to Discover Devices: Unsupported Requst Header');
438
- this.debugLog('The client has defined a contentType header that is not supported by the server.');
439
- } else if (e.message.includes('422')) {
440
- this.errorLog('Failed to Discover Devices: Unprocessable Entity');
441
- this.debugLog(
442
- 'The client has made a valid request, but the server cannot process it.' +
443
- ' This is often used for APIs for which certain limits have been exceeded.',
444
- );
445
- } else if (e.message.includes('429')) {
446
- this.errorLog('Failed to Discover Devices: Too Many Requests');
447
- this.debugLog('The client has exceeded the number of requests allowed for a given time window.');
448
- } else if (e.message.includes('500')) {
449
- this.errorLog('Failed to Discover Devices: Internal Server Error');
450
- this.debugLog('An unexpected error on the SmartThings servers has occurred. These errors should be rare.');
451
- } else {
452
- this.errorLog('Failed to Discover Devices');
453
- }
454
- if (this.platformLogging === 'debug') {
455
- this.errorLog(`Failed to Discover Devices, Error Message: ${JSON.stringify(e.message)}`);
427
+ this.debugErrorLog(`Failed to Discover Devices, Error Message: ${superStringify(e.message)}`);
428
+ this.debugErrorLog(`Failed to Discover Devices, Error: ${e}`);
456
429
  }
457
- if (this.platformLogging === 'debugMode') {
458
- this.errorLog(`Failed to Discover Devices, Error: ${JSON.stringify(e)}`);
430
+ if (this.config.options?.reDiscoverDevices) {
431
+ this.debugWarnLog(`Rediscover Devices Interval, discoverDevicesInterval: ${this.config.options.reDiscoverDevicesInterval} Day`);
432
+ if (this.config.options.reDiscoverDevicesInterval > 1) {
433
+ interval(this.config.options.reDiscoverDevicesInterval * 86400000)
434
+ .pipe(take(1))
435
+ .subscribe(async () => {
436
+ await this.discoverDevices();
437
+ });
438
+ } else {
439
+ this.debugErrorLog(`discoverDevicesInterval must be > 1, discoverDevicesInterval: ${this.config.options.reDiscoverDevicesInterval} Day`);
440
+ }
459
441
  }
460
442
  }
461
443
 
@@ -492,7 +474,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
492
474
  this.createContact(device);
493
475
  break;
494
476
  case 'Curtain':
495
- this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
477
+ this.debugLog(`Discovered ${device.deviceType} ${device.deviceName}: ${device.deviceId}`);
496
478
  this.createCurtain(device);
497
479
  break;
498
480
  case 'Plug':
@@ -514,7 +496,6 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
514
496
  this.createStripLight(device);
515
497
  break;
516
498
  case 'Indoor Cam':
517
- this.deviceInfo(device);
518
499
  this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
519
500
  this.warnLog(`Device: ${device.deviceName} with Device Type: ${device.deviceType}, is currently not supported.`);
520
501
  break;
@@ -522,7 +503,6 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
522
503
  this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId} is Not Supported.`);
523
504
  break;
524
505
  default:
525
- this.deviceInfo(device);
526
506
  this.warnLog(`Device: ${device.deviceName} with Device Type: ${device.deviceType}, is currently not supported.`);
527
507
  this.warnLog('Submit Feature Requests Here: https://git.io/JL14Z');
528
508
  }
@@ -585,29 +565,12 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
585
565
  this.createOthers(device);
586
566
  break;
587
567
  default:
588
- this.deviceInfo(device);
568
+ this.debugLog(`Unsupported Device: ${superStringify(device)}`);
589
569
  this.warnLog(`Device: ${device.deviceName} with Device Type: ${device.remoteType}, is currently not supported.`);
590
570
  this.warnLog('Submit Feature Requests Here: https://git.io/JL14Z');
591
571
  }
592
572
  }
593
573
 
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
574
  private async createHumidifier(device: device & devicesConfig) {
612
575
  const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
613
576
 
@@ -617,17 +580,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
617
580
 
618
581
  if (existingAccessory) {
619
582
  // the accessory already exists
620
- if (this.registerDevice(device)) {
621
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
622
-
583
+ if (await this.registerDevice(device)) {
623
584
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
624
- //existingAccessory.context.firmwareRevision = firmware;
625
585
  existingAccessory.context.model = device.deviceType;
626
586
  existingAccessory.context.deviceID = device.deviceId;
627
- existingAccessory.displayName = device.deviceName;
587
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
628
588
  existingAccessory.context.firmwareRevision = device.firmware;
629
589
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
630
- await this.connectionTypeExistingAccessory(device, existingAccessory);
590
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
591
+ existingAccessory.context.connectionType = await this.connectionType(device);
631
592
  this.api.updatePlatformAccessories([existingAccessory]);
632
593
  // create the accessory handler for the restored accessory
633
594
  // this is imported from `platformAccessory.ts`
@@ -636,7 +597,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
636
597
  } else {
637
598
  this.unregisterPlatformAccessories(existingAccessory);
638
599
  }
639
- } else if (this.registerDevice(device)) {
600
+ } else if (await this.registerDevice(device)) {
640
601
  // the accessory does not yet exist, so we need to create it
641
602
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
642
603
 
@@ -650,17 +611,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
650
611
  accessory.context.deviceID = device.deviceId;
651
612
  accessory.context.firmwareRevision = device.firmware;
652
613
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
653
- await this.connectionTypeNewAccessory(device, accessory);
614
+ accessory.context.connectionType = await this.connectionType(device);
654
615
  // create the accessory handler for the newly create accessory
655
616
  // this is imported from `platformAccessory.ts`
656
617
  new Humidifier(this, accessory, device);
657
618
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
658
619
 
659
- // link the accessory to your platform
660
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
620
+ // publish device externally or link the accessory to your platform
621
+ this.externalOrPlatform(device, accessory);
661
622
  this.accessories.push(accessory);
662
623
  } else {
663
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
624
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
664
625
  }
665
626
  }
666
627
 
@@ -673,18 +634,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
673
634
 
674
635
  if (existingAccessory) {
675
636
  // the accessory already exists
676
- 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));
637
+ if (await this.registerDevice(device)) {
680
638
 
639
+ this.debugLog(superStringify(device.bot?.mode));
681
640
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
682
641
  existingAccessory.context.model = device.deviceType;
683
642
  existingAccessory.context.deviceID = device.deviceId;
684
- existingAccessory.displayName = device.deviceName;
643
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
685
644
  existingAccessory.context.firmwareRevision = device.firmware;
686
645
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
687
- await this.connectionTypeExistingAccessory(device, existingAccessory);
646
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
647
+ existingAccessory.context.connectionType = await this.connectionType(device);
688
648
  this.api.updatePlatformAccessories([existingAccessory]);
689
649
  // create the accessory handler for the restored accessory
690
650
  // this is imported from `platformAccessory.ts`
@@ -693,11 +653,11 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
693
653
  } else {
694
654
  this.unregisterPlatformAccessories(existingAccessory);
695
655
  }
696
- } else if (this.registerDevice(device)) {
656
+ } else if (await this.registerDevice(device)) {
697
657
  // the accessory does not yet exist, so we need to create it
698
658
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
699
659
 
700
- this.debugLog(JSON.stringify(device.bot?.mode));
660
+ this.debugLog(superStringify(device.bot?.mode));
701
661
 
702
662
  // create a new accessory
703
663
  const accessory = new this.api.platformAccessory(device.deviceName, uuid);
@@ -709,18 +669,18 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
709
669
  accessory.context.deviceID = device.deviceId;
710
670
  accessory.context.firmwareRevision = device.firmware;
711
671
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
712
- await this.connectionTypeNewAccessory(device, accessory);
672
+ accessory.context.connectionType = await this.connectionType(device);
713
673
  // accessory.context.firmwareRevision = findaccessories.accessoryAttribute.softwareRevision;
714
674
  // create the accessory handler for the newly create accessory
715
675
  // this is imported from `platformAccessory.ts`
716
676
  new Bot(this, accessory, device);
717
677
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
718
678
 
719
- // link the accessory to your platform
720
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
679
+ // publish device externally or link the accessory to your platform
680
+ this.externalOrPlatform(device, accessory);
721
681
  this.accessories.push(accessory);
722
682
  } else {
723
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
683
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
724
684
  }
725
685
  }
726
686
 
@@ -733,16 +693,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
733
693
 
734
694
  if (existingAccessory) {
735
695
  // the accessory already exists
736
- if (this.registerDevice(device)) {
737
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
738
-
696
+ if (await this.registerDevice(device)) {
739
697
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
740
698
  existingAccessory.context.model = device.deviceType;
741
699
  existingAccessory.context.deviceID = device.deviceId;
742
- existingAccessory.displayName = device.deviceName;
700
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
743
701
  existingAccessory.context.firmwareRevision = device.firmware;
744
702
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
745
- await this.connectionTypeExistingAccessory(device, existingAccessory);
703
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
704
+ existingAccessory.context.connectionType = await this.connectionType(device);
746
705
  this.api.updatePlatformAccessories([existingAccessory]);
747
706
  // create the accessory handler for the restored accessory
748
707
  // this is imported from `platformAccessory.ts`
@@ -751,7 +710,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
751
710
  } else {
752
711
  this.unregisterPlatformAccessories(existingAccessory);
753
712
  }
754
- } else if (this.registerDevice(device)) {
713
+ } else if (await this.registerDevice(device)) {
755
714
  // the accessory does not yet exist, so we need to create it
756
715
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
757
716
 
@@ -765,17 +724,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
765
724
  accessory.context.deviceID = device.deviceId;
766
725
  accessory.context.firmwareRevision = device.firmware;
767
726
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
768
- await this.connectionTypeNewAccessory(device, accessory);
727
+ accessory.context.connectionType = await this.connectionType(device);
769
728
  // create the accessory handler for the newly create accessory
770
729
  // this is imported from `platformAccessory.ts`
771
730
  new Meter(this, accessory, device);
772
731
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
773
732
 
774
- // link the accessory to your platform
775
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
733
+ // publish device externally or link the accessory to your platform
734
+ this.externalOrPlatform(device, accessory);
776
735
  this.accessories.push(accessory);
777
736
  } else {
778
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
737
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
779
738
  }
780
739
  }
781
740
 
@@ -788,16 +747,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
788
747
 
789
748
  if (existingAccessory) {
790
749
  // the accessory already exists
791
- if (this.registerDevice(device)) {
792
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
793
-
750
+ if (await this.registerDevice(device)) {
794
751
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
795
752
  existingAccessory.context.model = device.deviceType;
796
753
  existingAccessory.context.deviceID = device.deviceId;
797
- existingAccessory.displayName = device.deviceName;
754
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
798
755
  existingAccessory.context.firmwareRevision = device.firmware;
799
756
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
800
- await this.connectionTypeExistingAccessory(device, existingAccessory);
757
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
758
+ existingAccessory.context.connectionType = await this.connectionType(device);
801
759
  this.api.updatePlatformAccessories([existingAccessory]);
802
760
  // create the accessory handler for the restored accessory
803
761
  // this is imported from `platformAccessory.ts`
@@ -806,7 +764,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
806
764
  } else {
807
765
  this.unregisterPlatformAccessories(existingAccessory);
808
766
  }
809
- } else if (this.registerDevice(device)) {
767
+ } else if (await this.registerDevice(device)) {
810
768
  // the accessory does not yet exist, so we need to create it
811
769
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
812
770
 
@@ -820,17 +778,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
820
778
  accessory.context.deviceID = device.deviceId;
821
779
  accessory.context.firmwareRevision = device.firmware;
822
780
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
823
- await this.connectionTypeNewAccessory(device, accessory);
781
+ accessory.context.connectionType = await this.connectionType(device);
824
782
  // create the accessory handler for the newly create accessory
825
783
  // this is imported from `platformAccessory.ts`
826
784
  new MeterPlus(this, accessory, device);
827
785
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
828
786
 
829
- // link the accessory to your platform
830
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
787
+ // publish device externally or link the accessory to your platform
788
+ this.externalOrPlatform(device, accessory);
831
789
  this.accessories.push(accessory);
832
790
  } else {
833
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
791
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
834
792
  }
835
793
  }
836
794
 
@@ -843,16 +801,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
843
801
 
844
802
  if (existingAccessory) {
845
803
  // the accessory already exists
846
- if (this.registerDevice(device)) {
847
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
848
-
804
+ if (await this.registerDevice(device)) {
849
805
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
850
806
  existingAccessory.context.model = device.deviceType;
851
807
  existingAccessory.context.deviceID = device.deviceId;
852
- existingAccessory.displayName = device.deviceName;
808
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
853
809
  existingAccessory.context.firmwareRevision = device.firmware;
854
810
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
855
- await this.connectionTypeExistingAccessory(device, existingAccessory);
811
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
812
+ existingAccessory.context.connectionType = await this.connectionType(device);
856
813
  this.api.updatePlatformAccessories([existingAccessory]);
857
814
  // create the accessory handler for the restored accessory
858
815
  // this is imported from `platformAccessory.ts`
@@ -861,7 +818,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
861
818
  } else {
862
819
  this.unregisterPlatformAccessories(existingAccessory);
863
820
  }
864
- } else if (this.registerDevice(device)) {
821
+ } else if (await this.registerDevice(device)) {
865
822
  // the accessory does not yet exist, so we need to create it
866
823
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
867
824
 
@@ -875,17 +832,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
875
832
  accessory.context.deviceID = device.deviceId;
876
833
  accessory.context.firmwareRevision = device.firmware;
877
834
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
878
- await this.connectionTypeNewAccessory(device, accessory);
835
+ accessory.context.connectionType = await this.connectionType(device);
879
836
  // create the accessory handler for the newly create accessory
880
837
  // this is imported from `platformAccessory.ts`
881
838
  new Motion(this, accessory, device);
882
839
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
883
840
 
884
- // link the accessory to your platform
885
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
841
+ // publish device externally or link the accessory to your platform
842
+ this.externalOrPlatform(device, accessory);
886
843
  this.accessories.push(accessory);
887
844
  } else {
888
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
845
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
889
846
  }
890
847
  }
891
848
 
@@ -898,16 +855,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
898
855
 
899
856
  if (existingAccessory) {
900
857
  // the accessory already exists
901
- if (this.registerDevice(device)) {
902
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
903
-
858
+ if (await this.registerDevice(device)) {
904
859
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
905
860
  existingAccessory.context.model = device.deviceType;
906
861
  existingAccessory.context.deviceID = device.deviceId;
907
- existingAccessory.displayName = device.deviceName;
862
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
908
863
  existingAccessory.context.firmwareRevision = device.firmware;
909
864
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
910
- await this.connectionTypeExistingAccessory(device, existingAccessory);
865
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
866
+ existingAccessory.context.connectionType = await this.connectionType(device);
911
867
  this.api.updatePlatformAccessories([existingAccessory]);
912
868
  // create the accessory handler for the restored accessory
913
869
  // this is imported from `platformAccessory.ts`
@@ -916,7 +872,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
916
872
  } else {
917
873
  this.unregisterPlatformAccessories(existingAccessory);
918
874
  }
919
- } else if (this.registerDevice(device)) {
875
+ } else if (await this.registerDevice(device)) {
920
876
  // the accessory does not yet exist, so we need to create it
921
877
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
922
878
 
@@ -930,17 +886,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
930
886
  accessory.context.deviceID = device.deviceId;
931
887
  accessory.context.firmwareRevision = device.firmware;
932
888
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
933
- await this.connectionTypeNewAccessory(device, accessory);
889
+ accessory.context.connectionType = await this.connectionType(device);
934
890
  // create the accessory handler for the newly create accessory
935
891
  // this is imported from `platformAccessory.ts`
936
892
  new Contact(this, accessory, device);
937
893
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
938
894
 
939
- // link the accessory to your platform
940
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
895
+ // publish device externally or link the accessory to your platform
896
+ this.externalOrPlatform(device, accessory);
941
897
  this.accessories.push(accessory);
942
898
  } else {
943
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
899
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
944
900
  }
945
901
  }
946
902
 
@@ -953,16 +909,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
953
909
 
954
910
  if (existingAccessory) {
955
911
  // the accessory already exists
956
- if (this.isCurtainGrouped(device)) {
957
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
958
-
912
+ if (await this.registerDevice(device)) {
959
913
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
960
914
  existingAccessory.context.model = device.deviceType;
961
915
  existingAccessory.context.deviceID = device.deviceId;
962
- existingAccessory.displayName = device.deviceName;
916
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
963
917
  existingAccessory.context.firmwareRevision = device.firmware;
964
918
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
965
- await this.connectionTypeExistingAccessory(device, existingAccessory);
919
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
920
+ existingAccessory.context.connectionType = await this.connectionType(device);
966
921
  this.api.updatePlatformAccessories([existingAccessory]);
967
922
  // create the accessory handler for the restored accessory
968
923
  // this is imported from `platformAccessory.ts`
@@ -971,7 +926,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
971
926
  } else {
972
927
  this.unregisterPlatformAccessories(existingAccessory);
973
928
  }
974
- } else if (this.isCurtainGrouped(device)) {
929
+ } else if (await this.registerDevice(device)) {
975
930
  // the accessory does not yet exist, so we need to create it
976
931
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
977
932
 
@@ -980,9 +935,9 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
980
935
  , Secondary curtain automatically hidden. Main Curtain: ${device.deviceName}, DeviceID: ${device.deviceId}`);
981
936
  } else {
982
937
  if (device.master) {
983
- this.debugLog(`Main Curtain: ${device.deviceName}, DeviceID: ${device.deviceId}`);
938
+ this.warnLog(`Main Curtain: ${device.deviceName}, DeviceID: ${device.deviceId}`);
984
939
  } else {
985
- this.debugLog(`Secondary Curtain: ${device.deviceName}, DeviceID: ${device.deviceId}`);
940
+ this.errorLog(`Secondary Curtain: ${device.deviceName}, DeviceID: ${device.deviceId}`);
986
941
  }
987
942
  }
988
943
 
@@ -996,32 +951,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
996
951
  accessory.context.deviceID = device.deviceId;
997
952
  accessory.context.firmwareRevision = device.firmware;
998
953
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
999
- await this.connectionTypeNewAccessory(device, accessory);
954
+ accessory.context.connectionType = await this.connectionType(device);
1000
955
  // create the accessory handler for the newly create accessory
1001
956
  // this is imported from `platformAccessory.ts`
1002
957
  new Curtain(this, accessory, device);
1003
958
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1004
959
 
1005
- // link the accessory to your platform
1006
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
960
+ // publish device externally or link the accessory to your platform
961
+ this.externalOrPlatform(device, accessory);
1007
962
  this.accessories.push(accessory);
1008
963
  } else {
1009
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
1010
- }
1011
- }
1012
-
1013
- private isCurtainGrouped(device: device & devicesConfig) {
1014
- this.debugLog(
1015
- `deviceId: ${device.deviceId}, curtainDevicesIds: ${device.curtainDevicesIds},` +
1016
- ` master: ${device.master}, group: ${device.group}, disable_group: ${device.curtain?.disable_group}`,
1017
- );
1018
-
1019
- if (device.group && !device.curtain?.disable_group) {
1020
- this.debugLog(`[Curtain Config] disable_group: ${device.curtain?.disable_group}`);
1021
- return device.master && this.registerDevice(device);
1022
- } else {
1023
- this.debugLog(`[Curtain Config] disable_group: ${device.curtain?.disable_group}, UnGrouping ${device.master}`);
1024
- return this.registerDevice(device);
964
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1025
965
  }
1026
966
  }
1027
967
 
@@ -1034,16 +974,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1034
974
 
1035
975
  if (existingAccessory) {
1036
976
  // the accessory already exists
1037
- if (this.registerDevice(device)) {
1038
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1039
-
977
+ if (await this.registerDevice(device)) {
1040
978
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1041
979
  existingAccessory.context.model = device.deviceType;
1042
980
  existingAccessory.context.deviceID = device.deviceId;
1043
- existingAccessory.displayName = device.deviceName;
981
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1044
982
  existingAccessory.context.firmwareRevision = device.firmware;
1045
983
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1046
- await this.connectionTypeExistingAccessory(device, existingAccessory);
984
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
985
+ existingAccessory.context.connectionType = await this.connectionType(device);
1047
986
  this.api.updatePlatformAccessories([existingAccessory]);
1048
987
  // create the accessory handler for the restored accessory
1049
988
  // this is imported from `platformAccessory.ts`
@@ -1052,7 +991,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1052
991
  } else {
1053
992
  this.unregisterPlatformAccessories(existingAccessory);
1054
993
  }
1055
- } else if (this.registerDevice(device)) {
994
+ } else if (await this.registerDevice(device)) {
1056
995
  // the accessory does not yet exist, so we need to create it
1057
996
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1058
997
 
@@ -1066,17 +1005,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1066
1005
  accessory.context.deviceID = device.deviceId;
1067
1006
  accessory.context.firmwareRevision = device.firmware;
1068
1007
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1069
- await this.connectionTypeNewAccessory(device, accessory);
1008
+ accessory.context.connectionType = await this.connectionType(device);
1070
1009
  // create the accessory handler for the newly create accessory
1071
1010
  // this is imported from `platformAccessory.ts`
1072
1011
  new Plug(this, accessory, device);
1073
1012
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1074
1013
 
1075
- // link the accessory to your platform
1076
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1014
+ // publish device externally or link the accessory to your platform
1015
+ this.externalOrPlatform(device, accessory);
1077
1016
  this.accessories.push(accessory);
1078
1017
  } else {
1079
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
1018
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1080
1019
  }
1081
1020
  }
1082
1021
 
@@ -1089,16 +1028,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1089
1028
 
1090
1029
  if (existingAccessory) {
1091
1030
  // the accessory already exists
1092
- if (this.registerDevice(device)) {
1093
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1094
-
1031
+ if (await this.registerDevice(device)) {
1095
1032
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1096
1033
  existingAccessory.context.model = device.deviceType;
1097
1034
  existingAccessory.context.deviceID = device.deviceId;
1098
- existingAccessory.displayName = device.deviceName;
1035
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1099
1036
  existingAccessory.context.firmwareRevision = device.firmware;
1100
1037
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1101
- await this.connectionTypeExistingAccessory(device, existingAccessory);
1038
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1039
+ existingAccessory.context.connectionType = await this.connectionType(device);
1102
1040
  this.api.updatePlatformAccessories([existingAccessory]);
1103
1041
  // create the accessory handler for the restored accessory
1104
1042
  // this is imported from `platformAccessory.ts`
@@ -1107,7 +1045,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1107
1045
  } else {
1108
1046
  this.unregisterPlatformAccessories(existingAccessory);
1109
1047
  }
1110
- } else if (this.registerDevice(device)) {
1048
+ } else if (await this.registerDevice(device)) {
1111
1049
  // the accessory does not yet exist, so we need to create it
1112
1050
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1113
1051
 
@@ -1121,17 +1059,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1121
1059
  accessory.context.deviceID = device.deviceId;
1122
1060
  accessory.context.firmwareRevision = device.firmware;
1123
1061
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1124
- await this.connectionTypeNewAccessory(device, accessory);
1062
+ accessory.context.connectionType = await this.connectionType(device);
1125
1063
  // create the accessory handler for the newly create accessory
1126
1064
  // this is imported from `platformAccessory.ts`
1127
1065
  new Lock(this, accessory, device);
1128
1066
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1129
1067
 
1130
- // link the accessory to your platform
1131
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1068
+ // publish device externally or link the accessory to your platform
1069
+ this.externalOrPlatform(device, accessory);
1132
1070
  this.accessories.push(accessory);
1133
1071
  } else {
1134
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
1072
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1135
1073
  }
1136
1074
  }
1137
1075
 
@@ -1144,16 +1082,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1144
1082
 
1145
1083
  if (existingAccessory) {
1146
1084
  // the accessory already exists
1147
- if (this.registerDevice(device)) {
1148
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1149
-
1085
+ if (await this.registerDevice(device)) {
1150
1086
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1151
1087
  existingAccessory.context.model = device.deviceType;
1152
1088
  existingAccessory.context.deviceID = device.deviceId;
1153
- existingAccessory.displayName = device.deviceName;
1089
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1154
1090
  existingAccessory.context.firmwareRevision = device.firmware;
1155
1091
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1156
- await this.connectionTypeExistingAccessory(device, existingAccessory);
1092
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1093
+ existingAccessory.context.connectionType = await this.connectionType(device);
1157
1094
  this.api.updatePlatformAccessories([existingAccessory]);
1158
1095
  // create the accessory handler for the restored accessory
1159
1096
  // this is imported from `platformAccessory.ts`
@@ -1162,7 +1099,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1162
1099
  } else {
1163
1100
  this.unregisterPlatformAccessories(existingAccessory);
1164
1101
  }
1165
- } else if (this.registerDevice(device)) {
1102
+ } else if (await this.registerDevice(device)) {
1166
1103
  // the accessory does not yet exist, so we need to create it
1167
1104
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1168
1105
 
@@ -1176,17 +1113,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1176
1113
  accessory.context.deviceID = device.deviceId;
1177
1114
  accessory.context.firmwareRevision = device.firmware;
1178
1115
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1179
- await this.connectionTypeNewAccessory(device, accessory);
1116
+ accessory.context.connectionType = await this.connectionType(device);
1180
1117
  // create the accessory handler for the newly create accessory
1181
1118
  // this is imported from `platformAccessory.ts`
1182
1119
  new ColorBulb(this, accessory, device);
1183
1120
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1184
1121
 
1185
- // link the accessory to your platform
1186
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1122
+ // publish device externally or link the accessory to your platform
1123
+ this.externalOrPlatform(device, accessory);
1187
1124
  this.accessories.push(accessory);
1188
1125
  } else {
1189
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
1126
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1190
1127
  }
1191
1128
  }
1192
1129
 
@@ -1199,16 +1136,15 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1199
1136
 
1200
1137
  if (existingAccessory) {
1201
1138
  // the accessory already exists
1202
- if (this.registerDevice(device)) {
1203
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1204
-
1139
+ if (await this.registerDevice(device)) {
1205
1140
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1206
1141
  existingAccessory.context.model = device.deviceType;
1207
1142
  existingAccessory.context.deviceID = device.deviceId;
1208
- existingAccessory.displayName = device.deviceName;
1143
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1209
1144
  existingAccessory.context.firmwareRevision = device.firmware;
1210
1145
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1211
- await this.connectionTypeExistingAccessory(device, existingAccessory);
1146
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1147
+ existingAccessory.context.connectionType = await this.connectionType(device);
1212
1148
  this.api.updatePlatformAccessories([existingAccessory]);
1213
1149
  // create the accessory handler for the restored accessory
1214
1150
  // this is imported from `platformAccessory.ts`
@@ -1217,7 +1153,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1217
1153
  } else {
1218
1154
  this.unregisterPlatformAccessories(existingAccessory);
1219
1155
  }
1220
- } else if (this.registerDevice(device)) {
1156
+ } else if (await this.registerDevice(device)) {
1221
1157
  // the accessory does not yet exist, so we need to create it
1222
1158
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1223
1159
 
@@ -1231,17 +1167,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1231
1167
  accessory.context.deviceID = device.deviceId;
1232
1168
  accessory.context.firmwareRevision = device.firmware;
1233
1169
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1234
- await this.connectionTypeNewAccessory(device, accessory);
1170
+ accessory.context.connectionType = await this.connectionType(device);
1235
1171
  // create the accessory handler for the newly create accessory
1236
1172
  // this is imported from `platformAccessory.ts`
1237
1173
  new StripLight(this, accessory, device);
1238
1174
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1239
1175
 
1240
- // link the accessory to your platform
1241
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1176
+ // publish device externally or link the accessory to your platform
1177
+ this.externalOrPlatform(device, accessory);
1242
1178
  this.accessories.push(accessory);
1243
1179
  } else {
1244
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
1180
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1245
1181
  }
1246
1182
  }
1247
1183
 
@@ -1253,15 +1189,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1253
1189
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1254
1190
 
1255
1191
  if (!device.hide_device && existingAccessory) {
1256
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1257
-
1258
1192
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1259
1193
  existingAccessory.context.model = device.remoteType;
1260
1194
  existingAccessory.context.deviceID = device.deviceId;
1261
- existingAccessory.displayName = device.deviceName;
1195
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1262
1196
  existingAccessory.context.firmwareRevision = device.firmware;
1263
1197
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1264
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1198
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1199
+ existingAccessory.context.connectionType = await this.connectionType(device);
1265
1200
  this.api.updatePlatformAccessories([existingAccessory]);
1266
1201
  // create the accessory handler for the restored accessory
1267
1202
  // this is imported from `platformAccessory.ts`
@@ -1281,7 +1216,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1281
1216
  accessory.context.deviceID = device.deviceId;
1282
1217
  accessory.context.firmwareRevision = device.firmware;
1283
1218
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1284
- await this.connectionTypeNewIRAccessory(accessory);
1219
+ accessory.context.connectionType = await this.connectionType(device);
1285
1220
  // create the accessory handler for the newly create accessory
1286
1221
  // this is imported from `platformAccessory.ts`
1287
1222
  new TV(this, accessory, device);
@@ -1292,10 +1227,10 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1292
1227
  * Only one TV can exist per bridge, to bypass this limitation, you should
1293
1228
  * publish your TV as an external accessory.
1294
1229
  */
1295
- this.api.publishExternalAccessories(PLUGIN_NAME, [accessory]);
1230
+ this.externalAccessory(accessory);
1296
1231
  this.accessories.push(accessory);
1297
1232
  } else {
1298
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1233
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1299
1234
  }
1300
1235
  }
1301
1236
 
@@ -1309,16 +1244,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1309
1244
  if (existingAccessory) {
1310
1245
  // the accessory already exists
1311
1246
  if (!device.hide_device && device.hubDeviceId) {
1312
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1313
-
1314
1247
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1315
-
1316
1248
  existingAccessory.context.model = device.remoteType;
1317
1249
  existingAccessory.context.deviceID = device.deviceId;
1318
- existingAccessory.displayName = device.deviceName;
1250
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1319
1251
  existingAccessory.context.firmwareRevision = device.firmware;
1320
1252
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1321
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1253
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1254
+ existingAccessory.context.connectionType = await this.connectionType(device);
1322
1255
  this.api.updatePlatformAccessories([existingAccessory]);
1323
1256
  // create the accessory handler for the restored accessory
1324
1257
  // this is imported from `platformAccessory.ts`
@@ -1341,17 +1274,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1341
1274
  accessory.context.deviceID = device.deviceId;
1342
1275
  accessory.context.firmwareRevision = device.firmware;
1343
1276
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1344
- await this.connectionTypeNewIRAccessory(accessory);
1277
+ accessory.context.connectionType = await this.connectionType(device);
1345
1278
  // create the accessory handler for the newly create accessory
1346
1279
  // this is imported from `platformAccessory.ts`
1347
1280
  new Fan(this, accessory, device);
1348
1281
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1349
1282
 
1350
- // link the accessory to your platform
1351
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1283
+ // publish device externally or link the accessory to your platform
1284
+ this.externalOrPlatform(device, accessory);
1352
1285
  this.accessories.push(accessory);
1353
1286
  } else {
1354
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1287
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1355
1288
  }
1356
1289
  }
1357
1290
 
@@ -1365,15 +1298,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1365
1298
  if (existingAccessory) {
1366
1299
  // the accessory already exists
1367
1300
  if (!device.hide_device && device.hubDeviceId) {
1368
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1369
-
1370
1301
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1371
1302
  existingAccessory.context.model = device.remoteType;
1372
1303
  existingAccessory.context.deviceID = device.deviceId;
1373
- existingAccessory.displayName = device.deviceName;
1304
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1374
1305
  existingAccessory.context.firmwareRevision = device.firmware;
1375
1306
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1376
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1307
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1308
+ existingAccessory.context.connectionType = await this.connectionType(device);
1377
1309
  this.api.updatePlatformAccessories([existingAccessory]);
1378
1310
  // create the accessory handler for the restored accessory
1379
1311
  // this is imported from `platformAccessory.ts`
@@ -1396,17 +1328,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1396
1328
  accessory.context.deviceID = device.deviceId;
1397
1329
  accessory.context.firmwareRevision = device.firmware;
1398
1330
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1399
- await this.connectionTypeNewIRAccessory(accessory);
1331
+ accessory.context.connectionType = await this.connectionType(device);
1400
1332
  // create the accessory handler for the newly create accessory
1401
1333
  // this is imported from `platformAccessory.ts`
1402
1334
  new Light(this, accessory, device);
1403
1335
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1404
1336
 
1405
- // link the accessory to your platform
1406
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1337
+ // publish device externally or link the accessory to your platform
1338
+ this.externalOrPlatform(device, accessory);
1407
1339
  this.accessories.push(accessory);
1408
1340
  } else {
1409
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1341
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1410
1342
  }
1411
1343
  }
1412
1344
 
@@ -1420,15 +1352,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1420
1352
  if (existingAccessory) {
1421
1353
  // the accessory already exists
1422
1354
  if (!device.hide_device && device.hubDeviceId) {
1423
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1424
-
1425
1355
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1426
1356
  existingAccessory.context.model = device.remoteType;
1427
1357
  existingAccessory.context.deviceID = device.deviceId;
1428
- existingAccessory.displayName = device.deviceName;
1358
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1429
1359
  existingAccessory.context.firmwareRevision = device.firmware;
1430
1360
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1431
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1361
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1362
+ existingAccessory.context.connectionType = await this.connectionType(device);
1432
1363
  this.api.updatePlatformAccessories([existingAccessory]);
1433
1364
  // create the accessory handler for the restored accessory
1434
1365
  // this is imported from `platformAccessory.ts`
@@ -1451,17 +1382,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1451
1382
  accessory.context.deviceID = device.deviceId;
1452
1383
  accessory.context.firmwareRevision = device.firmware;
1453
1384
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1454
- await this.connectionTypeNewIRAccessory(accessory);
1385
+ accessory.context.connectionType = await this.connectionType(device);
1455
1386
  // create the accessory handler for the newly create accessory
1456
1387
  // this is imported from `platformAccessory.ts`
1457
1388
  new AirConditioner(this, accessory, device);
1458
1389
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1459
1390
 
1460
- // link the accessory to your platform
1461
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1391
+ // publish device externally or link the accessory to your platform
1392
+ this.externalOrPlatform(device, accessory);
1462
1393
  this.accessories.push(accessory);
1463
1394
  } else {
1464
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1395
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1465
1396
  }
1466
1397
  }
1467
1398
 
@@ -1475,15 +1406,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1475
1406
  if (existingAccessory) {
1476
1407
  // the accessory already exists
1477
1408
  if (!device.hide_device && device.hubDeviceId) {
1478
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1479
-
1480
1409
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1481
1410
  existingAccessory.context.model = device.remoteType;
1482
1411
  existingAccessory.context.deviceID = device.deviceId;
1483
- existingAccessory.displayName = device.deviceName;
1412
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1484
1413
  existingAccessory.context.firmwareRevision = device.firmware;
1485
1414
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1486
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1415
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1416
+ existingAccessory.context.connectionType = await this.connectionType(device);
1487
1417
  this.api.updatePlatformAccessories([existingAccessory]);
1488
1418
  // create the accessory handler for the restored accessory
1489
1419
  // this is imported from `platformAccessory.ts`
@@ -1506,17 +1436,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1506
1436
  accessory.context.deviceID = device.deviceId;
1507
1437
  accessory.context.firmwareRevision = device.firmware;
1508
1438
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1509
- await this.connectionTypeNewIRAccessory(accessory);
1439
+ accessory.context.connectionType = await this.connectionType(device);
1510
1440
  // create the accessory handler for the newly create accessory
1511
1441
  // this is imported from `platformAccessory.ts`
1512
1442
  new AirPurifier(this, accessory, device);
1513
1443
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1514
1444
 
1515
- // link the accessory to your platform
1516
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1445
+ // publish device externally or link the accessory to your platform
1446
+ this.externalOrPlatform(device, accessory);
1517
1447
  this.accessories.push(accessory);
1518
1448
  } else {
1519
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1449
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1520
1450
  }
1521
1451
  }
1522
1452
 
@@ -1530,15 +1460,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1530
1460
  if (existingAccessory) {
1531
1461
  // the accessory already exists
1532
1462
  if (!device.hide_device && device.hubDeviceId) {
1533
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1534
-
1535
1463
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1536
1464
  existingAccessory.context.model = device.remoteType;
1537
1465
  existingAccessory.context.deviceID = device.deviceId;
1538
- existingAccessory.displayName = device.deviceName;
1466
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1539
1467
  existingAccessory.context.firmwareRevision = device.firmware;
1540
1468
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1541
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1469
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1470
+ existingAccessory.context.connectionType = await this.connectionType(device);
1542
1471
  this.api.updatePlatformAccessories([existingAccessory]);
1543
1472
  // create the accessory handler for the restored accessory
1544
1473
  // this is imported from `platformAccessory.ts`
@@ -1561,17 +1490,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1561
1490
  accessory.context.deviceID = device.deviceId;
1562
1491
  accessory.context.firmwareRevision = device.firmware;
1563
1492
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1564
- await this.connectionTypeNewIRAccessory(accessory);
1493
+ accessory.context.connectionType = await this.connectionType(device);
1565
1494
  // create the accessory handler for the newly create accessory
1566
1495
  // this is imported from `platformAccessory.ts`
1567
1496
  new WaterHeater(this, accessory, device);
1568
1497
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1569
1498
 
1570
- // link the accessory to your platform
1571
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1499
+ // publish device externally or link the accessory to your platform
1500
+ this.externalOrPlatform(device, accessory);
1572
1501
  this.accessories.push(accessory);
1573
1502
  } else {
1574
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1503
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1575
1504
  }
1576
1505
  }
1577
1506
 
@@ -1585,15 +1514,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1585
1514
  if (existingAccessory) {
1586
1515
  // the accessory already exists
1587
1516
  if (!device.hide_device && device.hubDeviceId) {
1588
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1589
-
1590
1517
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1591
1518
  existingAccessory.context.model = device.remoteType;
1592
1519
  existingAccessory.context.deviceID = device.deviceId;
1593
- existingAccessory.displayName = device.deviceName;
1520
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1594
1521
  existingAccessory.context.firmwareRevision = device.firmware;
1595
1522
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1596
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1523
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1524
+ existingAccessory.context.connectionType = await this.connectionType(device);
1597
1525
  this.api.updatePlatformAccessories([existingAccessory]);
1598
1526
  // create the accessory handler for the restored accessory
1599
1527
  // this is imported from `platformAccessory.ts`
@@ -1616,17 +1544,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1616
1544
  accessory.context.deviceID = device.deviceId;
1617
1545
  accessory.context.firmwareRevision = device.firmware;
1618
1546
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1619
- await this.connectionTypeNewIRAccessory(accessory);
1547
+ accessory.context.connectionType = await this.connectionType(device);
1620
1548
  // create the accessory handler for the newly create accessory
1621
1549
  // this is imported from `platformAccessory.ts`
1622
1550
  new VacuumCleaner(this, accessory, device);
1623
1551
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1624
1552
 
1625
- // link the accessory to your platform
1626
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1553
+ // publish device externally or link the accessory to your platform
1554
+ this.externalOrPlatform(device, accessory);
1627
1555
  this.accessories.push(accessory);
1628
1556
  } else {
1629
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1557
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1630
1558
  }
1631
1559
  }
1632
1560
 
@@ -1640,15 +1568,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1640
1568
  if (existingAccessory) {
1641
1569
  // the accessory already exists
1642
1570
  if (!device.hide_device && device.hubDeviceId) {
1643
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1644
-
1645
1571
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1646
1572
  existingAccessory.context.model = device.remoteType;
1647
1573
  existingAccessory.context.deviceID = device.deviceId;
1648
- existingAccessory.displayName = device.deviceName;
1574
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1649
1575
  existingAccessory.context.firmwareRevision = device.firmware;
1650
1576
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1651
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1577
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1578
+ existingAccessory.context.connectionType = await this.connectionType(device);
1652
1579
  this.api.updatePlatformAccessories([existingAccessory]);
1653
1580
  // create the accessory handler for the restored accessory
1654
1581
  // this is imported from `platformAccessory.ts`
@@ -1671,17 +1598,17 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1671
1598
  accessory.context.deviceID = device.deviceId;
1672
1599
  accessory.context.firmwareRevision = device.firmware;
1673
1600
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1674
- await this.connectionTypeNewIRAccessory(accessory);
1601
+ accessory.context.connectionType = await this.connectionType(device);
1675
1602
  // create the accessory handler for the newly create accessory
1676
1603
  // this is imported from `platformAccessory.ts`
1677
1604
  new Camera(this, accessory, device);
1678
1605
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1679
1606
 
1680
- // link the accessory to your platform
1681
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1607
+ // publish device externally or link the accessory to your platform
1608
+ this.externalOrPlatform(device, accessory);
1682
1609
  this.accessories.push(accessory);
1683
1610
  } else {
1684
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1611
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1685
1612
  }
1686
1613
  }
1687
1614
 
@@ -1695,15 +1622,14 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1695
1622
  if (existingAccessory) {
1696
1623
  // the accessory already exists
1697
1624
  if (!device.hide_device && device.hubDeviceId) {
1698
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1699
-
1700
1625
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1701
1626
  existingAccessory.context.model = device.remoteType;
1702
1627
  existingAccessory.context.deviceID = device.deviceId;
1703
- existingAccessory.displayName = device.deviceName;
1628
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1704
1629
  existingAccessory.context.firmwareRevision = device.firmware;
1705
1630
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1706
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1631
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1632
+ existingAccessory.context.connectionType = await this.connectionType(device);
1707
1633
  this.api.updatePlatformAccessories([existingAccessory]);
1708
1634
  // create the accessory handler for the restored accessory
1709
1635
  // this is imported from `platformAccessory.ts`
@@ -1726,42 +1652,117 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1726
1652
  accessory.context.deviceID = device.deviceId;
1727
1653
  accessory.context.firmwareRevision = device.firmware;
1728
1654
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1729
- await this.connectionTypeNewIRAccessory(accessory);
1655
+ accessory.context.connectionType = await this.connectionType(device);
1730
1656
  // create the accessory handler for the newly create accessory
1731
1657
  // this is imported from `platformAccessory.ts`
1732
1658
  new Others(this, accessory, device);
1733
1659
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1734
1660
 
1735
- // link the accessory to your platform
1736
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1661
+ // publish device externally or link the accessory to your platform
1662
+ this.externalOrPlatform(device, accessory);
1737
1663
  this.accessories.push(accessory);
1738
1664
  } else {
1739
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1665
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1740
1666
  }
1741
1667
  }
1742
1668
 
1743
- public async connectionTypeNewAccessory(device: device & devicesConfig, accessory: PlatformAccessory) {
1744
- if (device.ble) {
1745
- accessory.context.connectionType = 'BLE';
1669
+ async registerCurtains(device: device & devicesConfig) {
1670
+ this.warnLog(`deviceName: ${device.deviceName} deviceId: ${device.deviceId}, curtainDevicesIds: ${device.curtainDevicesIds}, master: ` +
1671
+ `${device.master}, group: ${device.group}, disable_group: ${device.curtain?.disable_group}, connectionType: ${device.connectionType}`);
1672
+
1673
+ let registerCurtain: boolean;
1674
+ if (device.group && !device.curtain?.disable_group) {
1675
+ registerCurtain = true;
1676
+ this.debugLog(`deviceName: ${device.deviceName} [Curtain Config] disable_group: ${device.curtain?.disable_group}`);
1677
+ this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`);
1678
+ } else if (device.connectionType?.includes('BLE') || device.connectionType === 'Disabled') {
1679
+ registerCurtain = true;
1680
+ this.debugLog(`deviceName: ${device.deviceName} [Curtain Config] connectionType: ${device.connectionType}`);
1681
+ this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`);
1746
1682
  } else {
1747
- accessory.context.connectionType = 'OpenAPI';
1683
+ registerCurtain = false;
1684
+ this.errorLog(`deviceName: ${device.deviceName} [Curtain Config] disable_group: ${device.curtain?.disable_group}, UnGrouping ${device.master}`);
1685
+ this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`);
1748
1686
  }
1687
+ return registerCurtain;
1749
1688
  }
1750
1689
 
1751
- public async connectionTypeExistingAccessory(device: device & devicesConfig, existingAccessory: PlatformAccessory) {
1752
- if (device.ble) {
1753
- existingAccessory.context.connectionType = 'BLE';
1690
+ async connectionType(device: device & devicesConfig): Promise<any> {
1691
+ let connectionType: string;
1692
+ if (!device.connectionType && this.config.credentials?.token && this.config.credentials.secret) {
1693
+ connectionType = 'OpenAPI';
1754
1694
  } else {
1755
- existingAccessory.context.connectionType = 'OpenAPI';
1695
+ connectionType = device.connectionType!;
1756
1696
  }
1697
+ return connectionType;
1757
1698
  }
1758
1699
 
1759
- public async connectionTypeNewIRAccessory(accessory: PlatformAccessory) {
1760
- accessory.context.connectionType = 'IR wirth OpenAPI';
1700
+ async registerDevice(device: device & devicesConfig) {
1701
+ device.connectionType = await this.connectionType(device);
1702
+ let registerDevice: boolean;
1703
+ if (!device.hide_device && device.enableCloudService && device.connectionType === 'BLE/OpenAPI') {
1704
+ if (device.deviceType === 'Curtain') {
1705
+ registerDevice = await this.registerCurtains(device);
1706
+ this.debugWarnLog(`Device: ${device.deviceName} Curtain registerDevice: ${registerDevice}`);
1707
+ } else {
1708
+ registerDevice = true;
1709
+ this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
1710
+ }
1711
+ this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will display in HomeKit`);
1712
+ } else if (!device.hide_device && device.deviceId && device.configDeviceType && device.configDeviceName
1713
+ && device.connectionType === 'BLE') {
1714
+ if (device.deviceType === 'Curtain') {
1715
+ registerDevice = await this.registerCurtains(device);
1716
+ this.debugWarnLog(`Device: ${device.deviceName} Curtain registerDevice: ${registerDevice}`);
1717
+ } else {
1718
+ registerDevice = true;
1719
+ this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
1720
+ }
1721
+ this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will display in HomeKit`);
1722
+ } else if (!device.hide_device && device.enableCloudService && device.connectionType === 'OpenAPI') {
1723
+ if (device.deviceType === 'Curtain') {
1724
+ registerDevice = await this.registerCurtains(device);
1725
+ this.debugWarnLog(`Device: ${device.deviceName} Curtain registerDevice: ${registerDevice}`);
1726
+ } else {
1727
+ registerDevice = true;
1728
+ this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
1729
+ }
1730
+ this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will display in HomeKit`);
1731
+ } else if (!device.hide_device && device.connectionType === 'Disabled') {
1732
+ if (device.deviceType === 'Curtain') {
1733
+ registerDevice = await this.registerCurtains(device);
1734
+ this.debugWarnLog(`Device: ${device.deviceName} Curtain registerDevice: ${registerDevice}`);
1735
+ } else {
1736
+ registerDevice = true;
1737
+ this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
1738
+ }
1739
+ this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will continue to display in HomeKit`);
1740
+ } else if (!device.connectionType && !device.hide_device) {
1741
+ registerDevice = false;
1742
+ this.debugErrorLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will not display in HomeKit`);
1743
+ } else if (device.hide_device){
1744
+ registerDevice = false;
1745
+ this.debugErrorLog(`Device: ${device.deviceName} hide_device: ${device.hide_device}, will not display in HomeKit`);
1746
+ } else {
1747
+ registerDevice = false;
1748
+ this.debugErrorLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, hide_device: `
1749
+ + `${device.hide_device}, will not display in HomeKit`);
1750
+ }
1751
+ return registerDevice;
1761
1752
  }
1762
1753
 
1763
- public async connectionTypeExistingIRAccessory(existingAccessory: PlatformAccessory) {
1764
- existingAccessory.context.connectionType = 'IR with OpenAPI';
1754
+ public async externalOrPlatform(device: device & devicesConfig, accessory: PlatformAccessory) {
1755
+ this.debugLog(`${accessory.displayName} External Accessory Mode: ${device.external}`);
1756
+ if (device.external) {
1757
+ this.warnLog(`${accessory.displayName} External Accessory Mode`);
1758
+ this.externalAccessory(accessory);
1759
+ } else {
1760
+ this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
1761
+ }
1762
+ }
1763
+
1764
+ public async externalAccessory(accessory: PlatformAccessory) {
1765
+ this.api.publishExternalAccessories(PLUGIN_NAME, [accessory]);
1765
1766
  }
1766
1767
 
1767
1768
  public unregisterPlatformAccessories(existingAccessory: PlatformAccessory) {
@@ -1770,53 +1771,35 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1770
1771
  this.warnLog(`Removing existing accessory from cache: ${existingAccessory.displayName}`);
1771
1772
  }
1772
1773
 
1773
- public deviceListInfo(devices) {
1774
- if (this.platformLogging?.includes('debug')) {
1775
- this.warnLog(`deviceListInfoStatus: ${JSON.stringify(devices)}`);
1776
- }
1777
- }
1778
-
1779
- public async deviceInfo(device: (irdevice & devicesConfig) | (device & devicesConfig)) {
1780
- if (this.platformLogging?.includes('debug')) {
1781
- this.warnLog(JSON.stringify(device));
1782
-
1783
- const t = Date.now();
1784
- const nonce = 'requestID';
1785
- const data = this.config.credentials?.token + t + nonce;
1786
- const signTerm = crypto.createHmac('sha256', this.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
1787
- const sign = signTerm.toString('base64');
1788
- this.debugLog(sign);
1789
- const options = {
1790
- hostname: HostDomain,
1791
- port: 443,
1792
- path: `${DevicePath}/${device.deviceId}/status`,
1793
- method: 'GET',
1794
- headers: {
1795
- Authorization: this.config.credentials?.token,
1796
- sign: sign,
1797
- nonce: nonce,
1798
- t: t,
1799
- 'Content-Type': 'application/json',
1800
- },
1801
- };
1802
- const req = https.request(options, (res) => {
1803
- this.debugLog(`statusCode: ${res.statusCode}`);
1804
- res.on('data', (d) => {
1805
- process.stdout.write(d);
1806
- });
1807
- });
1808
- req.on('error', (error) => {
1809
- this.errorLog(error);
1810
- });
1811
- req.end();
1812
-
1813
- this.deviceStatus = req;
1814
- if (this.deviceStatus.message === 'success') {
1815
- this.warnLog(`${device.deviceName} deviceInfoStatus: ${JSON.stringify(this.deviceStatus)}`);
1816
- } else {
1817
- this.warnLog(`${device.deviceName} deviceInfoStatus: ${JSON.stringify(this.deviceStatus.message)}`);
1818
- this.warnLog('Unable to retreive deviceInfoStatus.');
1819
- }
1774
+ async statusCode({ res }: { res: IncomingMessage }): Promise<void> {
1775
+ switch (res.statusCode) {
1776
+ case 151:
1777
+ this.errorLog('Command not supported by this device type.');
1778
+ break;
1779
+ case 152:
1780
+ this.errorLog('Device not found.');
1781
+ break;
1782
+ case 160:
1783
+ this.errorLog('Command is not supported.');
1784
+ break;
1785
+ case 161:
1786
+ this.errorLog('Device is offline.');
1787
+ break;
1788
+ case 171:
1789
+ this.errorLog('is offline');
1790
+ break;
1791
+ case 190:
1792
+ this.errorLog('Requests reached the daily limit');
1793
+ break;
1794
+ case 100:
1795
+ if (this.debugMode) {
1796
+ this.debugLog('Command successfully sent.');
1797
+ }
1798
+ break;
1799
+ default:
1800
+ if (this.debugMode) {
1801
+ this.debugLog('Unknown statusCode.');
1802
+ }
1820
1803
  }
1821
1804
  }
1822
1805
 
@@ -1827,13 +1810,27 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1827
1810
  try {
1828
1811
  Switchbot = require('node-switchbot');
1829
1812
  switchbot = new Switchbot();
1830
- } catch (e) {
1813
+ } catch (e: any) {
1831
1814
  switchbot = false;
1832
1815
  this.errorLog(`Was 'node-switchbot' found: ${switchbot}`);
1833
1816
  }
1834
1817
  return switchbot;
1835
1818
  }
1836
1819
 
1820
+ logs() {
1821
+ this.debugMode = process.argv.includes('-D') || process.argv.includes('--debug');
1822
+ if (this.config.options?.logging === 'debug' || this.config.options?.logging === 'standard' || this.config.options?.logging === 'none') {
1823
+ this.platformLogging = this.config.options!.logging;
1824
+ this.debugWarnLog(`Using Config Logging: ${this.platformLogging}`);
1825
+ } else if (this.debugMode) {
1826
+ this.platformLogging = 'debugMode';
1827
+ this.debugWarnLog(`Using ${this.platformLogging} Logging`);
1828
+ } else {
1829
+ this.platformLogging = 'standard';
1830
+ this.debugWarnLog(`Using ${this.platformLogging} Logging`);
1831
+ }
1832
+ }
1833
+
1837
1834
  /**
1838
1835
  * If device level logging is turned on, log to log.warn
1839
1836
  * Otherwise send debug logs to log.debug
@@ -1850,12 +1847,28 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1850
1847
  }
1851
1848
  }
1852
1849
 
1850
+ debugWarnLog(...log: any[]): void {
1851
+ if (this.enablingPlatfromLogging()) {
1852
+ if (this.platformLogging?.includes('debug')) {
1853
+ this.log.warn('[DEBUG]', String(...log));
1854
+ }
1855
+ }
1856
+ }
1857
+
1853
1858
  errorLog(...log: any[]): void {
1854
1859
  if (this.enablingPlatfromLogging()) {
1855
1860
  this.log.error(String(...log));
1856
1861
  }
1857
1862
  }
1858
1863
 
1864
+ debugErrorLog(...log: any[]): void {
1865
+ if (this.enablingPlatfromLogging()) {
1866
+ if (this.platformLogging?.includes('debug')) {
1867
+ this.log.error('[DEBUG]', String(...log));
1868
+ }
1869
+ }
1870
+ }
1871
+
1859
1872
  debugLog(...log: any[]): void {
1860
1873
  if (this.enablingPlatfromLogging()) {
1861
1874
  if (this.platformLogging === 'debugMode') {