tsvesync 1.0.20 → 1.0.23

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.
@@ -85,23 +85,18 @@ function objectFactory(details, manager) {
85
85
  // Add category to device details
86
86
  details.deviceCategory = category;
87
87
  // Handle outdoor plug sub-devices
88
- if (deviceType === 'ESO15-TB' && details.subDevices) {
88
+ if (deviceType === 'ESO15-TB' && details.subDeviceNo) {
89
89
  const devices = [];
90
90
  // Create a device instance for each sub-device
91
- details.subDevices.forEach((subDevice) => {
92
- const subDeviceDetails = {
93
- ...details,
94
- deviceName: subDevice.subDeviceName,
95
- deviceStatus: subDevice.subDeviceStatus,
96
- subDeviceNo: subDevice.subDeviceNo,
97
- isSubDevice: true,
98
- parentCid: details.cid,
99
- // Generate a unique CID for the sub-device
100
- cid: `${details.cid}_${subDevice.subDeviceNo}`
101
- };
102
- const device = new DeviceClass(subDeviceDetails, manager);
103
- devices.push([category, device]);
104
- });
91
+ const subDeviceDetails = {
92
+ ...details,
93
+ deviceName: details.deviceName,
94
+ deviceStatus: details.deviceStatus,
95
+ subDeviceNo: details.subDeviceNo,
96
+ isSubDevice: true
97
+ };
98
+ const device = new DeviceClass(subDeviceDetails, manager);
99
+ devices.push([category, device]);
105
100
  // Return array of sub-devices
106
101
  return devices[0]; // Return first device, manager will handle adding all devices
107
102
  }
@@ -291,37 +286,45 @@ class VeSync {
291
286
  * Process devices from API response
292
287
  */
293
288
  processDevices(deviceList) {
294
- // Clear existing devices
295
- for (const category of Object.keys(this._devList)) {
296
- this._devList[category].length = 0;
297
- }
298
- // Process each device
299
- deviceList.forEach(dev => {
300
- const [category, device] = objectFactory(dev, this);
301
- // Handle outdoor plug sub-devices
302
- if (dev.deviceType === 'ESO15-TB' && dev.subDevices) {
303
- dev.subDevices.forEach((subDev) => {
289
+ try {
290
+ // Clear existing devices
291
+ for (const category of Object.keys(this._devList)) {
292
+ this._devList[category].length = 0;
293
+ }
294
+ if (!deviceList || deviceList.length === 0) {
295
+ logger_1.logger.warn('No devices found in API response');
296
+ return false;
297
+ }
298
+ // Process each device
299
+ deviceList.forEach(dev => {
300
+ const [category, device] = objectFactory(dev, this);
301
+ // Handle outdoor plug sub-devices
302
+ if (dev.deviceType === 'ESO15-TB' && dev.subDeviceNo) {
304
303
  const subDeviceDetails = {
305
304
  ...dev,
306
- deviceName: subDev.subDeviceName,
307
- deviceStatus: subDev.subDeviceStatus,
308
- subDeviceNo: subDev.subDeviceNo,
305
+ deviceName: dev.deviceName,
306
+ deviceStatus: dev.deviceStatus,
307
+ subDeviceNo: dev.subDeviceNo,
309
308
  isSubDevice: true,
310
- parentCid: dev.cid,
311
- cid: `${dev.cid}_${subDev.subDeviceNo}`
312
309
  };
313
310
  const [subCategory, subDevice] = objectFactory(subDeviceDetails, this);
314
311
  if (subDevice && subCategory in this._devList) {
315
312
  this._devList[subCategory].push(subDevice);
316
313
  }
317
- });
318
- }
319
- else if (device && category in this._devList) {
320
- this._devList[category].push(device);
321
- }
322
- });
323
- // Update device list reference
324
- this.devices = Object.values(this._devList).flat();
314
+ }
315
+ else if (device && category in this._devList) {
316
+ this._devList[category].push(device);
317
+ }
318
+ });
319
+ // Update device list reference
320
+ this.devices = Object.values(this._devList).flat();
321
+ // Return true if we processed at least one device successfully
322
+ return this.devices.length > 0;
323
+ }
324
+ catch (error) {
325
+ logger_1.logger.error('Error processing devices:', error);
326
+ return false;
327
+ }
325
328
  }
326
329
  /**
327
330
  * Get list of VeSync devices
@@ -333,12 +336,12 @@ class VeSync {
333
336
  return false;
334
337
  }
335
338
  this._inProcess = true;
336
- let procReturn = false;
339
+ let success = false;
337
340
  try {
338
341
  const [response] = await helpers_1.Helpers.callApi('/cloud/v2/deviceManaged/devices', 'post', helpers_1.Helpers.reqBody(this, 'devicelist'), helpers_1.Helpers.reqHeaders(this));
339
342
  if ((_a = response === null || response === void 0 ? void 0 : response.result) === null || _a === void 0 ? void 0 : _a.list) {
340
343
  const deviceList = response.result.list;
341
- this.processDevices(deviceList);
344
+ success = this.processDevices(deviceList);
342
345
  // Log device discovery results
343
346
  logger_1.logger.debug('\n=== Device Discovery Summary ===');
344
347
  logger_1.logger.debug(`Total devices processed: ${deviceList.length}`);
@@ -376,7 +379,7 @@ class VeSync {
376
379
  logger_1.logger.error('Unable to get device list:', error);
377
380
  }
378
381
  this._inProcess = false;
379
- return procReturn;
382
+ return success;
380
383
  }
381
384
  /**
382
385
  * Login to VeSync server
@@ -57,9 +57,8 @@ export declare class VeSyncOutlet15A extends VeSyncOutlet {
57
57
  * VeSync Outdoor Plug
58
58
  */
59
59
  export declare class VeSyncOutdoorPlug extends VeSyncOutlet {
60
- protected subDeviceNo?: number;
61
- protected isSubDevice: boolean;
62
- protected parentCid?: string;
60
+ subDeviceNo?: number;
61
+ isSubDevice: boolean;
63
62
  constructor(details: Record<string, any>, manager: VeSync);
64
63
  /**
65
64
  * Turn outlet on
@@ -194,16 +194,11 @@ class VeSyncOutdoorPlug extends vesyncOutlet_1.VeSyncOutlet {
194
194
  super(details, manager);
195
195
  this.isSubDevice = details.isSubDevice || false;
196
196
  this.subDeviceNo = details.subDeviceNo;
197
- this.parentCid = details.parentCid;
198
197
  }
199
198
  /**
200
199
  * Turn outlet on
201
200
  */
202
201
  async turnOn() {
203
- if (!this.isSubDevice) {
204
- logger_1.logger.error(`[${this.deviceName}] Cannot control parent device directly`);
205
- return false;
206
- }
207
202
  logger_1.logger.debug(`[${this.deviceName}] Turning outlet on`);
208
203
  const body = {
209
204
  ...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
@@ -224,10 +219,6 @@ class VeSyncOutdoorPlug extends vesyncOutlet_1.VeSyncOutlet {
224
219
  * Turn outlet off
225
220
  */
226
221
  async turnOff() {
227
- if (!this.isSubDevice) {
228
- logger_1.logger.error(`[${this.deviceName}] Cannot control parent device directly`);
229
- return false;
230
- }
231
222
  logger_1.logger.debug(`[${this.deviceName}] Turning outlet off`);
232
223
  const body = {
233
224
  ...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsvesync",
3
- "version": "1.0.20",
3
+ "version": "1.0.23",
4
4
  "description": "A TypeScript library for interacting with VeSync smart home devices",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/lib/vesync.ts CHANGED
@@ -8,7 +8,6 @@ import { fanModules } from './vesyncFanImpl';
8
8
  import { outletModules } from './vesyncOutletImpl';
9
9
  import { switchModules } from './vesyncSwitchImpl';
10
10
  import { bulbModules } from './vesyncBulbImpl';
11
- import { VeSyncBulb } from './vesyncBulb';
12
11
  import { logger, setLogger, Logger } from './logger';
13
12
 
14
13
  const DEFAULT_ENERGY_UPDATE_INTERVAL = 21600;
@@ -105,24 +104,20 @@ function objectFactory(details: Record<string, any>, manager: VeSync): [string,
105
104
  details.deviceCategory = category;
106
105
 
107
106
  // Handle outdoor plug sub-devices
108
- if (deviceType === 'ESO15-TB' && details.subDevices) {
107
+ if (deviceType === 'ESO15-TB' && details.subDeviceNo) {
109
108
  const devices: [string, VeSyncBaseDevice | null][] = [];
110
109
 
111
110
  // Create a device instance for each sub-device
112
- details.subDevices.forEach((subDevice: any) => {
113
- const subDeviceDetails = {
114
- ...details,
115
- deviceName: subDevice.subDeviceName,
116
- deviceStatus: subDevice.subDeviceStatus,
117
- subDeviceNo: subDevice.subDeviceNo,
118
- isSubDevice: true,
119
- parentCid: details.cid,
120
- // Generate a unique CID for the sub-device
121
- cid: `${details.cid}_${subDevice.subDeviceNo}`
122
- };
123
- const device = new DeviceClass(subDeviceDetails, manager);
124
- devices.push([category, device]);
125
- });
111
+
112
+ const subDeviceDetails = {
113
+ ...details,
114
+ deviceName: details.deviceName,
115
+ deviceStatus: details.deviceStatus,
116
+ subDeviceNo: details.subDeviceNo,
117
+ isSubDevice: true
118
+ };
119
+ const device = new DeviceClass(subDeviceDetails, manager);
120
+ devices.push([category, device]);
126
121
 
127
122
  // Return array of sub-devices
128
123
  return devices[0]; // Return first device, manager will handle adding all devices
@@ -358,40 +353,48 @@ export class VeSync {
358
353
  /**
359
354
  * Process devices from API response
360
355
  */
361
- private processDevices(deviceList: any[]): void {
362
- // Clear existing devices
363
- for (const category of Object.keys(this._devList)) {
364
- this._devList[category].length = 0;
365
- }
356
+ private processDevices(deviceList: any[]): boolean {
357
+ try {
358
+ // Clear existing devices
359
+ for (const category of Object.keys(this._devList)) {
360
+ this._devList[category].length = 0;
361
+ }
366
362
 
367
- // Process each device
368
- deviceList.forEach(dev => {
369
- const [category, device] = objectFactory(dev, this);
370
-
371
- // Handle outdoor plug sub-devices
372
- if (dev.deviceType === 'ESO15-TB' && dev.subDevices) {
373
- dev.subDevices.forEach((subDev: any) => {
363
+ if (!deviceList || deviceList.length === 0) {
364
+ logger.warn('No devices found in API response');
365
+ return false;
366
+ }
367
+
368
+ // Process each device
369
+ deviceList.forEach(dev => {
370
+ const [category, device] = objectFactory(dev, this);
371
+ // Handle outdoor plug sub-devices
372
+ if (dev.deviceType === 'ESO15-TB' && dev.subDeviceNo) {
374
373
  const subDeviceDetails = {
375
374
  ...dev,
376
- deviceName: subDev.subDeviceName,
377
- deviceStatus: subDev.subDeviceStatus,
378
- subDeviceNo: subDev.subDeviceNo,
375
+ deviceName: dev.deviceName,
376
+ deviceStatus: dev.deviceStatus,
377
+ subDeviceNo: dev.subDeviceNo,
379
378
  isSubDevice: true,
380
- parentCid: dev.cid,
381
- cid: `${dev.cid}_${subDev.subDeviceNo}`
382
379
  };
383
380
  const [subCategory, subDevice] = objectFactory(subDeviceDetails, this);
384
381
  if (subDevice && subCategory in this._devList) {
385
382
  this._devList[subCategory].push(subDevice);
386
383
  }
387
- });
388
- } else if (device && category in this._devList) {
389
- this._devList[category].push(device);
390
- }
391
- });
384
+ } else if (device && category in this._devList) {
385
+ this._devList[category].push(device);
386
+ }
387
+ });
392
388
 
393
- // Update device list reference
394
- this.devices = Object.values(this._devList).flat();
389
+ // Update device list reference
390
+ this.devices = Object.values(this._devList).flat();
391
+
392
+ // Return true if we processed at least one device successfully
393
+ return this.devices.length > 0;
394
+ } catch (error) {
395
+ logger.error('Error processing devices:', error);
396
+ return false;
397
+ }
395
398
  }
396
399
 
397
400
  /**
@@ -404,7 +407,7 @@ export class VeSync {
404
407
  }
405
408
 
406
409
  this._inProcess = true;
407
- let procReturn = false;
410
+ let success = false;
408
411
 
409
412
  try {
410
413
  const [response] = await Helpers.callApi(
@@ -416,7 +419,7 @@ export class VeSync {
416
419
 
417
420
  if (response?.result?.list) {
418
421
  const deviceList = response.result.list;
419
- this.processDevices(deviceList);
422
+ success = this.processDevices(deviceList);
420
423
 
421
424
  // Log device discovery results
422
425
  logger.debug('\n=== Device Discovery Summary ===');
@@ -458,7 +461,7 @@ export class VeSync {
458
461
  }
459
462
 
460
463
  this._inProcess = false;
461
- return procReturn;
464
+ return success;
462
465
  }
463
466
 
464
467
  /**
@@ -254,25 +254,19 @@ export class VeSyncOutlet15A extends VeSyncOutlet {
254
254
  * VeSync Outdoor Plug
255
255
  */
256
256
  export class VeSyncOutdoorPlug extends VeSyncOutlet {
257
- protected subDeviceNo?: number;
258
- protected isSubDevice: boolean;
259
- protected parentCid?: string;
257
+ public subDeviceNo?: number;
258
+ public isSubDevice: boolean;
260
259
 
261
260
  constructor(details: Record<string, any>, manager: VeSync) {
262
261
  super(details, manager);
263
262
  this.isSubDevice = details.isSubDevice || false;
264
263
  this.subDeviceNo = details.subDeviceNo;
265
- this.parentCid = details.parentCid;
266
264
  }
267
265
 
268
266
  /**
269
267
  * Turn outlet on
270
268
  */
271
269
  async turnOn(): Promise<boolean> {
272
- if (!this.isSubDevice) {
273
- logger.error(`[${this.deviceName}] Cannot control parent device directly`);
274
- return false;
275
- }
276
270
 
277
271
  logger.debug(`[${this.deviceName}] Turning outlet on`);
278
272
  const body = {
@@ -302,10 +296,6 @@ export class VeSyncOutdoorPlug extends VeSyncOutlet {
302
296
  * Turn outlet off
303
297
  */
304
298
  async turnOff(): Promise<boolean> {
305
- if (!this.isSubDevice) {
306
- logger.error(`[${this.deviceName}] Cannot control parent device directly`);
307
- return false;
308
- }
309
299
 
310
300
  logger.debug(`[${this.deviceName}] Turning outlet off`);
311
301
  const body = {