@switchbot/homebridge-switchbot 2.8.1-beta.1 → 2.8.1-beta.11

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.
package/src/platform.ts CHANGED
@@ -300,12 +300,18 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
300
300
  const { body, statusCode, headers } = await request(Devices, {
301
301
  headers: this.generateHeaders(),
302
302
  });
303
- if (statusCode === 200) {
304
- const devicesAPI: any = await body.json();
303
+ this.debugWarnLog(`body: ${JSON.stringify(body)}`);
304
+ this.debugWarnLog(`statusCode: ${JSON.stringify(statusCode)}`);
305
+ this.debugWarnLog(`headers: ${JSON.stringify(headers)}`);
306
+ const devicesAPI: any = await body.json();
307
+ if ((statusCode === 200 || statusCode === 100) && devicesAPI.statusCode === 200) {
305
308
  this.debugLog(`Devices: ${JSON.stringify(devicesAPI.body)}`);
306
- this.debugLog(`Headers: ${JSON.stringify(headers)}`);
307
309
  // SwitchBot Devices
310
+ this.debugWarnLog(`devicesAPI: ${JSON.stringify(devicesAPI)}`);
311
+ this.debugWarnLog(`devicesAPI Length: ${devicesAPI.length}`);
308
312
  const deviceLists = devicesAPI.body.deviceList;
313
+ this.debugWarnLog(`DeviceLists: ${JSON.stringify(deviceLists)}`);
314
+ this.debugWarnLog(`DeviceLists Length: ${deviceLists.length}`);
309
315
  if (!this.config.options?.devices) {
310
316
  this.debugLog(`SwitchBot Device Config Not Set: ${JSON.stringify(this.config.options?.devices)}`);
311
317
  if (deviceLists.length === 0) {
@@ -323,25 +329,29 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
323
329
  }
324
330
  } else if (this.config.credentials?.token && this.config.options.devices) {
325
331
  this.debugLog(`SwitchBot Device Config Set: ${JSON.stringify(this.config.options?.devices)}`);
326
- const deviceConfigs = this.config.options?.devices;
327
-
328
- const mergeBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
329
- a1.map((itm: { deviceId: string }) => ({
330
- ...a2.find(
331
- (item: { deviceId: string }) =>
332
- item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item,
333
- ),
334
- ...itm,
335
- }));
336
-
337
- const devices = mergeBydeviceId(deviceLists, deviceConfigs);
338
- this.debugLog(`SwitchBot Devices: ${JSON.stringify(devices)}`);
339
- for (const device of devices) {
340
- if (device.deviceType) {
341
- if (device.configDeviceName) {
342
- device.deviceName = device.configDeviceName;
332
+ if (deviceLists.length === 0) {
333
+ this.debugLog(`SwitchBot API Currently Doesn't Have Any Devices With Cloud Services Enabled: ${JSON.stringify(devicesAPI.body)}`);
334
+ } else {
335
+ const deviceConfigs = this.config.options?.devices;
336
+
337
+ const mergeBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
338
+ a1.map((itm: { deviceId: string }) => ({
339
+ ...a2.find(
340
+ (item: { deviceId: string }) =>
341
+ item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item,
342
+ ),
343
+ ...itm,
344
+ }));
345
+
346
+ const devices = mergeBydeviceId(deviceLists, deviceConfigs);
347
+ this.debugLog(`SwitchBot Devices: ${JSON.stringify(devices)}`);
348
+ for (const device of devices) {
349
+ if (device.deviceType) {
350
+ if (device.configDeviceName) {
351
+ device.deviceName = device.configDeviceName;
352
+ }
353
+ this.createDevice(device);
343
354
  }
344
- this.createDevice(device);
345
355
  }
346
356
  }
347
357
  } else {
@@ -388,7 +398,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
388
398
  this.debugLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
389
399
  }
390
400
  } else {
391
- this.statusCode(statusCode);
401
+ this.statusCode(devicesAPI.statusCode);
392
402
  }
393
403
  } catch (e: any) {
394
404
  this.debugErrorLog(
@@ -2184,7 +2194,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
2184
2194
  this.debugLog(`Request successful, statusCode: ${statusCode}`);
2185
2195
  break;
2186
2196
  default:
2187
- this.infoLog(`Unknown statusCode, statusCode: ${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
2197
+ this.errorLog(`Unknown statusCode, statusCode: ${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
2188
2198
  }
2189
2199
  }
2190
2200