@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/dist/platform.js CHANGED
@@ -28,8 +28,10 @@ const https_1 = __importDefault(require("https"));
28
28
  const crypto_1 = __importDefault(require("crypto"));
29
29
  const buffer_1 = require("buffer");
30
30
  const fakegato_history_1 = __importDefault(require("fakegato-history"));
31
+ const super_stringify_1 = __importDefault(require("super-stringify"));
31
32
  const fs_1 = require("fs");
32
33
  const settings_1 = require("./settings");
34
+ const rxjs_1 = require("rxjs");
33
35
  /**
34
36
  * HomebridgePlatform
35
37
  * This class is the main constructor for your plugin, this is where you should
@@ -61,10 +63,8 @@ class SwitchBotPlatform {
61
63
  this.debugLog('Config OK');
62
64
  }
63
65
  catch (e) {
64
- this.errorLog(JSON.stringify(e.message));
65
- if (this.platformLogging?.includes('debug')) {
66
- this.errorLog(JSON.stringify(e));
67
- }
66
+ this.errorLog(`Verify Config, Error Message: ${e.message}`);
67
+ this.debugErrorLog(`Verify Config, Error: ${e}`);
68
68
  return;
69
69
  }
70
70
  // import fakegato-history module
@@ -80,39 +80,20 @@ class SwitchBotPlatform {
80
80
  if (this.config.credentials?.openToken && !this.config.credentials.token) {
81
81
  await this.updateToken();
82
82
  }
83
+ else if (this.config.credentials?.token && !this.config.credentials?.secret) {
84
+ // eslint-disable-next-line no-useless-escape
85
+ this.errorLog('\"secret\" config is not populated, you must populate then please restart Homebridge.');
86
+ }
83
87
  else {
84
88
  this.discoverDevices();
85
89
  }
86
90
  }
87
91
  catch (e) {
88
- this.errorLog('Failed to Discover Devices.', JSON.stringify(e.message));
89
- if (this.platformLogging?.includes('debug')) {
90
- this.errorLog(JSON.stringify(e));
91
- }
92
+ this.errorLog(`Failed to Discover, Error Message: ${e.message}`);
93
+ this.debugErrorLog(`Failed to Discover, Error: ${e}`);
92
94
  }
93
95
  });
94
96
  }
95
- logs() {
96
- this.debugMode = process.argv.includes('-D') || process.argv.includes('--debug');
97
- if (this.config.options?.logging === 'debug' || this.config.options?.logging === 'standard' || this.config.options?.logging === 'none') {
98
- this.platformLogging = this.config.options.logging;
99
- if (this.platformLogging.includes('debug')) {
100
- this.log.warn(`Using Config Logging: ${this.platformLogging}`);
101
- }
102
- }
103
- else if (this.debugMode) {
104
- this.platformLogging = 'debugMode';
105
- if (this.platformLogging?.includes('debug')) {
106
- this.log.warn(`Using ${this.platformLogging} Logging`);
107
- }
108
- }
109
- else {
110
- this.platformLogging = 'standard';
111
- if (this.platformLogging?.includes('debug')) {
112
- this.log.warn(`Using ${this.platformLogging} Logging`);
113
- }
114
- }
115
- }
116
97
  /**
117
98
  * This function is invoked when homebridge restores cached accessories from disk at startup.
118
99
  * It should be used to setup event handlers for characteristics and update respective values.
@@ -131,14 +112,14 @@ class SwitchBotPlatform {
131
112
  if (this.config.options.logging) {
132
113
  platformConfig['logging'] = this.config.options.logging;
133
114
  }
134
- if (this.config.options.logging) {
115
+ if (this.config.options.logging && this.config.options.refreshRate) {
135
116
  platformConfig['refreshRate'] = this.config.options.refreshRate;
136
117
  }
137
- if (this.config.options.logging) {
118
+ if (this.config.options.logging && this.config.options.pushRate) {
138
119
  platformConfig['pushRate'] = this.config.options.pushRate;
139
120
  }
140
121
  if (Object.entries(platformConfig).length !== 0) {
141
- this.infoLog(`Platform Config: ${JSON.stringify(platformConfig)}`);
122
+ this.infoLog(`Platform Config: ${(0, super_stringify_1.default)(platformConfig)}`);
142
123
  }
143
124
  if (this.config.options) {
144
125
  // Device Config
@@ -148,7 +129,7 @@ class SwitchBotPlatform {
148
129
  if (!deviceConfig.deviceId) {
149
130
  throw new Error('The devices config section is missing the *Device ID* in the config. Please check your config.');
150
131
  }
151
- if (!deviceConfig.configDeviceType && deviceConfig.ble) {
132
+ if (!deviceConfig.configDeviceType && deviceConfig.connectionType) {
152
133
  throw new Error('The devices config section is missing the *Device Type* in the config. Please check your config.');
153
134
  }
154
135
  }
@@ -174,37 +155,37 @@ class SwitchBotPlatform {
174
155
  if (!this.config.options.refreshRate) {
175
156
  // default 120 seconds (2 minutes)
176
157
  this.config.options.refreshRate = 120;
177
- if (this.platformLogging?.includes('debug')) {
178
- this.debugLog('Using Default Refresh Rate (2 minutes).');
179
- }
180
- if (!this.config.options.pushRate) {
181
- // default 100 milliseconds
182
- this.config.options.pushRate = 0.1;
183
- if (this.platformLogging?.includes('debug')) {
184
- this.warnLog('Using Default Push Rate.');
185
- }
186
- }
187
- if (!this.config.credentials) {
188
- if (this.platformLogging?.includes('debug')) {
189
- this.debugLog('Missing Credentials');
190
- }
191
- }
158
+ this.debugWarnLog('Using Default Refresh Rate (2 minutes).');
159
+ }
160
+ if (!this.config.options.pushRate) {
161
+ // default 100 milliseconds
162
+ this.config.options.pushRate = 0.1;
163
+ this.debugWarnLog('Using Default Push Rate.');
164
+ }
165
+ if (!this.config.credentials && !this.config.options) {
166
+ this.debugWarnLog('Missing Credentials');
167
+ }
168
+ else if (this.config.credentials && !this.config.credentials.notice) {
192
169
  if (!this.config.credentials?.token) {
193
- if (this.platformLogging?.includes('debug')) {
194
- this.errorLog('Missing token');
195
- this.warnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
196
- }
170
+ this.debugErrorLog('Missing token');
171
+ this.debugWarnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
197
172
  }
198
173
  if (this.config.credentials?.token) {
199
174
  if (!this.config.credentials?.secret) {
200
- if (this.platformLogging?.includes('debug')) {
201
- this.errorLog('Missing secret');
202
- this.warnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
203
- }
204
- throw Error('Missing secret');
175
+ this.debugErrorLog('Missing secret');
176
+ this.debugWarnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
205
177
  }
206
178
  }
207
179
  }
180
+ // Rediscover Devices
181
+ if (!this.config.options.reDiscoverDevices) {
182
+ this.config.options.reDiscoverDevices = false;
183
+ this.debugWarnLog(`Rediscover Devices disabled, discoverDevices: ${this.config.options.reDiscoverDevices}`);
184
+ }
185
+ if (!this.config.options.reDiscoverDevicesInterval && this.config.options.reDiscoverDevices) {
186
+ this.config.options.reDiscoverDevicesInterval = 1;
187
+ this.debugWarnLog(`Default Rediscover Devices Interval, discoverDevicesInterval: ${this.config.options.reDiscoverDevicesInterval} Day`);
188
+ }
208
189
  }
209
190
  /**
210
191
  * The openToken was old config.
@@ -233,20 +214,26 @@ class SwitchBotPlatform {
233
214
  throw new Error('pluginConfig.credentials is not an object');
234
215
  }
235
216
  // Move openToken to token
236
- this.warnLog('This plugin has been updated to use OpenAPI v1.1, config is set with openToken.');
237
- this.warnLog('`openToken` config has been moved to the `token` config, Please restart Homebridge.');
217
+ if (!this.config.credentials.secret) {
218
+ // eslint-disable-next-line no-useless-escape, max-len
219
+ 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');
220
+ // eslint-disable-next-line no-useless-escape
221
+ this.errorLog('\"secret\" config is not populated, you must populate then please restart Homebridge.');
222
+ }
223
+ else {
224
+ // eslint-disable-next-line no-useless-escape, max-len
225
+ 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.');
226
+ }
238
227
  // set the refresh token
239
228
  pluginConfig.credentials.token = this.config.credentials?.openToken;
240
229
  pluginConfig.credentials.openToken = 'Delete';
241
- if (this.platformLogging?.includes('debug')) {
242
- this.warnLog(`Token: ${pluginConfig.credentials.token}`);
243
- }
230
+ this.debugWarnLog(`Token: ${pluginConfig.credentials.token}`);
244
231
  // save the config, ensuring we maintain pretty json
245
232
  (0, fs_1.writeFileSync)(this.api.user.configPath(), JSON.stringify(currentConfig, null, 4));
246
233
  this.verifyConfig();
247
234
  }
248
235
  catch (e) {
249
- this.errorLog(e);
236
+ this.errorLog(`Update Token: ${e}`);
250
237
  }
251
238
  }
252
239
  /**
@@ -274,104 +261,132 @@ class SwitchBotPlatform {
274
261
  'Content-Type': 'application/json',
275
262
  },
276
263
  };
277
- const req = https_1.default.request(options, (res) => {
264
+ const req = https_1.default.request(options, async (res) => {
278
265
  this.debugLog(`statusCode: ${res.statusCode}`);
279
- this.debugLog(`headers: ${JSON.stringify(res.headers)}`);
266
+ await this.statusCode({ res });
267
+ this.debugLog(`headers: ${(0, super_stringify_1.default)(res.headers)}`);
280
268
  let rawData = '';
281
269
  res.on('data', (d) => {
282
270
  rawData += d;
271
+ this.deviceStatus = d;
283
272
  this.debugLog(`d: ${d}`);
284
273
  });
285
274
  res.on('end', () => {
286
275
  try {
287
276
  const devicesAPI = JSON.parse(rawData);
288
- this.debugLog(`devicesAPI: ${JSON.stringify(devicesAPI.body)}`);
277
+ this.debugLog(`devicesAPI: ${(0, super_stringify_1.default)(devicesAPI.body)}`);
289
278
  // SwitchBot Devices
290
- if (devicesAPI.body.deviceList.length !== 0) {
291
- this.infoLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
292
- }
293
- else {
294
- this.debugLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
295
- }
296
- const deviceLists = devicesAPI.body.deviceList;
297
- if (!this.config.options?.devices) {
298
- this.debugLog(`SwitchBot Device Config Not Set: ${JSON.stringify(this.config.options?.devices)}`);
299
- const devices = deviceLists.map((v) => v);
300
- for (const device of devices) {
301
- if (device.deviceType) {
302
- if (device.configDeviceName) {
303
- device.deviceName = device.configDeviceName;
279
+ /*if (devicesAPI.body.deviceList.length !== 0) {
280
+ this.infoLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
281
+ } else {
282
+ this.debugLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
283
+ }*/
284
+ const deviceBodyList = devicesAPI.body.deviceList;
285
+ let deviceLists;
286
+ if (deviceBodyList) {
287
+ if (deviceBodyList.length > 1) {
288
+ deviceLists = deviceBodyList;
289
+ this.infoLog(`Total SwitchBot Devices Found: ${deviceLists.length}`);
290
+ }
291
+ else {
292
+ deviceLists = [deviceBodyList];
293
+ this.infoLog(`Total SwitchBot Devices Found: ${deviceLists.length}`);
294
+ }
295
+ if (!this.config.options?.devices) {
296
+ this.debugLog(`SwitchBot Device Config Not Set: ${(0, super_stringify_1.default)(this.config.options?.devices)}`);
297
+ const devices = deviceLists.map((v) => v);
298
+ for (const device of devices) {
299
+ if (device.deviceType) {
300
+ if (device.configDeviceName) {
301
+ device.deviceName = device.configDeviceName;
302
+ }
303
+ this.createDevice(device);
304
304
  }
305
- this.createDevice(device);
306
305
  }
307
306
  }
308
- }
309
- else if (this.config.credentials?.token && this.config.options.devices) {
310
- this.debugLog(`SwitchBot Device Config Set: ${JSON.stringify(this.config.options?.devices)}`);
311
- const deviceConfigs = this.config.options?.devices;
312
- const mergeBydeviceId = (a1, a2) => a1.map((itm) => ({
313
- ...a2.find((item) => item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item),
314
- ...itm,
315
- }));
316
- const devices = mergeBydeviceId(deviceLists, deviceConfigs);
317
- this.debugLog(`SwitchBot Devices: ${JSON.stringify(devices)}`);
318
- for (const device of devices) {
319
- if (device.deviceType) {
320
- if (device.configDeviceName) {
321
- device.deviceName = device.configDeviceName;
307
+ else if (this.config.credentials?.token && this.config.options.devices) {
308
+ this.debugLog(`SwitchBot Device Config Set: ${(0, super_stringify_1.default)(this.config.options?.devices)}`);
309
+ const deviceConfigs = this.config.options?.devices;
310
+ const mergeBydeviceId = (a1, a2) => a1.map((itm) => ({
311
+ ...a2.find((item) => item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item),
312
+ ...itm,
313
+ }));
314
+ const devices = mergeBydeviceId(deviceLists, deviceConfigs);
315
+ this.debugLog(`SwitchBot Devices: ${(0, super_stringify_1.default)(devices)}`);
316
+ for (const device of devices) {
317
+ if (device.deviceType) {
318
+ if (device.configDeviceName) {
319
+ device.deviceName = device.configDeviceName;
320
+ }
321
+ this.createDevice(device);
322
322
  }
323
- this.createDevice(device);
324
323
  }
325
324
  }
325
+ else {
326
+ this.errorLog('SwitchBot Token Supplied, Issue with Auth.');
327
+ }
326
328
  }
327
329
  else {
328
- this.errorLog('SwitchBot Token Supplied, Issue with Auth.');
330
+ this.errorLog(`SwitchBot Device Request Issue: ${this.deviceStatus}`);
329
331
  }
330
332
  // IR Devices
331
- if (devicesAPI.body.infraredRemoteList.length !== 0) {
332
- this.infoLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
333
- }
334
- else {
335
- this.debugLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
336
- }
337
- const irDeviceLists = devicesAPI.body.infraredRemoteList;
338
- if (!this.config.options?.irdevices) {
339
- this.debugLog(`IR Device Config Not Set: ${JSON.stringify(this.config.options?.irdevices)}`);
340
- const devices = irDeviceLists.map((v) => v);
341
- for (const device of devices) {
342
- if (device.remoteType) {
343
- this.createIRDevice(device);
333
+ /*if (devicesAPI.body.infraredRemoteList.length !== 0) {
334
+ this.infoLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
335
+ } else {
336
+ this.debugLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
337
+ }*/
338
+ const irDeviceBodyList = devicesAPI.body.infraredRemoteList;
339
+ let irDeviceLists;
340
+ if (irDeviceBodyList) {
341
+ if (irDeviceBodyList.length > 1) {
342
+ irDeviceLists = irDeviceBodyList;
343
+ this.infoLog(`Total IR Devices Found: ${irDeviceLists.length}`);
344
+ }
345
+ else {
346
+ irDeviceLists = [irDeviceBodyList];
347
+ this.infoLog(`Total IR Devices Found: ${irDeviceLists.length}`);
348
+ }
349
+ if (!this.config.options?.irdevices) {
350
+ this.debugLog(`IR Device Config Not Set: ${(0, super_stringify_1.default)(this.config.options?.irdevices)}`);
351
+ const devices = irDeviceLists.map((v) => v);
352
+ for (const device of devices) {
353
+ if (device.remoteType) {
354
+ this.createIRDevice(device);
355
+ }
344
356
  }
345
357
  }
346
- }
347
- else {
348
- this.debugLog(`IR Device Config Set: ${JSON.stringify(this.config.options?.irdevices)}`);
349
- const irDeviceConfig = this.config.options?.irdevices;
350
- const mergeIRBydeviceId = (a1, a2) => a1.map((itm) => ({
351
- ...a2.find((item) => item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item),
352
- ...itm,
353
- }));
354
- const devices = mergeIRBydeviceId(irDeviceLists, irDeviceConfig);
355
- this.debugLog(`IR Devices: ${JSON.stringify(devices)}`);
356
- for (const device of devices) {
357
- if (device.remoteType) {
358
- this.createIRDevice(device);
358
+ else {
359
+ this.debugLog(`IR Device Config Set: ${(0, super_stringify_1.default)(this.config.options?.irdevices)}`);
360
+ const irDeviceConfig = this.config.options?.irdevices;
361
+ const mergeIRBydeviceId = (a1, a2) => a1.map((itm) => ({
362
+ ...a2.find((item) => item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item),
363
+ ...itm,
364
+ }));
365
+ const devices = mergeIRBydeviceId(irDeviceLists, irDeviceConfig);
366
+ this.debugLog(`IR Devices: ${(0, super_stringify_1.default)(devices)}`);
367
+ for (const device of devices) {
368
+ if (device.remoteType) {
369
+ this.createIRDevice(device);
370
+ }
359
371
  }
360
372
  }
361
373
  }
374
+ else {
375
+ this.errorLog(`SwitchBot IR Device Request Issue: ${this.deviceStatus}`);
376
+ }
362
377
  }
363
378
  catch (e) {
364
- this.errorLog(e.message);
379
+ this.errorLog(`API Request: ${e}`);
365
380
  }
366
381
  });
367
382
  });
368
- req.on('error', (error) => {
369
- this.errorLog(error);
383
+ req.on('error', (e) => {
384
+ this.errorLog(`req: ${e}`);
370
385
  });
371
386
  req.end();
372
387
  }
373
388
  else if (!this.config.credentials?.token && this.config.options?.devices) {
374
- this.debugLog(`SwitchBot Device Manual Config Set: ${JSON.stringify(this.config.options?.devices)}`);
389
+ this.debugLog(`SwitchBot Device Manual Config Set: ${(0, super_stringify_1.default)(this.config.options?.devices)}`);
375
390
  const deviceConfigs = this.config.options?.devices;
376
391
  const devices = deviceConfigs.map((v) => v);
377
392
  for (const device of devices) {
@@ -387,55 +402,21 @@ class SwitchBotPlatform {
387
402
  }
388
403
  }
389
404
  catch (e) {
390
- this.deviceError(e);
391
- }
392
- }
393
- deviceError(e) {
394
- if (e.message.includes('400')) {
395
- this.errorLog('Failed to Discover Devices: Bad Request');
396
- this.debugLog('The client has issued an invalid request. This is commonly used to specify validation errors in a request payload.');
397
- }
398
- else if (e.message.includes('401')) {
399
- this.errorLog('Failed to Discover Devices: Unauthorized Request');
400
- this.debugLog('Authorization for the API is required, but the request has not been authenticated.');
401
- }
402
- else if (e.message.includes('403')) {
403
- this.errorLog('Failed to Discover Devices: Forbidden Request');
404
- this.debugLog('The request has been authenticated but does not have appropriate permissions, or a requested resource is not found.');
405
- }
406
- else if (e.message.includes('404')) {
407
- this.errorLog('Failed to Discover Devices: Requst Not Found');
408
- this.debugLog('Specifies the requested path does not exist.');
409
- }
410
- else if (e.message.includes('406')) {
411
- this.errorLog('Failed to Discover Devices: Request Not Acceptable');
412
- this.debugLog('The client has requested a MIME type via the Accept header for a value not supported by the server.');
413
- }
414
- else if (e.message.includes('415')) {
415
- this.errorLog('Failed to Discover Devices: Unsupported Requst Header');
416
- this.debugLog('The client has defined a contentType header that is not supported by the server.');
417
- }
418
- else if (e.message.includes('422')) {
419
- this.errorLog('Failed to Discover Devices: Unprocessable Entity');
420
- this.debugLog('The client has made a valid request, but the server cannot process it.' +
421
- ' This is often used for APIs for which certain limits have been exceeded.');
422
- }
423
- else if (e.message.includes('429')) {
424
- this.errorLog('Failed to Discover Devices: Too Many Requests');
425
- this.debugLog('The client has exceeded the number of requests allowed for a given time window.');
426
- }
427
- else if (e.message.includes('500')) {
428
- this.errorLog('Failed to Discover Devices: Internal Server Error');
429
- this.debugLog('An unexpected error on the SmartThings servers has occurred. These errors should be rare.');
430
- }
431
- else {
432
- this.errorLog('Failed to Discover Devices');
433
- }
434
- if (this.platformLogging === 'debug') {
435
- this.errorLog(`Failed to Discover Devices, Error Message: ${JSON.stringify(e.message)}`);
436
- }
437
- if (this.platformLogging === 'debugMode') {
438
- this.errorLog(`Failed to Discover Devices, Error: ${JSON.stringify(e)}`);
405
+ this.debugErrorLog(`Failed to Discover Devices, Error Message: ${(0, super_stringify_1.default)(e.message)}`);
406
+ this.debugErrorLog(`Failed to Discover Devices, Error: ${e}`);
407
+ }
408
+ if (this.config.options?.reDiscoverDevices) {
409
+ this.debugWarnLog(`Rediscover Devices Interval, discoverDevicesInterval: ${this.config.options.reDiscoverDevicesInterval} Day`);
410
+ if (this.config.options.reDiscoverDevicesInterval > 1) {
411
+ (0, rxjs_1.interval)(this.config.options.reDiscoverDevicesInterval * 86400000)
412
+ .pipe((0, rxjs_1.take)(1))
413
+ .subscribe(async () => {
414
+ await this.discoverDevices();
415
+ });
416
+ }
417
+ else {
418
+ this.debugErrorLog(`discoverDevicesInterval must be > 1, discoverDevicesInterval: ${this.config.options.reDiscoverDevicesInterval} Day`);
419
+ }
439
420
  }
440
421
  }
441
422
  createDevice(device) {
@@ -471,7 +452,7 @@ class SwitchBotPlatform {
471
452
  this.createContact(device);
472
453
  break;
473
454
  case 'Curtain':
474
- this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
455
+ this.debugLog(`Discovered ${device.deviceType} ${device.deviceName}: ${device.deviceId}`);
475
456
  this.createCurtain(device);
476
457
  break;
477
458
  case 'Plug':
@@ -493,7 +474,6 @@ class SwitchBotPlatform {
493
474
  this.createStripLight(device);
494
475
  break;
495
476
  case 'Indoor Cam':
496
- this.deviceInfo(device);
497
477
  this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
498
478
  this.warnLog(`Device: ${device.deviceName} with Device Type: ${device.deviceType}, is currently not supported.`);
499
479
  break;
@@ -501,7 +481,6 @@ class SwitchBotPlatform {
501
481
  this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId} is Not Supported.`);
502
482
  break;
503
483
  default:
504
- this.deviceInfo(device);
505
484
  this.warnLog(`Device: ${device.deviceName} with Device Type: ${device.deviceType}, is currently not supported.`);
506
485
  this.warnLog('Submit Feature Requests Here: https://git.io/JL14Z');
507
486
  }
@@ -563,30 +542,11 @@ class SwitchBotPlatform {
563
542
  this.createOthers(device);
564
543
  break;
565
544
  default:
566
- this.deviceInfo(device);
545
+ this.debugLog(`Unsupported Device: ${(0, super_stringify_1.default)(device)}`);
567
546
  this.warnLog(`Device: ${device.deviceName} with Device Type: ${device.remoteType}, is currently not supported.`);
568
547
  this.warnLog('Submit Feature Requests Here: https://git.io/JL14Z');
569
548
  }
570
549
  }
571
- registerDevice(device) {
572
- if (!device.hide_device && device.enableCloudService && device.ble) {
573
- this.registeringDevice = true;
574
- this.debugLog(`Device: ${device.deviceName} Both OpenAPI and BLE Connections Enabled`);
575
- }
576
- else if (!device.hide_device && device.deviceId && device.configDeviceType && device.configDeviceName && !device.enableCloudService) {
577
- this.registeringDevice = true;
578
- this.debugLog(`Device: ${device.deviceName} BLE Connection Enabled`);
579
- }
580
- else if (!device.hide_device && device.enableCloudService && !device.ble) {
581
- this.registeringDevice = true;
582
- this.debugLog(`Device: ${device.deviceName} OpenAPI Connection Enabled`);
583
- }
584
- else {
585
- this.registeringDevice = false;
586
- this.debugLog(`Device: ${device.deviceName} OpenAPI and BLE Are Not Enabled`);
587
- }
588
- return this.registeringDevice;
589
- }
590
550
  async createHumidifier(device) {
591
551
  const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
592
552
  // see if an accessory with the same uuid has already been registered and restored from
@@ -594,16 +554,15 @@ class SwitchBotPlatform {
594
554
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
595
555
  if (existingAccessory) {
596
556
  // the accessory already exists
597
- if (this.registerDevice(device)) {
598
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
557
+ if (await this.registerDevice(device)) {
599
558
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
600
- //existingAccessory.context.firmwareRevision = firmware;
601
559
  existingAccessory.context.model = device.deviceType;
602
560
  existingAccessory.context.deviceID = device.deviceId;
603
- existingAccessory.displayName = device.deviceName;
561
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
604
562
  existingAccessory.context.firmwareRevision = device.firmware;
605
563
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
606
- await this.connectionTypeExistingAccessory(device, existingAccessory);
564
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
565
+ existingAccessory.context.connectionType = await this.connectionType(device);
607
566
  this.api.updatePlatformAccessories([existingAccessory]);
608
567
  // create the accessory handler for the restored accessory
609
568
  // this is imported from `platformAccessory.ts`
@@ -614,7 +573,7 @@ class SwitchBotPlatform {
614
573
  this.unregisterPlatformAccessories(existingAccessory);
615
574
  }
616
575
  }
617
- else if (this.registerDevice(device)) {
576
+ else if (await this.registerDevice(device)) {
618
577
  // the accessory does not yet exist, so we need to create it
619
578
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
620
579
  // create a new accessory
@@ -626,17 +585,17 @@ class SwitchBotPlatform {
626
585
  accessory.context.deviceID = device.deviceId;
627
586
  accessory.context.firmwareRevision = device.firmware;
628
587
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
629
- await this.connectionTypeNewAccessory(device, accessory);
588
+ accessory.context.connectionType = await this.connectionType(device);
630
589
  // create the accessory handler for the newly create accessory
631
590
  // this is imported from `platformAccessory.ts`
632
591
  new humidifier_1.Humidifier(this, accessory, device);
633
592
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
634
- // link the accessory to your platform
635
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
593
+ // publish device externally or link the accessory to your platform
594
+ this.externalOrPlatform(device, accessory);
636
595
  this.accessories.push(accessory);
637
596
  }
638
597
  else {
639
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
598
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
640
599
  }
641
600
  }
642
601
  async createBot(device) {
@@ -646,16 +605,16 @@ class SwitchBotPlatform {
646
605
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
647
606
  if (existingAccessory) {
648
607
  // the accessory already exists
649
- if (this.registerDevice(device)) {
650
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
651
- this.debugLog(JSON.stringify(device.bot?.mode));
608
+ if (await this.registerDevice(device)) {
609
+ this.debugLog((0, super_stringify_1.default)(device.bot?.mode));
652
610
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
653
611
  existingAccessory.context.model = device.deviceType;
654
612
  existingAccessory.context.deviceID = device.deviceId;
655
- existingAccessory.displayName = device.deviceName;
613
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
656
614
  existingAccessory.context.firmwareRevision = device.firmware;
657
615
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
658
- await this.connectionTypeExistingAccessory(device, existingAccessory);
616
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
617
+ existingAccessory.context.connectionType = await this.connectionType(device);
659
618
  this.api.updatePlatformAccessories([existingAccessory]);
660
619
  // create the accessory handler for the restored accessory
661
620
  // this is imported from `platformAccessory.ts`
@@ -666,10 +625,10 @@ class SwitchBotPlatform {
666
625
  this.unregisterPlatformAccessories(existingAccessory);
667
626
  }
668
627
  }
669
- else if (this.registerDevice(device)) {
628
+ else if (await this.registerDevice(device)) {
670
629
  // the accessory does not yet exist, so we need to create it
671
630
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
672
- this.debugLog(JSON.stringify(device.bot?.mode));
631
+ this.debugLog((0, super_stringify_1.default)(device.bot?.mode));
673
632
  // create a new accessory
674
633
  const accessory = new this.api.platformAccessory(device.deviceName, uuid);
675
634
  // store a copy of the device object in the `accessory.context`
@@ -679,18 +638,18 @@ class SwitchBotPlatform {
679
638
  accessory.context.deviceID = device.deviceId;
680
639
  accessory.context.firmwareRevision = device.firmware;
681
640
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
682
- await this.connectionTypeNewAccessory(device, accessory);
641
+ accessory.context.connectionType = await this.connectionType(device);
683
642
  // accessory.context.firmwareRevision = findaccessories.accessoryAttribute.softwareRevision;
684
643
  // create the accessory handler for the newly create accessory
685
644
  // this is imported from `platformAccessory.ts`
686
645
  new bot_1.Bot(this, accessory, device);
687
646
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
688
- // link the accessory to your platform
689
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
647
+ // publish device externally or link the accessory to your platform
648
+ this.externalOrPlatform(device, accessory);
690
649
  this.accessories.push(accessory);
691
650
  }
692
651
  else {
693
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
652
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
694
653
  }
695
654
  }
696
655
  async createMeter(device) {
@@ -700,15 +659,15 @@ class SwitchBotPlatform {
700
659
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
701
660
  if (existingAccessory) {
702
661
  // the accessory already exists
703
- if (this.registerDevice(device)) {
704
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
662
+ if (await this.registerDevice(device)) {
705
663
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
706
664
  existingAccessory.context.model = device.deviceType;
707
665
  existingAccessory.context.deviceID = device.deviceId;
708
- existingAccessory.displayName = device.deviceName;
666
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
709
667
  existingAccessory.context.firmwareRevision = device.firmware;
710
668
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
711
- await this.connectionTypeExistingAccessory(device, existingAccessory);
669
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
670
+ existingAccessory.context.connectionType = await this.connectionType(device);
712
671
  this.api.updatePlatformAccessories([existingAccessory]);
713
672
  // create the accessory handler for the restored accessory
714
673
  // this is imported from `platformAccessory.ts`
@@ -719,7 +678,7 @@ class SwitchBotPlatform {
719
678
  this.unregisterPlatformAccessories(existingAccessory);
720
679
  }
721
680
  }
722
- else if (this.registerDevice(device)) {
681
+ else if (await this.registerDevice(device)) {
723
682
  // the accessory does not yet exist, so we need to create it
724
683
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
725
684
  // create a new accessory
@@ -731,17 +690,17 @@ class SwitchBotPlatform {
731
690
  accessory.context.deviceID = device.deviceId;
732
691
  accessory.context.firmwareRevision = device.firmware;
733
692
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
734
- await this.connectionTypeNewAccessory(device, accessory);
693
+ accessory.context.connectionType = await this.connectionType(device);
735
694
  // create the accessory handler for the newly create accessory
736
695
  // this is imported from `platformAccessory.ts`
737
696
  new meter_1.Meter(this, accessory, device);
738
697
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
739
- // link the accessory to your platform
740
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
698
+ // publish device externally or link the accessory to your platform
699
+ this.externalOrPlatform(device, accessory);
741
700
  this.accessories.push(accessory);
742
701
  }
743
702
  else {
744
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
703
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
745
704
  }
746
705
  }
747
706
  async createMeterPlus(device) {
@@ -751,15 +710,15 @@ class SwitchBotPlatform {
751
710
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
752
711
  if (existingAccessory) {
753
712
  // the accessory already exists
754
- if (this.registerDevice(device)) {
755
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
713
+ if (await this.registerDevice(device)) {
756
714
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
757
715
  existingAccessory.context.model = device.deviceType;
758
716
  existingAccessory.context.deviceID = device.deviceId;
759
- existingAccessory.displayName = device.deviceName;
717
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
760
718
  existingAccessory.context.firmwareRevision = device.firmware;
761
719
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
762
- await this.connectionTypeExistingAccessory(device, existingAccessory);
720
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
721
+ existingAccessory.context.connectionType = await this.connectionType(device);
763
722
  this.api.updatePlatformAccessories([existingAccessory]);
764
723
  // create the accessory handler for the restored accessory
765
724
  // this is imported from `platformAccessory.ts`
@@ -770,7 +729,7 @@ class SwitchBotPlatform {
770
729
  this.unregisterPlatformAccessories(existingAccessory);
771
730
  }
772
731
  }
773
- else if (this.registerDevice(device)) {
732
+ else if (await this.registerDevice(device)) {
774
733
  // the accessory does not yet exist, so we need to create it
775
734
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
776
735
  // create a new accessory
@@ -782,17 +741,17 @@ class SwitchBotPlatform {
782
741
  accessory.context.deviceID = device.deviceId;
783
742
  accessory.context.firmwareRevision = device.firmware;
784
743
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
785
- await this.connectionTypeNewAccessory(device, accessory);
744
+ accessory.context.connectionType = await this.connectionType(device);
786
745
  // create the accessory handler for the newly create accessory
787
746
  // this is imported from `platformAccessory.ts`
788
747
  new meterplus_1.MeterPlus(this, accessory, device);
789
748
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
790
- // link the accessory to your platform
791
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
749
+ // publish device externally or link the accessory to your platform
750
+ this.externalOrPlatform(device, accessory);
792
751
  this.accessories.push(accessory);
793
752
  }
794
753
  else {
795
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
754
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
796
755
  }
797
756
  }
798
757
  async createMotion(device) {
@@ -802,15 +761,15 @@ class SwitchBotPlatform {
802
761
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
803
762
  if (existingAccessory) {
804
763
  // the accessory already exists
805
- if (this.registerDevice(device)) {
806
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
764
+ if (await this.registerDevice(device)) {
807
765
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
808
766
  existingAccessory.context.model = device.deviceType;
809
767
  existingAccessory.context.deviceID = device.deviceId;
810
- existingAccessory.displayName = device.deviceName;
768
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
811
769
  existingAccessory.context.firmwareRevision = device.firmware;
812
770
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
813
- await this.connectionTypeExistingAccessory(device, existingAccessory);
771
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
772
+ existingAccessory.context.connectionType = await this.connectionType(device);
814
773
  this.api.updatePlatformAccessories([existingAccessory]);
815
774
  // create the accessory handler for the restored accessory
816
775
  // this is imported from `platformAccessory.ts`
@@ -821,7 +780,7 @@ class SwitchBotPlatform {
821
780
  this.unregisterPlatformAccessories(existingAccessory);
822
781
  }
823
782
  }
824
- else if (this.registerDevice(device)) {
783
+ else if (await this.registerDevice(device)) {
825
784
  // the accessory does not yet exist, so we need to create it
826
785
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
827
786
  // create a new accessory
@@ -833,17 +792,17 @@ class SwitchBotPlatform {
833
792
  accessory.context.deviceID = device.deviceId;
834
793
  accessory.context.firmwareRevision = device.firmware;
835
794
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
836
- await this.connectionTypeNewAccessory(device, accessory);
795
+ accessory.context.connectionType = await this.connectionType(device);
837
796
  // create the accessory handler for the newly create accessory
838
797
  // this is imported from `platformAccessory.ts`
839
798
  new motion_1.Motion(this, accessory, device);
840
799
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
841
- // link the accessory to your platform
842
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
800
+ // publish device externally or link the accessory to your platform
801
+ this.externalOrPlatform(device, accessory);
843
802
  this.accessories.push(accessory);
844
803
  }
845
804
  else {
846
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
805
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
847
806
  }
848
807
  }
849
808
  async createContact(device) {
@@ -853,15 +812,15 @@ class SwitchBotPlatform {
853
812
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
854
813
  if (existingAccessory) {
855
814
  // the accessory already exists
856
- if (this.registerDevice(device)) {
857
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
815
+ if (await this.registerDevice(device)) {
858
816
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
859
817
  existingAccessory.context.model = device.deviceType;
860
818
  existingAccessory.context.deviceID = device.deviceId;
861
- existingAccessory.displayName = device.deviceName;
819
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
862
820
  existingAccessory.context.firmwareRevision = device.firmware;
863
821
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
864
- await this.connectionTypeExistingAccessory(device, existingAccessory);
822
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
823
+ existingAccessory.context.connectionType = await this.connectionType(device);
865
824
  this.api.updatePlatformAccessories([existingAccessory]);
866
825
  // create the accessory handler for the restored accessory
867
826
  // this is imported from `platformAccessory.ts`
@@ -872,7 +831,7 @@ class SwitchBotPlatform {
872
831
  this.unregisterPlatformAccessories(existingAccessory);
873
832
  }
874
833
  }
875
- else if (this.registerDevice(device)) {
834
+ else if (await this.registerDevice(device)) {
876
835
  // the accessory does not yet exist, so we need to create it
877
836
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
878
837
  // create a new accessory
@@ -884,17 +843,17 @@ class SwitchBotPlatform {
884
843
  accessory.context.deviceID = device.deviceId;
885
844
  accessory.context.firmwareRevision = device.firmware;
886
845
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
887
- await this.connectionTypeNewAccessory(device, accessory);
846
+ accessory.context.connectionType = await this.connectionType(device);
888
847
  // create the accessory handler for the newly create accessory
889
848
  // this is imported from `platformAccessory.ts`
890
849
  new contact_1.Contact(this, accessory, device);
891
850
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
892
- // link the accessory to your platform
893
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
851
+ // publish device externally or link the accessory to your platform
852
+ this.externalOrPlatform(device, accessory);
894
853
  this.accessories.push(accessory);
895
854
  }
896
855
  else {
897
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
856
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
898
857
  }
899
858
  }
900
859
  async createCurtain(device) {
@@ -904,15 +863,15 @@ class SwitchBotPlatform {
904
863
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
905
864
  if (existingAccessory) {
906
865
  // the accessory already exists
907
- if (this.isCurtainGrouped(device)) {
908
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
866
+ if (await this.registerDevice(device)) {
909
867
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
910
868
  existingAccessory.context.model = device.deviceType;
911
869
  existingAccessory.context.deviceID = device.deviceId;
912
- existingAccessory.displayName = device.deviceName;
870
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
913
871
  existingAccessory.context.firmwareRevision = device.firmware;
914
872
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
915
- await this.connectionTypeExistingAccessory(device, existingAccessory);
873
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
874
+ existingAccessory.context.connectionType = await this.connectionType(device);
916
875
  this.api.updatePlatformAccessories([existingAccessory]);
917
876
  // create the accessory handler for the restored accessory
918
877
  // this is imported from `platformAccessory.ts`
@@ -923,7 +882,7 @@ class SwitchBotPlatform {
923
882
  this.unregisterPlatformAccessories(existingAccessory);
924
883
  }
925
884
  }
926
- else if (this.isCurtainGrouped(device)) {
885
+ else if (await this.registerDevice(device)) {
927
886
  // the accessory does not yet exist, so we need to create it
928
887
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
929
888
  if (device.group && !device.curtain?.disable_group) {
@@ -932,10 +891,10 @@ class SwitchBotPlatform {
932
891
  }
933
892
  else {
934
893
  if (device.master) {
935
- this.debugLog(`Main Curtain: ${device.deviceName}, DeviceID: ${device.deviceId}`);
894
+ this.warnLog(`Main Curtain: ${device.deviceName}, DeviceID: ${device.deviceId}`);
936
895
  }
937
896
  else {
938
- this.debugLog(`Secondary Curtain: ${device.deviceName}, DeviceID: ${device.deviceId}`);
897
+ this.errorLog(`Secondary Curtain: ${device.deviceName}, DeviceID: ${device.deviceId}`);
939
898
  }
940
899
  }
941
900
  // create a new accessory
@@ -947,29 +906,17 @@ class SwitchBotPlatform {
947
906
  accessory.context.deviceID = device.deviceId;
948
907
  accessory.context.firmwareRevision = device.firmware;
949
908
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
950
- await this.connectionTypeNewAccessory(device, accessory);
909
+ accessory.context.connectionType = await this.connectionType(device);
951
910
  // create the accessory handler for the newly create accessory
952
911
  // this is imported from `platformAccessory.ts`
953
912
  new curtain_1.Curtain(this, accessory, device);
954
913
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
955
- // link the accessory to your platform
956
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
914
+ // publish device externally or link the accessory to your platform
915
+ this.externalOrPlatform(device, accessory);
957
916
  this.accessories.push(accessory);
958
917
  }
959
918
  else {
960
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
961
- }
962
- }
963
- isCurtainGrouped(device) {
964
- this.debugLog(`deviceId: ${device.deviceId}, curtainDevicesIds: ${device.curtainDevicesIds},` +
965
- ` master: ${device.master}, group: ${device.group}, disable_group: ${device.curtain?.disable_group}`);
966
- if (device.group && !device.curtain?.disable_group) {
967
- this.debugLog(`[Curtain Config] disable_group: ${device.curtain?.disable_group}`);
968
- return device.master && this.registerDevice(device);
969
- }
970
- else {
971
- this.debugLog(`[Curtain Config] disable_group: ${device.curtain?.disable_group}, UnGrouping ${device.master}`);
972
- return this.registerDevice(device);
919
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
973
920
  }
974
921
  }
975
922
  async createPlug(device) {
@@ -979,15 +926,15 @@ class SwitchBotPlatform {
979
926
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
980
927
  if (existingAccessory) {
981
928
  // the accessory already exists
982
- if (this.registerDevice(device)) {
983
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
929
+ if (await this.registerDevice(device)) {
984
930
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
985
931
  existingAccessory.context.model = device.deviceType;
986
932
  existingAccessory.context.deviceID = device.deviceId;
987
- existingAccessory.displayName = device.deviceName;
933
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
988
934
  existingAccessory.context.firmwareRevision = device.firmware;
989
935
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
990
- await this.connectionTypeExistingAccessory(device, existingAccessory);
936
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
937
+ existingAccessory.context.connectionType = await this.connectionType(device);
991
938
  this.api.updatePlatformAccessories([existingAccessory]);
992
939
  // create the accessory handler for the restored accessory
993
940
  // this is imported from `platformAccessory.ts`
@@ -998,7 +945,7 @@ class SwitchBotPlatform {
998
945
  this.unregisterPlatformAccessories(existingAccessory);
999
946
  }
1000
947
  }
1001
- else if (this.registerDevice(device)) {
948
+ else if (await this.registerDevice(device)) {
1002
949
  // the accessory does not yet exist, so we need to create it
1003
950
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1004
951
  // create a new accessory
@@ -1010,17 +957,17 @@ class SwitchBotPlatform {
1010
957
  accessory.context.deviceID = device.deviceId;
1011
958
  accessory.context.firmwareRevision = device.firmware;
1012
959
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1013
- await this.connectionTypeNewAccessory(device, accessory);
960
+ accessory.context.connectionType = await this.connectionType(device);
1014
961
  // create the accessory handler for the newly create accessory
1015
962
  // this is imported from `platformAccessory.ts`
1016
963
  new plug_1.Plug(this, accessory, device);
1017
964
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1018
- // link the accessory to your platform
1019
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
965
+ // publish device externally or link the accessory to your platform
966
+ this.externalOrPlatform(device, accessory);
1020
967
  this.accessories.push(accessory);
1021
968
  }
1022
969
  else {
1023
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
970
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1024
971
  }
1025
972
  }
1026
973
  async createLock(device) {
@@ -1030,15 +977,15 @@ class SwitchBotPlatform {
1030
977
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1031
978
  if (existingAccessory) {
1032
979
  // the accessory already exists
1033
- if (this.registerDevice(device)) {
1034
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
980
+ if (await this.registerDevice(device)) {
1035
981
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1036
982
  existingAccessory.context.model = device.deviceType;
1037
983
  existingAccessory.context.deviceID = device.deviceId;
1038
- existingAccessory.displayName = device.deviceName;
984
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1039
985
  existingAccessory.context.firmwareRevision = device.firmware;
1040
986
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1041
- await this.connectionTypeExistingAccessory(device, existingAccessory);
987
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
988
+ existingAccessory.context.connectionType = await this.connectionType(device);
1042
989
  this.api.updatePlatformAccessories([existingAccessory]);
1043
990
  // create the accessory handler for the restored accessory
1044
991
  // this is imported from `platformAccessory.ts`
@@ -1049,7 +996,7 @@ class SwitchBotPlatform {
1049
996
  this.unregisterPlatformAccessories(existingAccessory);
1050
997
  }
1051
998
  }
1052
- else if (this.registerDevice(device)) {
999
+ else if (await this.registerDevice(device)) {
1053
1000
  // the accessory does not yet exist, so we need to create it
1054
1001
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1055
1002
  // create a new accessory
@@ -1061,17 +1008,17 @@ class SwitchBotPlatform {
1061
1008
  accessory.context.deviceID = device.deviceId;
1062
1009
  accessory.context.firmwareRevision = device.firmware;
1063
1010
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1064
- await this.connectionTypeNewAccessory(device, accessory);
1011
+ accessory.context.connectionType = await this.connectionType(device);
1065
1012
  // create the accessory handler for the newly create accessory
1066
1013
  // this is imported from `platformAccessory.ts`
1067
1014
  new lock_1.Lock(this, accessory, device);
1068
1015
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1069
- // link the accessory to your platform
1070
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
1016
+ // publish device externally or link the accessory to your platform
1017
+ this.externalOrPlatform(device, accessory);
1071
1018
  this.accessories.push(accessory);
1072
1019
  }
1073
1020
  else {
1074
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
1021
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1075
1022
  }
1076
1023
  }
1077
1024
  async createColorBulb(device) {
@@ -1081,15 +1028,15 @@ class SwitchBotPlatform {
1081
1028
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1082
1029
  if (existingAccessory) {
1083
1030
  // the accessory already exists
1084
- if (this.registerDevice(device)) {
1085
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1031
+ if (await this.registerDevice(device)) {
1086
1032
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1087
1033
  existingAccessory.context.model = device.deviceType;
1088
1034
  existingAccessory.context.deviceID = device.deviceId;
1089
- existingAccessory.displayName = device.deviceName;
1035
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1090
1036
  existingAccessory.context.firmwareRevision = device.firmware;
1091
1037
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1092
- 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);
1093
1040
  this.api.updatePlatformAccessories([existingAccessory]);
1094
1041
  // create the accessory handler for the restored accessory
1095
1042
  // this is imported from `platformAccessory.ts`
@@ -1100,7 +1047,7 @@ class SwitchBotPlatform {
1100
1047
  this.unregisterPlatformAccessories(existingAccessory);
1101
1048
  }
1102
1049
  }
1103
- else if (this.registerDevice(device)) {
1050
+ else if (await this.registerDevice(device)) {
1104
1051
  // the accessory does not yet exist, so we need to create it
1105
1052
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1106
1053
  // create a new accessory
@@ -1112,17 +1059,17 @@ class SwitchBotPlatform {
1112
1059
  accessory.context.deviceID = device.deviceId;
1113
1060
  accessory.context.firmwareRevision = device.firmware;
1114
1061
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1115
- await this.connectionTypeNewAccessory(device, accessory);
1062
+ accessory.context.connectionType = await this.connectionType(device);
1116
1063
  // create the accessory handler for the newly create accessory
1117
1064
  // this is imported from `platformAccessory.ts`
1118
1065
  new colorbulb_1.ColorBulb(this, accessory, device);
1119
1066
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1120
- // link the accessory to your platform
1121
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
1067
+ // publish device externally or link the accessory to your platform
1068
+ this.externalOrPlatform(device, accessory);
1122
1069
  this.accessories.push(accessory);
1123
1070
  }
1124
1071
  else {
1125
- 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}`);
1126
1073
  }
1127
1074
  }
1128
1075
  async createStripLight(device) {
@@ -1132,15 +1079,15 @@ class SwitchBotPlatform {
1132
1079
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1133
1080
  if (existingAccessory) {
1134
1081
  // the accessory already exists
1135
- if (this.registerDevice(device)) {
1136
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1082
+ if (await this.registerDevice(device)) {
1137
1083
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1138
1084
  existingAccessory.context.model = device.deviceType;
1139
1085
  existingAccessory.context.deviceID = device.deviceId;
1140
- existingAccessory.displayName = device.deviceName;
1086
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1141
1087
  existingAccessory.context.firmwareRevision = device.firmware;
1142
1088
  existingAccessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1143
- await this.connectionTypeExistingAccessory(device, existingAccessory);
1089
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1090
+ existingAccessory.context.connectionType = await this.connectionType(device);
1144
1091
  this.api.updatePlatformAccessories([existingAccessory]);
1145
1092
  // create the accessory handler for the restored accessory
1146
1093
  // this is imported from `platformAccessory.ts`
@@ -1151,7 +1098,7 @@ class SwitchBotPlatform {
1151
1098
  this.unregisterPlatformAccessories(existingAccessory);
1152
1099
  }
1153
1100
  }
1154
- else if (this.registerDevice(device)) {
1101
+ else if (await this.registerDevice(device)) {
1155
1102
  // the accessory does not yet exist, so we need to create it
1156
1103
  this.infoLog(`Adding new accessory: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1157
1104
  // create a new accessory
@@ -1163,17 +1110,17 @@ class SwitchBotPlatform {
1163
1110
  accessory.context.deviceID = device.deviceId;
1164
1111
  accessory.context.firmwareRevision = device.firmware;
1165
1112
  accessory.context.deviceType = `SwitchBot: ${device.deviceType}`;
1166
- await this.connectionTypeNewAccessory(device, accessory);
1113
+ accessory.context.connectionType = await this.connectionType(device);
1167
1114
  // create the accessory handler for the newly create accessory
1168
1115
  // this is imported from `platformAccessory.ts`
1169
1116
  new striplight_1.StripLight(this, accessory, device);
1170
1117
  this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1171
- // link the accessory to your platform
1172
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
1118
+ // publish device externally or link the accessory to your platform
1119
+ this.externalOrPlatform(device, accessory);
1173
1120
  this.accessories.push(accessory);
1174
1121
  }
1175
1122
  else {
1176
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.deviceType} - ${device.deviceId}`);
1123
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} DeviceID: ${device.deviceId}`);
1177
1124
  }
1178
1125
  }
1179
1126
  async createTV(device) {
@@ -1182,14 +1129,14 @@ class SwitchBotPlatform {
1182
1129
  // the cached devices we stored in the `configureAccessory` method above
1183
1130
  const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1184
1131
  if (!device.hide_device && existingAccessory) {
1185
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1186
1132
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1187
1133
  existingAccessory.context.model = device.remoteType;
1188
1134
  existingAccessory.context.deviceID = device.deviceId;
1189
- existingAccessory.displayName = device.deviceName;
1135
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1190
1136
  existingAccessory.context.firmwareRevision = device.firmware;
1191
1137
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1192
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1138
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1139
+ existingAccessory.context.connectionType = await this.connectionType(device);
1193
1140
  this.api.updatePlatformAccessories([existingAccessory]);
1194
1141
  // create the accessory handler for the restored accessory
1195
1142
  // this is imported from `platformAccessory.ts`
@@ -1208,7 +1155,7 @@ class SwitchBotPlatform {
1208
1155
  accessory.context.deviceID = device.deviceId;
1209
1156
  accessory.context.firmwareRevision = device.firmware;
1210
1157
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1211
- await this.connectionTypeNewIRAccessory(accessory);
1158
+ accessory.context.connectionType = await this.connectionType(device);
1212
1159
  // create the accessory handler for the newly create accessory
1213
1160
  // this is imported from `platformAccessory.ts`
1214
1161
  new tv_1.TV(this, accessory, device);
@@ -1218,11 +1165,11 @@ class SwitchBotPlatform {
1218
1165
  * Only one TV can exist per bridge, to bypass this limitation, you should
1219
1166
  * publish your TV as an external accessory.
1220
1167
  */
1221
- this.api.publishExternalAccessories(settings_1.PLUGIN_NAME, [accessory]);
1168
+ this.externalAccessory(accessory);
1222
1169
  this.accessories.push(accessory);
1223
1170
  }
1224
1171
  else {
1225
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1172
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1226
1173
  }
1227
1174
  }
1228
1175
  async createFan(device) {
@@ -1233,14 +1180,14 @@ class SwitchBotPlatform {
1233
1180
  if (existingAccessory) {
1234
1181
  // the accessory already exists
1235
1182
  if (!device.hide_device && device.hubDeviceId) {
1236
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1237
1183
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1238
1184
  existingAccessory.context.model = device.remoteType;
1239
1185
  existingAccessory.context.deviceID = device.deviceId;
1240
- existingAccessory.displayName = device.deviceName;
1186
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1241
1187
  existingAccessory.context.firmwareRevision = device.firmware;
1242
1188
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1243
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1189
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1190
+ existingAccessory.context.connectionType = await this.connectionType(device);
1244
1191
  this.api.updatePlatformAccessories([existingAccessory]);
1245
1192
  // create the accessory handler for the restored accessory
1246
1193
  // this is imported from `platformAccessory.ts`
@@ -1263,17 +1210,17 @@ class SwitchBotPlatform {
1263
1210
  accessory.context.deviceID = device.deviceId;
1264
1211
  accessory.context.firmwareRevision = device.firmware;
1265
1212
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1266
- await this.connectionTypeNewIRAccessory(accessory);
1213
+ accessory.context.connectionType = await this.connectionType(device);
1267
1214
  // create the accessory handler for the newly create accessory
1268
1215
  // this is imported from `platformAccessory.ts`
1269
1216
  new fan_1.Fan(this, accessory, device);
1270
1217
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1271
- // link the accessory to your platform
1272
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
1218
+ // publish device externally or link the accessory to your platform
1219
+ this.externalOrPlatform(device, accessory);
1273
1220
  this.accessories.push(accessory);
1274
1221
  }
1275
1222
  else {
1276
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1223
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1277
1224
  }
1278
1225
  }
1279
1226
  async createLight(device) {
@@ -1284,14 +1231,14 @@ class SwitchBotPlatform {
1284
1231
  if (existingAccessory) {
1285
1232
  // the accessory already exists
1286
1233
  if (!device.hide_device && device.hubDeviceId) {
1287
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1288
1234
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1289
1235
  existingAccessory.context.model = device.remoteType;
1290
1236
  existingAccessory.context.deviceID = device.deviceId;
1291
- existingAccessory.displayName = device.deviceName;
1237
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1292
1238
  existingAccessory.context.firmwareRevision = device.firmware;
1293
1239
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1294
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1240
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1241
+ existingAccessory.context.connectionType = await this.connectionType(device);
1295
1242
  this.api.updatePlatformAccessories([existingAccessory]);
1296
1243
  // create the accessory handler for the restored accessory
1297
1244
  // this is imported from `platformAccessory.ts`
@@ -1314,17 +1261,17 @@ class SwitchBotPlatform {
1314
1261
  accessory.context.deviceID = device.deviceId;
1315
1262
  accessory.context.firmwareRevision = device.firmware;
1316
1263
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1317
- await this.connectionTypeNewIRAccessory(accessory);
1264
+ accessory.context.connectionType = await this.connectionType(device);
1318
1265
  // create the accessory handler for the newly create accessory
1319
1266
  // this is imported from `platformAccessory.ts`
1320
1267
  new light_1.Light(this, accessory, device);
1321
1268
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1322
- // link the accessory to your platform
1323
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
1269
+ // publish device externally or link the accessory to your platform
1270
+ this.externalOrPlatform(device, accessory);
1324
1271
  this.accessories.push(accessory);
1325
1272
  }
1326
1273
  else {
1327
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1274
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1328
1275
  }
1329
1276
  }
1330
1277
  async createAirConditioner(device) {
@@ -1335,14 +1282,14 @@ class SwitchBotPlatform {
1335
1282
  if (existingAccessory) {
1336
1283
  // the accessory already exists
1337
1284
  if (!device.hide_device && device.hubDeviceId) {
1338
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1339
1285
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1340
1286
  existingAccessory.context.model = device.remoteType;
1341
1287
  existingAccessory.context.deviceID = device.deviceId;
1342
- existingAccessory.displayName = device.deviceName;
1288
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1343
1289
  existingAccessory.context.firmwareRevision = device.firmware;
1344
1290
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1345
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1291
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1292
+ existingAccessory.context.connectionType = await this.connectionType(device);
1346
1293
  this.api.updatePlatformAccessories([existingAccessory]);
1347
1294
  // create the accessory handler for the restored accessory
1348
1295
  // this is imported from `platformAccessory.ts`
@@ -1365,17 +1312,17 @@ class SwitchBotPlatform {
1365
1312
  accessory.context.deviceID = device.deviceId;
1366
1313
  accessory.context.firmwareRevision = device.firmware;
1367
1314
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1368
- await this.connectionTypeNewIRAccessory(accessory);
1315
+ accessory.context.connectionType = await this.connectionType(device);
1369
1316
  // create the accessory handler for the newly create accessory
1370
1317
  // this is imported from `platformAccessory.ts`
1371
1318
  new airconditioner_1.AirConditioner(this, accessory, device);
1372
1319
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1373
- // link the accessory to your platform
1374
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
1320
+ // publish device externally or link the accessory to your platform
1321
+ this.externalOrPlatform(device, accessory);
1375
1322
  this.accessories.push(accessory);
1376
1323
  }
1377
1324
  else {
1378
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1325
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1379
1326
  }
1380
1327
  }
1381
1328
  async createAirPurifier(device) {
@@ -1386,14 +1333,14 @@ class SwitchBotPlatform {
1386
1333
  if (existingAccessory) {
1387
1334
  // the accessory already exists
1388
1335
  if (!device.hide_device && device.hubDeviceId) {
1389
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1390
1336
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1391
1337
  existingAccessory.context.model = device.remoteType;
1392
1338
  existingAccessory.context.deviceID = device.deviceId;
1393
- existingAccessory.displayName = device.deviceName;
1339
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1394
1340
  existingAccessory.context.firmwareRevision = device.firmware;
1395
1341
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1396
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1342
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1343
+ existingAccessory.context.connectionType = await this.connectionType(device);
1397
1344
  this.api.updatePlatformAccessories([existingAccessory]);
1398
1345
  // create the accessory handler for the restored accessory
1399
1346
  // this is imported from `platformAccessory.ts`
@@ -1416,17 +1363,17 @@ class SwitchBotPlatform {
1416
1363
  accessory.context.deviceID = device.deviceId;
1417
1364
  accessory.context.firmwareRevision = device.firmware;
1418
1365
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1419
- await this.connectionTypeNewIRAccessory(accessory);
1366
+ accessory.context.connectionType = await this.connectionType(device);
1420
1367
  // create the accessory handler for the newly create accessory
1421
1368
  // this is imported from `platformAccessory.ts`
1422
1369
  new airpurifier_1.AirPurifier(this, accessory, device);
1423
1370
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1424
- // link the accessory to your platform
1425
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
1371
+ // publish device externally or link the accessory to your platform
1372
+ this.externalOrPlatform(device, accessory);
1426
1373
  this.accessories.push(accessory);
1427
1374
  }
1428
1375
  else {
1429
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1376
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1430
1377
  }
1431
1378
  }
1432
1379
  async createWaterHeater(device) {
@@ -1437,14 +1384,14 @@ class SwitchBotPlatform {
1437
1384
  if (existingAccessory) {
1438
1385
  // the accessory already exists
1439
1386
  if (!device.hide_device && device.hubDeviceId) {
1440
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1441
1387
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1442
1388
  existingAccessory.context.model = device.remoteType;
1443
1389
  existingAccessory.context.deviceID = device.deviceId;
1444
- existingAccessory.displayName = device.deviceName;
1390
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1445
1391
  existingAccessory.context.firmwareRevision = device.firmware;
1446
1392
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1447
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1393
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1394
+ existingAccessory.context.connectionType = await this.connectionType(device);
1448
1395
  this.api.updatePlatformAccessories([existingAccessory]);
1449
1396
  // create the accessory handler for the restored accessory
1450
1397
  // this is imported from `platformAccessory.ts`
@@ -1467,17 +1414,17 @@ class SwitchBotPlatform {
1467
1414
  accessory.context.deviceID = device.deviceId;
1468
1415
  accessory.context.firmwareRevision = device.firmware;
1469
1416
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1470
- await this.connectionTypeNewIRAccessory(accessory);
1417
+ accessory.context.connectionType = await this.connectionType(device);
1471
1418
  // create the accessory handler for the newly create accessory
1472
1419
  // this is imported from `platformAccessory.ts`
1473
1420
  new waterheater_1.WaterHeater(this, accessory, device);
1474
1421
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1475
- // link the accessory to your platform
1476
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
1422
+ // publish device externally or link the accessory to your platform
1423
+ this.externalOrPlatform(device, accessory);
1477
1424
  this.accessories.push(accessory);
1478
1425
  }
1479
1426
  else {
1480
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1427
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1481
1428
  }
1482
1429
  }
1483
1430
  async createVacuumCleaner(device) {
@@ -1488,14 +1435,14 @@ class SwitchBotPlatform {
1488
1435
  if (existingAccessory) {
1489
1436
  // the accessory already exists
1490
1437
  if (!device.hide_device && device.hubDeviceId) {
1491
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1492
1438
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1493
1439
  existingAccessory.context.model = device.remoteType;
1494
1440
  existingAccessory.context.deviceID = device.deviceId;
1495
- existingAccessory.displayName = device.deviceName;
1441
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1496
1442
  existingAccessory.context.firmwareRevision = device.firmware;
1497
1443
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1498
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1444
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1445
+ existingAccessory.context.connectionType = await this.connectionType(device);
1499
1446
  this.api.updatePlatformAccessories([existingAccessory]);
1500
1447
  // create the accessory handler for the restored accessory
1501
1448
  // this is imported from `platformAccessory.ts`
@@ -1518,17 +1465,17 @@ class SwitchBotPlatform {
1518
1465
  accessory.context.deviceID = device.deviceId;
1519
1466
  accessory.context.firmwareRevision = device.firmware;
1520
1467
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1521
- await this.connectionTypeNewIRAccessory(accessory);
1468
+ accessory.context.connectionType = await this.connectionType(device);
1522
1469
  // create the accessory handler for the newly create accessory
1523
1470
  // this is imported from `platformAccessory.ts`
1524
1471
  new vacuumcleaner_1.VacuumCleaner(this, accessory, device);
1525
1472
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1526
- // link the accessory to your platform
1527
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
1473
+ // publish device externally or link the accessory to your platform
1474
+ this.externalOrPlatform(device, accessory);
1528
1475
  this.accessories.push(accessory);
1529
1476
  }
1530
1477
  else {
1531
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1478
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1532
1479
  }
1533
1480
  }
1534
1481
  async createCamera(device) {
@@ -1539,14 +1486,14 @@ class SwitchBotPlatform {
1539
1486
  if (existingAccessory) {
1540
1487
  // the accessory already exists
1541
1488
  if (!device.hide_device && device.hubDeviceId) {
1542
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1543
1489
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1544
1490
  existingAccessory.context.model = device.remoteType;
1545
1491
  existingAccessory.context.deviceID = device.deviceId;
1546
- existingAccessory.displayName = device.deviceName;
1492
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1547
1493
  existingAccessory.context.firmwareRevision = device.firmware;
1548
1494
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1549
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1495
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1496
+ existingAccessory.context.connectionType = await this.connectionType(device);
1550
1497
  this.api.updatePlatformAccessories([existingAccessory]);
1551
1498
  // create the accessory handler for the restored accessory
1552
1499
  // this is imported from `platformAccessory.ts`
@@ -1569,17 +1516,17 @@ class SwitchBotPlatform {
1569
1516
  accessory.context.deviceID = device.deviceId;
1570
1517
  accessory.context.firmwareRevision = device.firmware;
1571
1518
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1572
- await this.connectionTypeNewIRAccessory(accessory);
1519
+ accessory.context.connectionType = await this.connectionType(device);
1573
1520
  // create the accessory handler for the newly create accessory
1574
1521
  // this is imported from `platformAccessory.ts`
1575
1522
  new camera_1.Camera(this, accessory, device);
1576
1523
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1577
- // link the accessory to your platform
1578
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
1524
+ // publish device externally or link the accessory to your platform
1525
+ this.externalOrPlatform(device, accessory);
1579
1526
  this.accessories.push(accessory);
1580
1527
  }
1581
1528
  else {
1582
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1529
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1583
1530
  }
1584
1531
  }
1585
1532
  async createOthers(device) {
@@ -1590,14 +1537,14 @@ class SwitchBotPlatform {
1590
1537
  if (existingAccessory) {
1591
1538
  // the accessory already exists
1592
1539
  if (!device.hide_device && device.hubDeviceId) {
1593
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1594
1540
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1595
1541
  existingAccessory.context.model = device.remoteType;
1596
1542
  existingAccessory.context.deviceID = device.deviceId;
1597
- existingAccessory.displayName = device.deviceName;
1543
+ existingAccessory.displayName = device.configDeviceName || device.deviceName;
1598
1544
  existingAccessory.context.firmwareRevision = device.firmware;
1599
1545
  existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
1600
- await this.connectionTypeExistingIRAccessory(existingAccessory);
1546
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} DeviceID: ${device.deviceId}`);
1547
+ existingAccessory.context.connectionType = await this.connectionType(device);
1601
1548
  this.api.updatePlatformAccessories([existingAccessory]);
1602
1549
  // create the accessory handler for the restored accessory
1603
1550
  // this is imported from `platformAccessory.ts`
@@ -1620,91 +1567,160 @@ class SwitchBotPlatform {
1620
1567
  accessory.context.deviceID = device.deviceId;
1621
1568
  accessory.context.firmwareRevision = device.firmware;
1622
1569
  accessory.context.deviceType = `IR: ${device.remoteType}`;
1623
- await this.connectionTypeNewIRAccessory(accessory);
1570
+ accessory.context.connectionType = await this.connectionType(device);
1624
1571
  // create the accessory handler for the newly create accessory
1625
1572
  // this is imported from `platformAccessory.ts`
1626
1573
  new other_1.Others(this, accessory, device);
1627
1574
  this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1628
- // link the accessory to your platform
1629
- this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
1575
+ // publish device externally or link the accessory to your platform
1576
+ this.externalOrPlatform(device, accessory);
1630
1577
  this.accessories.push(accessory);
1631
1578
  }
1632
1579
  else {
1633
- this.debugLog(`Unable to Register new device: ${device.deviceName} ${device.remoteType} - ${device.deviceId}`);
1580
+ this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} DeviceID: ${device.deviceId}`);
1581
+ }
1582
+ }
1583
+ async registerCurtains(device) {
1584
+ this.warnLog(`deviceName: ${device.deviceName} deviceId: ${device.deviceId}, curtainDevicesIds: ${device.curtainDevicesIds}, master: ` +
1585
+ `${device.master}, group: ${device.group}, disable_group: ${device.curtain?.disable_group}, connectionType: ${device.connectionType}`);
1586
+ let registerCurtain;
1587
+ if (device.group && !device.curtain?.disable_group) {
1588
+ registerCurtain = true;
1589
+ this.debugLog(`deviceName: ${device.deviceName} [Curtain Config] disable_group: ${device.curtain?.disable_group}`);
1590
+ this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`);
1591
+ }
1592
+ else if (device.connectionType?.includes('BLE') || device.connectionType === 'Disabled') {
1593
+ registerCurtain = true;
1594
+ this.debugLog(`deviceName: ${device.deviceName} [Curtain Config] connectionType: ${device.connectionType}`);
1595
+ this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`);
1596
+ }
1597
+ else {
1598
+ registerCurtain = false;
1599
+ this.errorLog(`deviceName: ${device.deviceName} [Curtain Config] disable_group: ${device.curtain?.disable_group}, UnGrouping ${device.master}`);
1600
+ this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`);
1634
1601
  }
1602
+ return registerCurtain;
1635
1603
  }
1636
- async connectionTypeNewAccessory(device, accessory) {
1637
- if (device.ble) {
1638
- accessory.context.connectionType = 'BLE';
1604
+ async connectionType(device) {
1605
+ let connectionType;
1606
+ if (!device.connectionType && this.config.credentials?.token && this.config.credentials.secret) {
1607
+ connectionType = 'OpenAPI';
1639
1608
  }
1640
1609
  else {
1641
- accessory.context.connectionType = 'OpenAPI';
1610
+ connectionType = device.connectionType;
1642
1611
  }
1612
+ return connectionType;
1643
1613
  }
1644
- async connectionTypeExistingAccessory(device, existingAccessory) {
1645
- if (device.ble) {
1646
- existingAccessory.context.connectionType = 'BLE';
1614
+ async registerDevice(device) {
1615
+ device.connectionType = await this.connectionType(device);
1616
+ let registerDevice;
1617
+ if (!device.hide_device && device.enableCloudService && device.connectionType === 'BLE/OpenAPI') {
1618
+ if (device.deviceType === 'Curtain') {
1619
+ registerDevice = await this.registerCurtains(device);
1620
+ this.debugWarnLog(`Device: ${device.deviceName} Curtain registerDevice: ${registerDevice}`);
1621
+ }
1622
+ else {
1623
+ registerDevice = true;
1624
+ this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
1625
+ }
1626
+ this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will display in HomeKit`);
1627
+ }
1628
+ else if (!device.hide_device && device.deviceId && device.configDeviceType && device.configDeviceName
1629
+ && device.connectionType === 'BLE') {
1630
+ if (device.deviceType === 'Curtain') {
1631
+ registerDevice = await this.registerCurtains(device);
1632
+ this.debugWarnLog(`Device: ${device.deviceName} Curtain registerDevice: ${registerDevice}`);
1633
+ }
1634
+ else {
1635
+ registerDevice = true;
1636
+ this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
1637
+ }
1638
+ this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will display in HomeKit`);
1639
+ }
1640
+ else if (!device.hide_device && device.enableCloudService && device.connectionType === 'OpenAPI') {
1641
+ if (device.deviceType === 'Curtain') {
1642
+ registerDevice = await this.registerCurtains(device);
1643
+ this.debugWarnLog(`Device: ${device.deviceName} Curtain registerDevice: ${registerDevice}`);
1644
+ }
1645
+ else {
1646
+ registerDevice = true;
1647
+ this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
1648
+ }
1649
+ this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will display in HomeKit`);
1650
+ }
1651
+ else if (!device.hide_device && device.connectionType === 'Disabled') {
1652
+ if (device.deviceType === 'Curtain') {
1653
+ registerDevice = await this.registerCurtains(device);
1654
+ this.debugWarnLog(`Device: ${device.deviceName} Curtain registerDevice: ${registerDevice}`);
1655
+ }
1656
+ else {
1657
+ registerDevice = true;
1658
+ this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
1659
+ }
1660
+ this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will continue to display in HomeKit`);
1661
+ }
1662
+ else if (!device.connectionType && !device.hide_device) {
1663
+ registerDevice = false;
1664
+ this.debugErrorLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will not display in HomeKit`);
1665
+ }
1666
+ else if (device.hide_device) {
1667
+ registerDevice = false;
1668
+ this.debugErrorLog(`Device: ${device.deviceName} hide_device: ${device.hide_device}, will not display in HomeKit`);
1647
1669
  }
1648
1670
  else {
1649
- existingAccessory.context.connectionType = 'OpenAPI';
1671
+ registerDevice = false;
1672
+ this.debugErrorLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, hide_device: `
1673
+ + `${device.hide_device}, will not display in HomeKit`);
1650
1674
  }
1675
+ return registerDevice;
1651
1676
  }
1652
- async connectionTypeNewIRAccessory(accessory) {
1653
- accessory.context.connectionType = 'IR wirth OpenAPI';
1677
+ async externalOrPlatform(device, accessory) {
1678
+ this.debugLog(`${accessory.displayName} External Accessory Mode: ${device.external}`);
1679
+ if (device.external) {
1680
+ this.warnLog(`${accessory.displayName} External Accessory Mode`);
1681
+ this.externalAccessory(accessory);
1682
+ }
1683
+ else {
1684
+ this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
1685
+ }
1654
1686
  }
1655
- async connectionTypeExistingIRAccessory(existingAccessory) {
1656
- existingAccessory.context.connectionType = 'IR with OpenAPI';
1687
+ async externalAccessory(accessory) {
1688
+ this.api.publishExternalAccessories(settings_1.PLUGIN_NAME, [accessory]);
1657
1689
  }
1658
1690
  unregisterPlatformAccessories(existingAccessory) {
1659
1691
  // remove platform accessories when no longer present
1660
1692
  this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [existingAccessory]);
1661
1693
  this.warnLog(`Removing existing accessory from cache: ${existingAccessory.displayName}`);
1662
1694
  }
1663
- deviceListInfo(devices) {
1664
- if (this.platformLogging?.includes('debug')) {
1665
- this.warnLog(`deviceListInfoStatus: ${JSON.stringify(devices)}`);
1666
- }
1667
- }
1668
- async deviceInfo(device) {
1669
- if (this.platformLogging?.includes('debug')) {
1670
- this.warnLog(JSON.stringify(device));
1671
- const t = Date.now();
1672
- const nonce = 'requestID';
1673
- const data = this.config.credentials?.token + t + nonce;
1674
- const signTerm = crypto_1.default.createHmac('sha256', this.config.credentials?.secret).update(buffer_1.Buffer.from(data, 'utf-8')).digest();
1675
- const sign = signTerm.toString('base64');
1676
- this.debugLog(sign);
1677
- const options = {
1678
- hostname: settings_1.HostDomain,
1679
- port: 443,
1680
- path: `${settings_1.DevicePath}/${device.deviceId}/status`,
1681
- method: 'GET',
1682
- headers: {
1683
- Authorization: this.config.credentials?.token,
1684
- sign: sign,
1685
- nonce: nonce,
1686
- t: t,
1687
- 'Content-Type': 'application/json',
1688
- },
1689
- };
1690
- const req = https_1.default.request(options, (res) => {
1691
- this.debugLog(`statusCode: ${res.statusCode}`);
1692
- res.on('data', (d) => {
1693
- process.stdout.write(d);
1694
- });
1695
- });
1696
- req.on('error', (error) => {
1697
- this.errorLog(error);
1698
- });
1699
- req.end();
1700
- this.deviceStatus = req;
1701
- if (this.deviceStatus.message === 'success') {
1702
- this.warnLog(`${device.deviceName} deviceInfoStatus: ${JSON.stringify(this.deviceStatus)}`);
1703
- }
1704
- else {
1705
- this.warnLog(`${device.deviceName} deviceInfoStatus: ${JSON.stringify(this.deviceStatus.message)}`);
1706
- this.warnLog('Unable to retreive deviceInfoStatus.');
1707
- }
1695
+ async statusCode({ res }) {
1696
+ switch (res.statusCode) {
1697
+ case 151:
1698
+ this.errorLog('Command not supported by this device type.');
1699
+ break;
1700
+ case 152:
1701
+ this.errorLog('Device not found.');
1702
+ break;
1703
+ case 160:
1704
+ this.errorLog('Command is not supported.');
1705
+ break;
1706
+ case 161:
1707
+ this.errorLog('Device is offline.');
1708
+ break;
1709
+ case 171:
1710
+ this.errorLog('is offline');
1711
+ break;
1712
+ case 190:
1713
+ this.errorLog('Requests reached the daily limit');
1714
+ break;
1715
+ case 100:
1716
+ if (this.debugMode) {
1717
+ this.debugLog('Command successfully sent.');
1718
+ }
1719
+ break;
1720
+ default:
1721
+ if (this.debugMode) {
1722
+ this.debugLog('Unknown statusCode.');
1723
+ }
1708
1724
  }
1709
1725
  }
1710
1726
  // BLE Connection
@@ -1721,6 +1737,21 @@ class SwitchBotPlatform {
1721
1737
  }
1722
1738
  return switchbot;
1723
1739
  }
1740
+ logs() {
1741
+ this.debugMode = process.argv.includes('-D') || process.argv.includes('--debug');
1742
+ if (this.config.options?.logging === 'debug' || this.config.options?.logging === 'standard' || this.config.options?.logging === 'none') {
1743
+ this.platformLogging = this.config.options.logging;
1744
+ this.debugWarnLog(`Using Config Logging: ${this.platformLogging}`);
1745
+ }
1746
+ else if (this.debugMode) {
1747
+ this.platformLogging = 'debugMode';
1748
+ this.debugWarnLog(`Using ${this.platformLogging} Logging`);
1749
+ }
1750
+ else {
1751
+ this.platformLogging = 'standard';
1752
+ this.debugWarnLog(`Using ${this.platformLogging} Logging`);
1753
+ }
1754
+ }
1724
1755
  /**
1725
1756
  * If device level logging is turned on, log to log.warn
1726
1757
  * Otherwise send debug logs to log.debug
@@ -1735,11 +1766,25 @@ class SwitchBotPlatform {
1735
1766
  this.log.warn(String(...log));
1736
1767
  }
1737
1768
  }
1769
+ debugWarnLog(...log) {
1770
+ if (this.enablingPlatfromLogging()) {
1771
+ if (this.platformLogging?.includes('debug')) {
1772
+ this.log.warn('[DEBUG]', String(...log));
1773
+ }
1774
+ }
1775
+ }
1738
1776
  errorLog(...log) {
1739
1777
  if (this.enablingPlatfromLogging()) {
1740
1778
  this.log.error(String(...log));
1741
1779
  }
1742
1780
  }
1781
+ debugErrorLog(...log) {
1782
+ if (this.enablingPlatfromLogging()) {
1783
+ if (this.platformLogging?.includes('debug')) {
1784
+ this.log.error('[DEBUG]', String(...log));
1785
+ }
1786
+ }
1787
+ }
1743
1788
  debugLog(...log) {
1744
1789
  if (this.enablingPlatfromLogging()) {
1745
1790
  if (this.platformLogging === 'debugMode') {