@switchbot/homebridge-switchbot 2.0.0-beta.4 → 2.0.0-beta.41

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