tsvesync 1.0.13 → 1.0.14

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 (39) hide show
  1. package/package.json +1 -1
  2. package/dist/lib/helpers.js.map +0 -1
  3. package/dist/lib/vesync.js.map +0 -1
  4. package/dist/lib/vesyncBaseDevice.js.map +0 -1
  5. package/dist/lib/vesyncBulb.js.map +0 -1
  6. package/dist/lib/vesyncFan.js.map +0 -1
  7. package/dist/lib/vesyncKitchen.d.ts +0 -0
  8. package/dist/lib/vesyncKitchen.js +0 -2
  9. package/dist/lib/vesyncKitchen.js.map +0 -1
  10. package/dist/lib/vesyncOutlet.js.map +0 -1
  11. package/dist/lib/vesyncSwitch.js.map +0 -1
  12. package/dist/src/lib/helpers.d.ts +0 -88
  13. package/dist/src/lib/helpers.js +0 -244
  14. package/dist/src/lib/vesync.d.ts +0 -89
  15. package/dist/src/lib/vesync.js +0 -430
  16. package/dist/src/lib/vesyncBaseDevice.d.ts +0 -43
  17. package/dist/src/lib/vesyncBaseDevice.js +0 -87
  18. package/dist/src/lib/vesyncBulb.d.ts +0 -79
  19. package/dist/src/lib/vesyncBulb.js +0 -198
  20. package/dist/src/lib/vesyncBulbImpl.d.ts +0 -38
  21. package/dist/src/lib/vesyncBulbImpl.js +0 -161
  22. package/dist/src/lib/vesyncFan.d.ts +0 -80
  23. package/dist/src/lib/vesyncFan.js +0 -406
  24. package/dist/src/lib/vesyncFanImpl.d.ts +0 -137
  25. package/dist/src/lib/vesyncFanImpl.js +0 -643
  26. package/dist/src/lib/vesyncOutlet.d.ts +0 -53
  27. package/dist/src/lib/vesyncOutlet.js +0 -98
  28. package/dist/src/lib/vesyncOutletImpl.d.ts +0 -70
  29. package/dist/src/lib/vesyncOutletImpl.js +0 -206
  30. package/dist/src/lib/vesyncSwitch.d.ts +0 -169
  31. package/dist/src/lib/vesyncSwitch.js +0 -414
  32. package/dist/src/lib/vesyncSwitchImpl.d.ts +0 -82
  33. package/dist/src/lib/vesyncSwitchImpl.js +0 -290
  34. package/dist/test/testFeatures.d.ts +0 -4
  35. package/dist/test/testFeatures.js +0 -394
  36. package/dist/test/testService.d.ts +0 -62
  37. package/dist/test/testService.js +0 -540
  38. package/dist/test/testSpecial.d.ts +0 -5
  39. package/dist/test/testSpecial.js +0 -227
@@ -1,430 +0,0 @@
1
- "use strict";
2
- /**
3
- * VeSync API Device Library
4
- */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.VeSync = void 0;
7
- const helpers_1 = require("./helpers");
8
- const vesyncFanImpl_1 = require("./vesyncFanImpl");
9
- const vesyncOutletImpl_1 = require("./vesyncOutletImpl");
10
- const vesyncSwitchImpl_1 = require("./vesyncSwitchImpl");
11
- const vesyncBulbImpl_1 = require("./vesyncBulbImpl");
12
- const DEFAULT_ENERGY_UPDATE_INTERVAL = 21600;
13
- /**
14
- * Create device instance based on type
15
- */
16
- function objectFactory(details, manager) {
17
- const deviceType = details.deviceType;
18
- let DeviceClass = null;
19
- let category = 'unknown';
20
- // Map of device categories to their module dictionaries
21
- const allModules = {
22
- outlets: vesyncOutletImpl_1.outletModules,
23
- fans: vesyncFanImpl_1.fanModules,
24
- bulbs: vesyncBulbImpl_1.bulbModules,
25
- switches: vesyncSwitchImpl_1.switchModules
26
- };
27
- // First try exact match in all modules
28
- for (const [cat, modules] of Object.entries(allModules)) {
29
- if (deviceType in modules) {
30
- DeviceClass = modules[deviceType];
31
- category = cat;
32
- console.debug(`Found exact match for ${deviceType} in ${cat} modules`);
33
- break;
34
- }
35
- }
36
- // If no exact match, try to find a base class
37
- if (!DeviceClass) {
38
- // Device type prefix mapping
39
- const prefixMap = {
40
- // Fans
41
- 'Core': 'fans',
42
- 'LAP': 'fans',
43
- 'LTF': 'fans',
44
- 'Classic': 'fans',
45
- 'Dual': 'fans',
46
- 'LUH': 'fans',
47
- 'LEH': 'fans',
48
- 'LV-PUR': 'fans',
49
- 'LV-RH': 'fans',
50
- // Outlets
51
- 'wifi-switch': 'outlets',
52
- 'ESW03': 'outlets',
53
- 'ESW01': 'outlets',
54
- 'ESW15': 'outlets',
55
- 'ESO': 'outlets',
56
- // Switches
57
- 'ESWL': 'switches',
58
- 'ESWD': 'switches',
59
- // Bulbs
60
- 'ESL': 'bulbs',
61
- 'XYD': 'bulbs'
62
- };
63
- // Find category based on device type prefix
64
- for (const [prefix, cat] of Object.entries(prefixMap)) {
65
- if (deviceType.startsWith(prefix)) {
66
- category = cat;
67
- console.debug(`Device type ${deviceType} matched prefix ${prefix} -> category ${cat}`);
68
- // Try to find a base class in this category's modules
69
- const modules = allModules[cat];
70
- for (const [moduleType, ModuleClass] of Object.entries(modules)) {
71
- const baseType = moduleType.split('-')[0]; // e.g., ESL100 from ESL100-USA
72
- if (deviceType.startsWith(baseType)) {
73
- DeviceClass = ModuleClass;
74
- console.debug(`Found base type match: ${deviceType} -> ${baseType}`);
75
- break;
76
- }
77
- }
78
- break;
79
- }
80
- }
81
- }
82
- if (DeviceClass) {
83
- try {
84
- // Add category to device details
85
- details.deviceCategory = category;
86
- const device = new DeviceClass(details, manager);
87
- return [category, device];
88
- }
89
- catch (error) {
90
- console.error(`Error creating device instance for ${deviceType}:`, error);
91
- return [category, null];
92
- }
93
- }
94
- else {
95
- console.warn(`No device class found for type: ${deviceType}`);
96
- return [category, null];
97
- }
98
- }
99
- /**
100
- * VeSync Manager Class
101
- */
102
- class VeSync {
103
- /**
104
- * Initialize VeSync Manager
105
- */
106
- constructor(username, password, timeZone = helpers_1.DEFAULT_TZ, debug = false, redact = true) {
107
- this._debug = debug;
108
- this._redact = redact;
109
- this._energyUpdateInterval = DEFAULT_ENERGY_UPDATE_INTERVAL;
110
- this._energyCheck = true;
111
- this._lastUpdateTs = null;
112
- this._inProcess = false;
113
- this.username = username;
114
- this.password = password;
115
- this.token = null;
116
- this.accountId = null;
117
- this.countryCode = null;
118
- this.devices = null;
119
- this.enabled = false;
120
- this.updateInterval = helpers_1.API_RATE_LIMIT;
121
- this.fans = [];
122
- this.outlets = [];
123
- this.switches = [];
124
- this.bulbs = [];
125
- this.scales = [];
126
- this._devList = {
127
- fans: this.fans,
128
- outlets: this.outlets,
129
- switches: this.switches,
130
- bulbs: this.bulbs
131
- };
132
- if (typeof timeZone === 'string' && timeZone) {
133
- const regTest = /[^a-zA-Z/_]/;
134
- if (regTest.test(timeZone)) {
135
- this.timeZone = helpers_1.DEFAULT_TZ;
136
- console.debug('Invalid characters in time zone - ', timeZone);
137
- }
138
- else {
139
- this.timeZone = timeZone;
140
- }
141
- }
142
- else {
143
- this.timeZone = helpers_1.DEFAULT_TZ;
144
- console.debug('Time zone is not a string');
145
- }
146
- if (debug) {
147
- this.debug = true;
148
- }
149
- if (redact) {
150
- this.redact = true;
151
- }
152
- }
153
- /**
154
- * Get/Set debug mode
155
- */
156
- get debug() {
157
- return this._debug;
158
- }
159
- set debug(flag) {
160
- this._debug = flag;
161
- }
162
- /**
163
- * Get/Set redact mode
164
- */
165
- get redact() {
166
- return this._redact;
167
- }
168
- set redact(flag) {
169
- this._redact = flag;
170
- helpers_1.Helpers.shouldRedact = flag;
171
- }
172
- /**
173
- * Get/Set energy update interval
174
- */
175
- get energyUpdateInterval() {
176
- return this._energyUpdateInterval;
177
- }
178
- set energyUpdateInterval(interval) {
179
- if (interval > 0) {
180
- this._energyUpdateInterval = interval;
181
- }
182
- }
183
- /**
184
- * Test if device should be removed
185
- */
186
- static removeDevTest(device, newList) {
187
- if (Array.isArray(newList) && device.cid) {
188
- for (const item of newList) {
189
- if ('cid' in item && device.cid === item.cid) {
190
- return true;
191
- }
192
- }
193
- console.debug(`Device removed - ${device.deviceName} - ${device.deviceType}`);
194
- return false;
195
- }
196
- return true;
197
- }
198
- /**
199
- * Test if new device should be added
200
- */
201
- addDevTest(newDev) {
202
- if ('cid' in newDev) {
203
- for (const devices of Object.values(this._devList)) {
204
- for (const dev of devices) {
205
- if (dev.cid === newDev.cid) {
206
- return false;
207
- }
208
- }
209
- }
210
- }
211
- return true;
212
- }
213
- /**
214
- * Remove devices not found in device list return
215
- */
216
- removeOldDevices(devices) {
217
- for (const [key, deviceList] of Object.entries(this._devList)) {
218
- const before = deviceList.length;
219
- this._devList[key] = deviceList.filter(device => VeSync.removeDevTest(device, devices));
220
- const after = this._devList[key].length;
221
- if (before !== after) {
222
- console.debug(`${before - after} ${key} removed`);
223
- }
224
- }
225
- return true;
226
- }
227
- /**
228
- * Correct devices without cid or uuid
229
- */
230
- static setDevId(devices) {
231
- const devRem = [];
232
- devices.forEach((dev, index) => {
233
- if (!dev.cid) {
234
- if (dev.macID) {
235
- dev.cid = dev.macID;
236
- }
237
- else if (dev.uuid) {
238
- dev.cid = dev.uuid;
239
- }
240
- else {
241
- devRem.push(index);
242
- console.warn(`Device with no ID - ${dev.deviceName || ''}`);
243
- }
244
- }
245
- });
246
- if (devRem.length > 0) {
247
- return devices.filter((_, index) => !devRem.includes(index));
248
- }
249
- return devices;
250
- }
251
- /**
252
- * Process device list and instantiate device objects
253
- */
254
- processDevices(devList) {
255
- // Ensure devices have proper IDs
256
- const devices = VeSync.setDevId(devList);
257
- // Count current devices
258
- let numDevices = 0;
259
- for (const deviceList of Object.values(this._devList)) {
260
- numDevices += deviceList.length;
261
- }
262
- if (!devices || devices.length === 0) {
263
- console.warn('No devices found in api return');
264
- return false;
265
- }
266
- // Initialize new device list or remove old devices
267
- if (numDevices === 0) {
268
- console.debug('New device list initialized');
269
- }
270
- else {
271
- this.removeOldDevices(devices);
272
- }
273
- // Filter out devices that already exist
274
- const newDevices = devices.filter(dev => this.addDevTest(dev));
275
- // Required keys for device instantiation
276
- const requiredKeys = ['deviceType', 'deviceName', 'deviceStatus', 'cid'];
277
- // Process each new device
278
- for (const dev of newDevices) {
279
- // Verify required device properties
280
- if (!requiredKeys.every(key => key in dev)) {
281
- console.debug(`Error adding device - missing required properties: ${dev.deviceName || 'Unknown'}`);
282
- continue;
283
- }
284
- const devType = dev.deviceType;
285
- try {
286
- // Create device instance using factory
287
- const [category, deviceObj] = objectFactory(dev, this);
288
- // Add device to appropriate category if valid
289
- if (deviceObj && category in this._devList) {
290
- this._devList[category].push(deviceObj);
291
- console.debug(`Added ${devType} device to ${category} category`);
292
- }
293
- else if (category === 'unknown') {
294
- console.debug(`Unknown device type: ${devType}`);
295
- }
296
- }
297
- catch (err) {
298
- console.error(`Error creating device ${devType}:`, err);
299
- continue;
300
- }
301
- }
302
- return true;
303
- }
304
- /**
305
- * Get list of VeSync devices
306
- */
307
- async getDevices() {
308
- if (!this.enabled) {
309
- console.error('Not logged in to VeSync');
310
- return false;
311
- }
312
- this._inProcess = true;
313
- let procReturn = false;
314
- try {
315
- const [response] = await helpers_1.Helpers.callApi('/cloud/v2/deviceManaged/devices', 'post', helpers_1.Helpers.reqBody(this, 'devicelist'), helpers_1.Helpers.reqHeaders(this));
316
- if (response?.result?.list) {
317
- const deviceList = response.result.list;
318
- procReturn = this.processDevices(deviceList);
319
- // Update the main devices array with all devices
320
- this.devices = Object.values(this._devList).flat();
321
- // Log device discovery results
322
- console.log('\n=== Device Discovery Summary ===');
323
- console.log(`Total devices processed: ${deviceList.length}`);
324
- // Log device types found
325
- const deviceTypes = deviceList.map((d) => d.deviceType);
326
- console.log('\nDevice types found:', deviceTypes);
327
- // Log devices by category with details
328
- console.log('\nDevices by Category:');
329
- console.log('---------------------');
330
- for (const [category, devices] of Object.entries(this._devList)) {
331
- if (devices.length > 0) {
332
- console.log(`\n${category.toUpperCase()} (${devices.length} devices):`);
333
- devices.forEach((d) => {
334
- console.log(` • ${d.deviceName}`);
335
- console.log(` Type: ${d.deviceType}`);
336
- console.log(` Status: ${d.deviceStatus}`);
337
- console.log(` ID: ${d.cid}`);
338
- });
339
- }
340
- }
341
- // Log summary statistics
342
- console.log('\nSummary Statistics:');
343
- console.log('-------------------');
344
- console.log(`Total Devices: ${this.devices?.length || 0}`);
345
- for (const [category, devices] of Object.entries(this._devList)) {
346
- console.log(`${category}: ${devices.length} devices`);
347
- }
348
- console.log('\n=== End of Device Discovery ===\n');
349
- }
350
- else {
351
- console.error('No devices found in response');
352
- }
353
- }
354
- catch (error) {
355
- console.error('Unable to get device list:', error);
356
- }
357
- this._inProcess = false;
358
- return procReturn;
359
- }
360
- /**
361
- * Login to VeSync server
362
- */
363
- async login() {
364
- const body = helpers_1.Helpers.reqBody(this, 'login');
365
- console.log('Login request:', {
366
- url: `${process.env.VESYNC_API_URL}/cloud/v1/user/login`,
367
- body
368
- });
369
- try {
370
- const [response, status] = await helpers_1.Helpers.callApi('/cloud/v1/user/login', 'post', body);
371
- console.log('Login response:', { status, response });
372
- if (response?.result?.token) {
373
- this.token = response.result.token;
374
- this.accountId = response.result.accountID;
375
- this.countryCode = response.result.countryCode;
376
- this.enabled = true;
377
- return true;
378
- }
379
- console.error('Unable to login with supplied credentials');
380
- return false;
381
- }
382
- catch (error) {
383
- console.error('Login error:', error);
384
- return false;
385
- }
386
- }
387
- /**
388
- * Test if update interval has been exceeded
389
- */
390
- deviceTimeCheck() {
391
- return (this._lastUpdateTs === null ||
392
- (Date.now() - this._lastUpdateTs) / 1000 > this.updateInterval);
393
- }
394
- /**
395
- * Update device list and details
396
- */
397
- async update() {
398
- if (this.deviceTimeCheck()) {
399
- if (!this.enabled) {
400
- console.error('Not logged in to VeSync');
401
- return;
402
- }
403
- await this.getDevices();
404
- console.debug('Start updating the device details one by one');
405
- for (const deviceList of Object.values(this._devList)) {
406
- for (const device of deviceList) {
407
- try {
408
- await device.getDetails();
409
- }
410
- catch (error) {
411
- console.error(`Error updating ${device.deviceName}:`, error);
412
- }
413
- }
414
- }
415
- this._lastUpdateTs = Date.now();
416
- }
417
- }
418
- /**
419
- * Create device instance from details
420
- */
421
- createDevice(details) {
422
- const deviceType = details.deviceType;
423
- const deviceClass = vesyncFanImpl_1.fanModules[deviceType];
424
- if (deviceClass) {
425
- return new deviceClass(details, this);
426
- }
427
- return null;
428
- }
429
- }
430
- exports.VeSync = VeSync;
@@ -1,43 +0,0 @@
1
- /**
2
- * Base class for VeSync devices
3
- */
4
- import { VeSync } from './vesync';
5
- export declare abstract class VeSyncBaseDevice {
6
- protected manager: VeSync;
7
- protected config: Record<string, any>;
8
- cid: string;
9
- deviceName: string;
10
- deviceStatus: string;
11
- deviceType: string;
12
- deviceRegion: string;
13
- uuid: string;
14
- configModule: string;
15
- macId: string;
16
- deviceCategory: string;
17
- connectionStatus: string;
18
- constructor(details: Record<string, any>, manager: VeSync);
19
- /**
20
- * Return formatted device info to stdout
21
- */
22
- display(): void;
23
- /**
24
- * Return JSON details for device
25
- */
26
- displayJSON(): string;
27
- /**
28
- * Check API response for errors
29
- */
30
- protected checkResponse(response: [any, number], method: string): boolean;
31
- /**
32
- * Log error with context
33
- */
34
- protected logError(method: string, error: any): void;
35
- /**
36
- * Get device details
37
- */
38
- abstract getDetails(): Promise<void>;
39
- /**
40
- * Update device details
41
- */
42
- update(): Promise<void>;
43
- }
@@ -1,87 +0,0 @@
1
- "use strict";
2
- /**
3
- * Base class for VeSync devices
4
- */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.VeSyncBaseDevice = void 0;
7
- class VeSyncBaseDevice {
8
- constructor(details, manager) {
9
- this.config = {};
10
- this.manager = manager;
11
- this.cid = details.cid || '';
12
- this.deviceName = details.deviceName || '';
13
- this.deviceStatus = details.deviceStatus || 'off';
14
- this.deviceType = details.deviceType || '';
15
- this.deviceRegion = details.deviceRegion || '';
16
- this.uuid = details.uuid || '';
17
- this.configModule = details.configModule || '';
18
- this.macId = details.macId || '';
19
- this.deviceCategory = details.deviceCategory || '';
20
- this.connectionStatus = details.connectionStatus || 'offline';
21
- }
22
- /**
23
- * Return formatted device info to stdout
24
- */
25
- display() {
26
- console.log(`Device Name: ${this.deviceName}`);
27
- console.log(`Status: ${this.deviceStatus}`);
28
- console.log(`Device Type: ${this.deviceType}`);
29
- console.log(`Connection: ${this.connectionStatus}`);
30
- console.log(`Version: ${this.config.current_firmware_version || 'Unknown'}`);
31
- }
32
- /**
33
- * Return JSON details for device
34
- */
35
- displayJSON() {
36
- return JSON.stringify({
37
- 'Device Name': this.deviceName,
38
- 'Status': this.deviceStatus,
39
- 'Device Type': this.deviceType,
40
- 'Connection': this.connectionStatus,
41
- 'Version': this.config.current_firmware_version || 'Unknown'
42
- }, null, 4);
43
- }
44
- /**
45
- * Check API response for errors
46
- */
47
- checkResponse(response, method) {
48
- const [data, status] = response;
49
- // Check HTTP status
50
- if (status !== 200) {
51
- this.logError(method, `Invalid HTTP status: ${status}`);
52
- return false;
53
- }
54
- // Check API response
55
- if (!data) {
56
- this.logError(method, 'No response data');
57
- return false;
58
- }
59
- // Check error code - code 0 indicates success
60
- if (data.code === 0) {
61
- return true;
62
- }
63
- const msg = data.msg || 'Unknown error';
64
- this.logError(method, `Error code ${data.code}: ${msg}`);
65
- return false;
66
- }
67
- /**
68
- * Log error with context
69
- */
70
- logError(method, error) {
71
- const errorMsg = error?.message || error;
72
- console.error(`[${this.deviceName}] ${method}: ${errorMsg}`);
73
- // Log additional error details if available
74
- if (error?.response?.data) {
75
- const responseData = error.response.data;
76
- const msg = responseData.msg || responseData.message || JSON.stringify(responseData);
77
- console.error(`Response: ${msg}`);
78
- }
79
- }
80
- /**
81
- * Update device details
82
- */
83
- async update() {
84
- await this.getDetails();
85
- }
86
- }
87
- exports.VeSyncBaseDevice = VeSyncBaseDevice;
@@ -1,79 +0,0 @@
1
- /**
2
- * VeSync Bulb Base Class
3
- */
4
- import { VeSyncBaseDevice } from './vesyncBaseDevice';
5
- import { VeSync } from './vesync';
6
- /**
7
- * VeSync Bulb Base Class
8
- */
9
- export declare abstract class VeSyncBulb extends VeSyncBaseDevice {
10
- protected brightness: number;
11
- protected colorTemp: number;
12
- protected colorValue: number;
13
- protected colorHue: number;
14
- protected colorSaturation: number;
15
- protected colorMode: string;
16
- protected features: string[];
17
- protected rgbValues: {
18
- red: number;
19
- green: number;
20
- blue: number;
21
- };
22
- constructor(details: Record<string, any>, manager: VeSync);
23
- /**
24
- * Get bulb details
25
- */
26
- getDetails(): Promise<void>;
27
- /**
28
- * Update bulb status
29
- */
30
- update(): Promise<void>;
31
- /**
32
- * Turn bulb on
33
- */
34
- turnOn(): Promise<boolean>;
35
- /**
36
- * Turn bulb off
37
- */
38
- turnOff(): Promise<boolean>;
39
- /**
40
- * Set bulb brightness
41
- */
42
- setBrightness(brightness: number): Promise<boolean>;
43
- /**
44
- * Get bulb brightness
45
- */
46
- getBrightness(): number;
47
- /**
48
- * Get color temperature in Kelvin
49
- */
50
- getColorTempKelvin(): number;
51
- /**
52
- * Get color temperature in percent
53
- */
54
- getColorTempPercent(): number;
55
- /**
56
- * Get color hue
57
- */
58
- getColorHue(): number;
59
- /**
60
- * Get color saturation
61
- */
62
- getColorSaturation(): number;
63
- /**
64
- * Get color value
65
- */
66
- getColorValue(): number;
67
- /**
68
- * Get RGB values
69
- */
70
- getRGBValues(): {
71
- red: number;
72
- green: number;
73
- blue: number;
74
- };
75
- /**
76
- * Set color temperature - Abstract method to be implemented by subclasses
77
- */
78
- abstract setColorTemp(colorTemp: number): Promise<boolean>;
79
- }