@switchbot/homebridge-switchbot 2.6.0-beta.4 → 2.6.0-beta.5

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 (138) hide show
  1. package/.vscode/tasks.json +13 -0
  2. package/dist/device/blindtilt.d.ts +1 -5
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +80 -140
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +1 -5
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +79 -138
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +2 -6
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +157 -304
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +2 -6
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +164 -311
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +1 -5
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +36 -72
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +1 -5
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +78 -138
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/hub.d.ts +2 -6
  27. package/dist/device/hub.d.ts.map +1 -1
  28. package/dist/device/hub.js +30 -63
  29. package/dist/device/hub.js.map +1 -1
  30. package/dist/device/humidifier.d.ts +1 -5
  31. package/dist/device/humidifier.d.ts.map +1 -1
  32. package/dist/device/humidifier.js +115 -228
  33. package/dist/device/humidifier.js.map +1 -1
  34. package/dist/device/iosensor.d.ts +1 -5
  35. package/dist/device/iosensor.d.ts.map +1 -1
  36. package/dist/device/iosensor.js +38 -74
  37. package/dist/device/iosensor.js.map +1 -1
  38. package/dist/device/lock.d.ts +1 -5
  39. package/dist/device/lock.d.ts.map +1 -1
  40. package/dist/device/lock.js +66 -126
  41. package/dist/device/lock.js.map +1 -1
  42. package/dist/device/meter.d.ts +1 -5
  43. package/dist/device/meter.d.ts.map +1 -1
  44. package/dist/device/meter.js +38 -74
  45. package/dist/device/meter.js.map +1 -1
  46. package/dist/device/meterplus.d.ts +1 -5
  47. package/dist/device/meterplus.d.ts.map +1 -1
  48. package/dist/device/meterplus.js +39 -75
  49. package/dist/device/meterplus.js.map +1 -1
  50. package/dist/device/motion.d.ts +1 -5
  51. package/dist/device/motion.d.ts.map +1 -1
  52. package/dist/device/motion.js +35 -72
  53. package/dist/device/motion.js.map +1 -1
  54. package/dist/device/plug.d.ts +1 -5
  55. package/dist/device/plug.d.ts.map +1 -1
  56. package/dist/device/plug.js +65 -126
  57. package/dist/device/plug.js.map +1 -1
  58. package/dist/device/robotvacuumcleaner.d.ts +1 -5
  59. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  60. package/dist/device/robotvacuumcleaner.js +79 -151
  61. package/dist/device/robotvacuumcleaner.js.map +1 -1
  62. package/dist/device/striplight.d.ts +1 -9
  63. package/dist/device/striplight.d.ts.map +1 -1
  64. package/dist/device/striplight.js +137 -193
  65. package/dist/device/striplight.js.map +1 -1
  66. package/dist/irdevice/airconditioner.d.ts +3 -7
  67. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  68. package/dist/irdevice/airconditioner.js +34 -62
  69. package/dist/irdevice/airconditioner.js.map +1 -1
  70. package/dist/irdevice/airpurifier.d.ts +3 -7
  71. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  72. package/dist/irdevice/airpurifier.js +31 -59
  73. package/dist/irdevice/airpurifier.js.map +1 -1
  74. package/dist/irdevice/camera.d.ts +1 -5
  75. package/dist/irdevice/camera.d.ts.map +1 -1
  76. package/dist/irdevice/camera.js +30 -57
  77. package/dist/irdevice/camera.js.map +1 -1
  78. package/dist/irdevice/fan.d.ts +2 -6
  79. package/dist/irdevice/fan.d.ts.map +1 -1
  80. package/dist/irdevice/fan.js +39 -66
  81. package/dist/irdevice/fan.js.map +1 -1
  82. package/dist/irdevice/light.d.ts +1 -5
  83. package/dist/irdevice/light.d.ts.map +1 -1
  84. package/dist/irdevice/light.js +32 -59
  85. package/dist/irdevice/light.js.map +1 -1
  86. package/dist/irdevice/other.d.ts +1 -5
  87. package/dist/irdevice/other.d.ts.map +1 -1
  88. package/dist/irdevice/other.js +30 -57
  89. package/dist/irdevice/other.js.map +1 -1
  90. package/dist/irdevice/tv.d.ts +3 -7
  91. package/dist/irdevice/tv.d.ts.map +1 -1
  92. package/dist/irdevice/tv.js +46 -76
  93. package/dist/irdevice/tv.js.map +1 -1
  94. package/dist/irdevice/vacuumcleaner.d.ts +2 -6
  95. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  96. package/dist/irdevice/vacuumcleaner.js +30 -58
  97. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  98. package/dist/irdevice/waterheater.d.ts +7 -23
  99. package/dist/irdevice/waterheater.d.ts.map +1 -1
  100. package/dist/irdevice/waterheater.js +41 -74
  101. package/dist/irdevice/waterheater.js.map +1 -1
  102. package/dist/platform.d.ts +14 -5
  103. package/dist/platform.d.ts.map +1 -1
  104. package/dist/platform.js +153 -146
  105. package/dist/platform.js.map +1 -1
  106. package/dist/settings.d.ts +4 -0
  107. package/dist/settings.d.ts.map +1 -1
  108. package/dist/settings.js +5 -1
  109. package/dist/settings.js.map +1 -1
  110. package/nodemon.json +1 -1
  111. package/package.json +10 -10
  112. package/src/device/blindtilt.ts +122 -181
  113. package/src/device/bot.ts +96 -149
  114. package/src/device/ceilinglight.ts +172 -315
  115. package/src/device/colorbulb.ts +178 -324
  116. package/src/device/contact.ts +48 -78
  117. package/src/device/curtain.ts +93 -148
  118. package/src/device/hub.ts +34 -63
  119. package/src/device/humidifier.ts +135 -243
  120. package/src/device/iosensor.ts +49 -79
  121. package/src/device/lock.ts +78 -134
  122. package/src/device/meter.ts +48 -78
  123. package/src/device/meterplus.ts +50 -80
  124. package/src/device/motion.ts +46 -77
  125. package/src/device/plug.ts +76 -134
  126. package/src/device/robotvacuumcleaner.ts +91 -159
  127. package/src/device/striplight.ts +148 -200
  128. package/src/irdevice/airconditioner.ts +56 -81
  129. package/src/irdevice/airpurifier.ts +41 -67
  130. package/src/irdevice/camera.ts +35 -60
  131. package/src/irdevice/fan.ts +46 -71
  132. package/src/irdevice/light.ts +38 -63
  133. package/src/irdevice/other.ts +35 -60
  134. package/src/irdevice/tv.ts +52 -80
  135. package/src/irdevice/vacuumcleaner.ts +36 -62
  136. package/src/irdevice/waterheater.ts +46 -76
  137. package/src/platform.ts +143 -157
  138. package/src/settings.ts +5 -0
@@ -1,7 +1,5 @@
1
- /// <reference types="node" />
2
1
  import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, Service, Characteristic } from 'homebridge';
3
2
  import { device, SwitchBotPlatformConfig, devicesConfig, irDevicesConfig } from './settings';
4
- import { IncomingMessage } from 'http';
5
3
  /**
6
4
  * HomebridgePlatform
7
5
  * This class is the main constructor for your plugin, this is where you should
@@ -34,8 +32,21 @@ export declare class SwitchBotPlatform implements DynamicPlatformPlugin {
34
32
  * @param this.config.credentials.openToken
35
33
  */
36
34
  updateToken(): Promise<void>;
35
+ generateHeaders: () => {
36
+ Authorization: any;
37
+ sign: string;
38
+ nonce: `${string}-${string}-${string}-${string}-${string}`;
39
+ t: string;
40
+ 'Content-Type': string;
41
+ };
37
42
  /**
38
43
  * this method discovers devices
44
+ *
45
+ const t = `${Date.now()}`;
46
+ const nonce = 'requestID';
47
+ const data = this.config.credentials?.token + t + nonce;
48
+ const signTerm = crypto.createHmac('sha256', this.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
49
+ const sign = signTerm.toString('base64');
39
50
  */
40
51
  discoverDevices(): Promise<void>;
41
52
  private createDevice;
@@ -72,9 +83,7 @@ export declare class SwitchBotPlatform implements DynamicPlatformPlugin {
72
83
  externalOrPlatform(device: device & devicesConfig, accessory: PlatformAccessory): Promise<void>;
73
84
  externalAccessory(accessory: PlatformAccessory): Promise<void>;
74
85
  unregisterPlatformAccessories(existingAccessory: PlatformAccessory): void;
75
- statusCode({ res }: {
76
- res: IncomingMessage;
77
- }): Promise<void>;
86
+ statusCode(statusCode: number): Promise<void>;
78
87
  connectBLE(): any;
79
88
  logs(): void;
80
89
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"platform.d.ts","sourceRoot":"","sources":["../src/platform.ts"],"names":[],"mappings":";AAgCA,OAAO,EAAE,GAAG,EAAE,qBAAqB,EAAE,MAAM,EAAE,iBAAiB,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5G,OAAO,EACiC,MAAM,EAAE,uBAAuB,EAAE,aAAa,EAA0B,eAAe,EAAE,MAAM,YAAY,CAAC;AACpJ,OAAO,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAEvC;;;;GAIG;AACH,qBAAa,iBAAkB,YAAW,qBAAqB;aAajC,GAAG,EAAE,MAAM;aAAkB,MAAM,EAAE,uBAAuB;aAAkB,GAAG,EAAE,GAAG;IAZlH,SAAgB,OAAO,EAAE,OAAO,OAAO,CAAwB;IAC/D,SAAgB,cAAc,EAAE,OAAO,cAAc,CAA+B;IAGpF,SAAgB,WAAW,EAAE,iBAAiB,EAAE,CAAM;IAEtD,OAAO,SAA8C;IACrD,SAAS,EAAG,OAAO,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB,SAAgB,WAAW,EAAE,GAAG,CAAC;gBAEL,GAAG,EAAE,MAAM,EAAkB,MAAM,EAAE,uBAAuB,EAAkB,GAAG,EAAE,GAAG;IAkDlH;;;OAGG;IACH,kBAAkB,CAAC,SAAS,EAAE,iBAAiB;IAO/C;;OAEG;IACG,YAAY;IA+ElB;;;;OAIG;IACG,WAAW;IAqDjB;;OAEG;IACG,eAAe;IAgJrB,OAAO,CAAC,YAAY;IA0FpB,OAAO,CAA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object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
27
  };
@@ -29,12 +52,11 @@ const airpurifier_1 = require("./irdevice/airpurifier");
29
52
  const waterheater_1 = require("./irdevice/waterheater");
30
53
  const vacuumcleaner_1 = require("./irdevice/vacuumcleaner");
31
54
  const airconditioner_1 = require("./irdevice/airconditioner");
32
- const https_1 = __importDefault(require("https"));
33
- const crypto_1 = __importDefault(require("crypto"));
55
+ const undici_1 = require("undici");
56
+ const crypto_1 = __importStar(require("crypto"));
34
57
  const buffer_1 = require("buffer");
35
58
  const rxjs_1 = require("rxjs");
36
59
  const fakegato_history_1 = __importDefault(require("fakegato-history"));
37
- const super_stringify_1 = __importDefault(require("super-stringify"));
38
60
  const fs_1 = require("fs");
39
61
  const settings_1 = require("./settings");
40
62
  /**
@@ -52,6 +74,20 @@ class SwitchBotPlatform {
52
74
  // this is used to track restored cached accessories
53
75
  this.accessories = [];
54
76
  this.version = process.env.npm_package_version || '2.1.1';
77
+ this.generateHeaders = () => {
78
+ const t = `${Date.now()}`;
79
+ const nonce = (0, crypto_1.randomUUID)();
80
+ const data = this.config.credentials?.token + t + nonce;
81
+ const signTerm = crypto_1.default.createHmac('sha256', this.config.credentials?.secret).update(buffer_1.Buffer.from(data, 'utf-8')).digest();
82
+ const sign = signTerm.toString('base64');
83
+ return {
84
+ 'Authorization': this.config.credentials?.token,
85
+ 'sign': sign,
86
+ 'nonce': nonce,
87
+ 't': t,
88
+ 'Content-Type': 'application/json',
89
+ };
90
+ };
55
91
  this.logs();
56
92
  this.debugLog('Finished initializing platform:', this.config.name);
57
93
  // only load if configured
@@ -125,7 +161,7 @@ class SwitchBotPlatform {
125
161
  platformConfig['pushRate'] = this.config.options.pushRate;
126
162
  }
127
163
  if (Object.entries(platformConfig).length !== 0) {
128
- this.infoLog(`Platform Config: ${(0, super_stringify_1.default)(platformConfig)}`);
164
+ this.infoLog(`Platform Config: ${JSON.stringify(platformConfig)}`);
129
165
  }
130
166
  if (this.config.options) {
131
167
  // Device Config
@@ -237,144 +273,115 @@ class SwitchBotPlatform {
237
273
  }
238
274
  /**
239
275
  * this method discovers devices
276
+ *
277
+ const t = `${Date.now()}`;
278
+ const nonce = 'requestID';
279
+ const data = this.config.credentials?.token + t + nonce;
280
+ const signTerm = crypto.createHmac('sha256', this.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
281
+ const sign = signTerm.toString('base64');
240
282
  */
241
283
  async discoverDevices() {
242
- try {
243
- if (this.config.credentials?.token) {
244
- const t = Date.now();
245
- const nonce = 'requestID';
246
- const data = this.config.credentials?.token + t + nonce;
247
- const signTerm = crypto_1.default.createHmac('sha256', this.config.credentials?.secret).update(buffer_1.Buffer.from(data, 'utf-8')).digest();
248
- const sign = signTerm.toString('base64');
249
- this.debugLog(`sing: ${sign}`);
250
- const options = {
251
- hostname: settings_1.HostDomain,
252
- port: 443,
253
- path: settings_1.DevicePath,
254
- method: 'GET',
255
- headers: {
256
- 'Authorization': this.config.credentials?.token,
257
- 'sign': sign,
258
- 'nonce': nonce,
259
- 't': t,
260
- 'Content-Type': 'application/json',
261
- },
262
- };
263
- const req = https_1.default.request(options, async (res) => {
264
- this.debugLog(`statusCode: ${res.statusCode}`);
265
- await this.statusCode({ res });
266
- this.debugLog(`headers: ${(0, super_stringify_1.default)(res.headers)}`);
267
- let rawData = '';
268
- res.on('data', (d) => {
269
- rawData += d;
270
- this.debugLog(`d: ${d}`);
271
- });
272
- res.on('end', () => {
273
- try {
274
- const devicesAPI = JSON.parse(rawData);
275
- this.debugLog(`devicesAPI: ${(0, super_stringify_1.default)(devicesAPI.body)}`);
276
- // SwitchBot Devices
277
- const deviceLists = devicesAPI.body.deviceList;
278
- if (!this.config.options?.devices) {
279
- this.debugLog(`SwitchBot Device Config Not Set: ${JSON.stringify(this.config.options?.devices)}`);
280
- const devices = deviceLists.map((v) => v);
281
- for (const device of devices) {
282
- if (device.deviceType) {
283
- if (device.configDeviceName) {
284
- device.deviceName = device.configDeviceName;
285
- }
286
- this.createDevice(device);
287
- }
288
- }
289
- }
290
- else if (this.config.credentials?.token && this.config.options.devices) {
291
- this.debugLog(`SwitchBot Device Config Set: ${(0, super_stringify_1.default)(this.config.options?.devices)}`);
292
- const deviceConfigs = this.config.options?.devices;
293
- const mergeBydeviceId = (a1, a2) => a1.map((itm) => ({
294
- ...a2.find((item) => item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item),
295
- ...itm,
296
- }));
297
- const devices = mergeBydeviceId(deviceLists, deviceConfigs);
298
- this.debugLog(`SwitchBot Devices: ${(0, super_stringify_1.default)(devices)}`);
299
- for (const device of devices) {
300
- if (device.deviceType) {
301
- if (device.configDeviceName) {
302
- device.deviceName = device.configDeviceName;
303
- }
304
- this.createDevice(device);
305
- }
306
- }
307
- }
308
- else {
309
- this.errorLog('SwitchBot Token Supplied, Issue with Auth.');
310
- }
311
- if (devicesAPI.body.deviceList.length !== 0) {
312
- this.infoLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
313
- }
314
- else {
315
- this.debugLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
316
- }
317
- // IR Devices
318
- const irDeviceLists = devicesAPI.body.infraredRemoteList;
319
- if (!this.config.options?.irdevices) {
320
- this.debugLog(`IR Device Config Not Set: ${JSON.stringify(this.config.options?.irdevices)}`);
321
- const devices = irDeviceLists.map((v) => v);
322
- for (const device of devices) {
323
- if (device.remoteType) {
324
- this.createIRDevice(device);
325
- }
326
- }
327
- }
328
- else {
329
- this.debugLog(`IR Device Config Set: ${(0, super_stringify_1.default)(this.config.options?.irdevices)}`);
330
- const irDeviceConfig = this.config.options?.irdevices;
331
- const mergeIRBydeviceId = (a1, a2) => a1.map((itm) => ({
332
- ...a2.find((item) => item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item),
333
- ...itm,
334
- }));
335
- const devices = mergeIRBydeviceId(irDeviceLists, irDeviceConfig);
336
- this.debugLog(`IR Devices: ${(0, super_stringify_1.default)(devices)}`);
337
- for (const device of devices) {
338
- this.createIRDevice(device);
339
- }
340
- }
341
- if (devicesAPI.body.infraredRemoteList.length !== 0) {
342
- this.infoLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
284
+ if (this.config.credentials?.token) {
285
+ try {
286
+ const { body, statusCode, headers } = await (0, undici_1.request)(settings_1.Devices, {
287
+ headers: this.generateHeaders(),
288
+ });
289
+ const devicesAPI = await body.json();
290
+ this.debugLog(`Devices: ${JSON.stringify(devicesAPI.body)}`);
291
+ this.statusCode(statusCode);
292
+ this.debugLog(`Headers: ${JSON.stringify(headers)}`);
293
+ // SwitchBot Devices
294
+ const deviceLists = devicesAPI.body.deviceList;
295
+ if (!this.config.options?.devices) {
296
+ this.debugLog(`SwitchBot Device Config Not Set: ${JSON.stringify(this.config.options?.devices)}`);
297
+ const devices = deviceLists.map((v) => v);
298
+ for (const device of devices) {
299
+ if (device.deviceType) {
300
+ if (device.configDeviceName) {
301
+ device.deviceName = device.configDeviceName;
343
302
  }
344
- else {
345
- this.debugLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
303
+ this.createDevice(device);
304
+ }
305
+ }
306
+ }
307
+ else if (this.config.credentials?.token && this.config.options.devices) {
308
+ this.debugLog(`SwitchBot Device Config Set: ${JSON.stringify(this.config.options?.devices)}`);
309
+ const deviceConfigs = this.config.options?.devices;
310
+ const mergeBydeviceId = (a1, a2) => a1.map((itm) => ({
311
+ ...a2.find((item) => item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item),
312
+ ...itm,
313
+ }));
314
+ const devices = mergeBydeviceId(deviceLists, deviceConfigs);
315
+ this.debugLog(`SwitchBot Devices: ${JSON.stringify(devices)}`);
316
+ for (const device of devices) {
317
+ if (device.deviceType) {
318
+ if (device.configDeviceName) {
319
+ device.deviceName = device.configDeviceName;
346
320
  }
321
+ this.createDevice(device);
347
322
  }
348
- catch (e) {
349
- this.errorLog(`API Request: ${e}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug');
323
+ }
324
+ }
325
+ else {
326
+ this.errorLog('SwitchBot Token Supplied, Issue with Auth.');
327
+ }
328
+ if (devicesAPI.body.deviceList.length !== 0) {
329
+ this.infoLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
330
+ }
331
+ else {
332
+ this.debugLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
333
+ }
334
+ // IR Devices
335
+ const irDeviceLists = devicesAPI.body.infraredRemoteList;
336
+ if (!this.config.options?.irdevices) {
337
+ this.debugLog(`IR Device Config Not Set: ${JSON.stringify(this.config.options?.irdevices)}`);
338
+ const devices = irDeviceLists.map((v) => v);
339
+ for (const device of devices) {
340
+ if (device.remoteType) {
341
+ this.createIRDevice(device);
350
342
  }
351
- });
352
- });
353
- req.on('error', (e) => {
354
- this.errorLog(`req: ${e}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug');
355
- });
356
- req.end();
357
- }
358
- else if (!this.config.credentials?.token && this.config.options?.devices) {
359
- this.debugLog(`SwitchBot Device Manual Config Set: ${(0, super_stringify_1.default)(this.config.options?.devices)}`);
360
- const deviceConfigs = this.config.options?.devices;
361
- const devices = deviceConfigs.map((v) => v);
362
- for (const device of devices) {
363
- device.deviceType = device.configDeviceType;
364
- device.deviceName = device.configDeviceName;
365
- if (device.deviceType) {
366
- this.createDevice(device);
367
343
  }
368
344
  }
345
+ else {
346
+ this.debugLog(`IR Device Config Set: ${JSON.stringify(this.config.options?.irdevices)}`);
347
+ const irDeviceConfig = this.config.options?.irdevices;
348
+ const mergeIRBydeviceId = (a1, a2) => a1.map((itm) => ({
349
+ ...a2.find((item) => item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item),
350
+ ...itm,
351
+ }));
352
+ const devices = mergeIRBydeviceId(irDeviceLists, irDeviceConfig);
353
+ this.debugLog(`IR Devices: ${JSON.stringify(devices)}`);
354
+ for (const device of devices) {
355
+ this.createIRDevice(device);
356
+ }
357
+ }
358
+ if (devicesAPI.body.infraredRemoteList.length !== 0) {
359
+ this.infoLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
360
+ }
361
+ else {
362
+ this.debugLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
363
+ }
369
364
  }
370
- else {
371
- this.errorLog('Neither SwitchBot Token or Device Config are not set.');
365
+ catch (e) {
366
+ this.debugErrorLog(`Failed to Discover Devices, Error Message: ${JSON.stringify(e.message)}, Submit Bugs Here: `
367
+ + 'https://tinyurl.com/SwitchBotBug');
368
+ this.debugErrorLog(`Failed to Discover Devices, Error: ${e}`);
369
+ }
370
+ }
371
+ else if (!this.config.credentials?.token && this.config.options?.devices) {
372
+ this.debugLog(`SwitchBot Device Manual Config Set: ${JSON.stringify(this.config.options?.devices)}`);
373
+ const deviceConfigs = this.config.options?.devices;
374
+ const devices = deviceConfigs.map((v) => v);
375
+ for (const device of devices) {
376
+ device.deviceType = device.configDeviceType;
377
+ device.deviceName = device.configDeviceName;
378
+ if (device.deviceType) {
379
+ this.createDevice(device);
380
+ }
372
381
  }
373
382
  }
374
- catch (e) {
375
- this.debugErrorLog(`Failed to Discover Devices, Error Message: ${(0, super_stringify_1.default)(e.message)}, Submit Bugs Here: `
376
- + 'https://tinyurl.com/SwitchBotBug');
377
- this.debugErrorLog(`Failed to Discover Devices, Error: ${e}`);
383
+ else {
384
+ this.errorLog('Neither SwitchBot Token or Device Config are not set.');
378
385
  }
379
386
  }
380
387
  createDevice(device) {
@@ -532,7 +539,7 @@ class SwitchBotPlatform {
532
539
  this.createOthers(device);
533
540
  break;
534
541
  default:
535
- this.debugLog(`Unsupported Device: ${(0, super_stringify_1.default)(device)}`);
542
+ this.debugLog(`Unsupported Device: ${JSON.stringify(device)}`);
536
543
  this.warnLog(`Device: ${device.deviceName} with Device Type: ${device.remoteType}, is currently not supported.`);
537
544
  this.warnLog('Submit Feature Requests Here: ' + 'https://tinyurl.com/SwitchBotFeatureRequest');
538
545
  }
@@ -2034,35 +2041,35 @@ class SwitchBotPlatform {
2034
2041
  this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [existingAccessory]);
2035
2042
  this.warnLog(`Removing existing accessory from cache: ${existingAccessory.displayName}`);
2036
2043
  }
2037
- async statusCode({ res }) {
2038
- switch (res.statusCode) {
2044
+ async statusCode(statusCode) {
2045
+ switch (statusCode) {
2039
2046
  case 151:
2040
- this.errorLog('Command not supported by this device type, Submit Feature Request Here: ' + 'https://tinyurl.com/SwitchBotFeatureRequest');
2047
+ this.errorLog(`Command not supported by this device type, statusCode: ${statusCode}, Submit Feature Request Here: `
2048
+ + 'https://tinyurl.com/SwitchBotFeatureRequest');
2041
2049
  break;
2042
2050
  case 152:
2043
- this.errorLog('Device not found.');
2051
+ this.errorLog(`Device not found, statusCode: ${statusCode}`);
2044
2052
  break;
2045
2053
  case 160:
2046
- this.errorLog('Command is not supported, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug');
2054
+ this.errorLog(`Command is not supported, statusCode: ${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
2047
2055
  break;
2048
2056
  case 161:
2049
- this.errorLog('Device is offline.');
2057
+ this.errorLog(`Device is offline, statusCode: ${statusCode}`);
2050
2058
  break;
2051
2059
  case 171:
2052
- this.errorLog('is offline');
2060
+ this.errorLog(`is offline, statusCode: ${statusCode}`);
2053
2061
  break;
2054
2062
  case 190:
2055
- this.errorLog('Requests reached the daily limit');
2063
+ this.errorLog(`Requests reached the daily limit, statusCode: ${statusCode}`);
2056
2064
  break;
2057
2065
  case 100:
2058
- if (this.debugMode) {
2059
- this.debugLog('Command successfully sent.');
2060
- }
2066
+ this.debugLog(`Command successfully sent, statusCode: ${statusCode}`);
2067
+ break;
2068
+ case 200:
2069
+ this.debugLog(`Request successful, statusCode: ${statusCode}`);
2061
2070
  break;
2062
2071
  default:
2063
- if (this.debugMode) {
2064
- this.debugLog('Unknown statusCode, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug');
2065
- }
2072
+ this.infoLog(`Unknown statusCode, statusCode: ${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
2066
2073
  }
2067
2074
  }
2068
2075
  // BLE Connection