@switchbot/homebridge-switchbot 2.0.0-beta.9 → 2.0.1-beta.0

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