tsvesync 1.0.73 → 1.0.75

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.
@@ -0,0 +1,533 @@
1
+ import { VeSyncFan } from '../vesyncFan';
2
+ import { VeSync } from '../vesync';
3
+ import { Helpers } from '../helpers';
4
+ import { logger } from '../logger';
5
+
6
+ /**
7
+ * VeSync Air Purifier 131 Series (LV-PUR131S, LV-RH131S)
8
+ * This class implements the specific API for LV series devices
9
+ */
10
+ export class VeSyncAir131 extends VeSyncFan {
11
+ protected readonly modes = ['auto', 'manual', 'sleep'] as const;
12
+ protected readonly displayModes = ['on', 'off'] as const;
13
+ protected readonly childLockModes = ['on', 'off'] as const;
14
+
15
+ constructor(details: Record<string, any>, manager: VeSync) {
16
+ super(details, manager);
17
+ logger.debug(`Initialized VeSyncAir131 device: ${this.deviceName}`);
18
+ }
19
+
20
+ /**
21
+ * Get device details
22
+ */
23
+ async getDetails(): Promise<Boolean> {
24
+ logger.debug(`Getting details for device: ${this.deviceName}`);
25
+
26
+ const [response, status] = await this.callApi(
27
+ '/131airPurifier/v1/device/devicedetail',
28
+ 'post',
29
+ {
30
+ acceptLanguage: 'en',
31
+ accountID: this.manager.accountId!,
32
+ appVersion: '2.8.6',
33
+ method: 'devicedetail',
34
+ mobileId: '1234567890123456',
35
+ phoneBrand: 'SM N9005',
36
+ phoneOS: 'Android',
37
+ timeZone: this.manager.timeZone!,
38
+ token: this.manager.token!,
39
+ uuid: this.uuid
40
+ },
41
+ {
42
+ 'accept-language': 'en',
43
+ 'accountId': this.manager.accountId!,
44
+ 'appVersion': '2.8.6',
45
+ 'content-type': 'application/json',
46
+ 'tk': this.manager.token!,
47
+ 'tz': this.manager.timeZone!
48
+ }
49
+ );
50
+
51
+ const success = this.checkResponse([response, status], 'getDetails');
52
+ if (success && response) {
53
+ this.deviceStatus = response.result?.result?.deviceStatus === 'on' ? 'on' : 'off';
54
+ this.details = {
55
+ mode: response.result?.result?.mode || '',
56
+ speed: response.result?.result?.level || 0,
57
+ filterLife: response.result?.result?.filterLife || 0,
58
+ screenStatus: response.result?.result?.display ? 'on' : 'off',
59
+ childLock: response.result?.result?.childLock || false,
60
+ airQuality: response.result?.result?.airQuality || 0,
61
+ active_time: response.result?.result?.activeTime || 0
62
+ };
63
+ return true;
64
+ }
65
+ return false;
66
+ }
67
+
68
+ /**
69
+ * Turn device on
70
+ */
71
+ async turnOn(): Promise<boolean> {
72
+ logger.info(`Turning on device: ${this.deviceName}`);
73
+
74
+ const [response, status] = await this.callApi(
75
+ '/131airPurifier/v1/device/deviceStatus',
76
+ 'put',
77
+ {
78
+ acceptLanguage: 'en',
79
+ accountID: this.manager.accountId!,
80
+ status: 'on',
81
+ timeZone: this.manager.timeZone!,
82
+ token: this.manager.token!,
83
+ uuid: this.uuid
84
+ },
85
+ {
86
+ 'accept-language': 'en',
87
+ 'accountId': this.manager.accountId!,
88
+ 'appVersion': '2.8.6',
89
+ 'content-type': 'application/json',
90
+ 'tk': this.manager.token!,
91
+ 'tz': this.manager.timeZone!
92
+ }
93
+ );
94
+
95
+ const success = this.checkResponse([response, status], 'turnOn');
96
+ if (!success) {
97
+ logger.error(`Failed to turn on device: ${this.deviceName}`);
98
+ }
99
+ return success;
100
+ }
101
+
102
+ /**
103
+ * Turn device off
104
+ */
105
+ async turnOff(): Promise<boolean> {
106
+ logger.info(`Turning off device: ${this.deviceName}`);
107
+
108
+ const [response, status] = await this.callApi(
109
+ '/131airPurifier/v1/device/deviceStatus',
110
+ 'put',
111
+ {
112
+ acceptLanguage: 'en',
113
+ accountID: this.manager.accountId!,
114
+ status: 'off',
115
+ timeZone: this.manager.timeZone!,
116
+ token: this.manager.token!,
117
+ uuid: this.uuid
118
+ },
119
+ {
120
+ 'accept-language': 'en',
121
+ 'accountId': this.manager.accountId!,
122
+ 'appVersion': '2.8.6',
123
+ 'content-type': 'application/json',
124
+ 'tk': this.manager.token!,
125
+ 'tz': this.manager.timeZone!
126
+ }
127
+ );
128
+
129
+ const success = this.checkResponse([response, status], 'turnOff');
130
+ if (!success) {
131
+ logger.error(`Failed to turn off device: ${this.deviceName}`);
132
+ }
133
+ return success;
134
+ }
135
+
136
+ /**
137
+ * Change fan speed
138
+ */
139
+ async changeFanSpeed(speed: number): Promise<boolean> {
140
+ logger.info(`Changing fan speed to ${speed} for device: ${this.deviceName}`);
141
+
142
+ // Check if device is in manual mode
143
+ if (this.details.mode !== 'manual') {
144
+ logger.debug(`${this.deviceName} not in manual mode, cannot change speed`);
145
+ return false;
146
+ }
147
+
148
+ // Validate speed for LV series (1-3)
149
+ if (speed < 1 || speed > 3) {
150
+ logger.error(`Invalid fan speed: ${speed}. Must be between 1 and 3 for device: ${this.deviceName}`);
151
+ return false;
152
+ }
153
+
154
+ const [response, status] = await this.callApi(
155
+ '/131airPurifier/v1/device/updateSpeed',
156
+ 'put',
157
+ {
158
+ acceptLanguage: 'en',
159
+ accountID: this.manager.accountId!,
160
+ level: speed,
161
+ timeZone: this.manager.timeZone!,
162
+ token: this.manager.token!,
163
+ uuid: this.uuid
164
+ },
165
+ {
166
+ 'accept-language': 'en',
167
+ 'accountId': this.manager.accountId!,
168
+ 'appVersion': '2.8.6',
169
+ 'content-type': 'application/json',
170
+ 'tk': this.manager.token!,
171
+ 'tz': this.manager.timeZone!
172
+ }
173
+ );
174
+
175
+ const success = this.checkResponse([response, status], 'changeFanSpeed');
176
+ if (success) {
177
+ this.details.speed = speed;
178
+ return true;
179
+ } else {
180
+ logger.error(`Failed to change fan speed to ${speed} for device: ${this.deviceName}`);
181
+ return false;
182
+ }
183
+ }
184
+
185
+ /**
186
+ * Set device mode
187
+ */
188
+ async setMode(mode: string): Promise<boolean> {
189
+ if (!this.modes.includes(mode as any)) {
190
+ const error = `Invalid mode: ${mode}. Must be one of: ${this.modes.join(', ')}`;
191
+ logger.error(`${error} for device: ${this.deviceName}`);
192
+ throw new Error(error);
193
+ }
194
+
195
+ logger.debug(`Setting mode to ${mode} for device: ${this.deviceName}`);
196
+
197
+ const payload: Record<string, any> = {
198
+ acceptLanguage: 'en',
199
+ accountID: this.manager.accountId!,
200
+ mode: mode,
201
+ timeZone: this.manager.timeZone!,
202
+ token: this.manager.token!,
203
+ uuid: this.uuid
204
+ };
205
+
206
+ // For manual mode, we need to set the level
207
+ if (mode === 'manual') {
208
+ payload.level = this.details.speed || 1;
209
+ }
210
+
211
+ const [response, status] = await this.callApi(
212
+ '/131airPurifier/v1/device/updateMode',
213
+ 'put',
214
+ payload,
215
+ {
216
+ 'accept-language': 'en',
217
+ 'accountId': this.manager.accountId!,
218
+ 'appVersion': '2.8.6',
219
+ 'content-type': 'application/json',
220
+ 'tk': this.manager.token!,
221
+ 'tz': this.manager.timeZone!
222
+ }
223
+ );
224
+
225
+ const success = this.checkResponse([response, status], 'setMode');
226
+ if (success) {
227
+ this.details.mode = mode;
228
+ return true;
229
+ } else {
230
+ logger.error(`Failed to set mode to ${mode} for device: ${this.deviceName}`);
231
+ return false;
232
+ }
233
+ }
234
+
235
+ /**
236
+ * Set display status
237
+ */
238
+ async setDisplay(enabled: boolean): Promise<boolean> {
239
+ if (!this.hasFeature('display')) {
240
+ const error = 'Display control not supported';
241
+ logger.error(`${error} for device: ${this.deviceName}`);
242
+ throw new Error(error);
243
+ }
244
+
245
+ // Check if device is in sleep mode - display control may not work in sleep mode
246
+ if (this.details.mode === 'sleep') {
247
+ logger.warn(`Device ${this.deviceName} is in sleep mode, display control may not work`);
248
+ }
249
+
250
+ logger.debug(`Setting display to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`);
251
+
252
+ const [response, status] = await this.callApi(
253
+ '/131airPurifier/v1/device/updateScreen',
254
+ 'put',
255
+ {
256
+ acceptLanguage: 'en',
257
+ accountID: this.manager.accountId!,
258
+ status: enabled ? 'on' : 'off',
259
+ timeZone: this.manager.timeZone!,
260
+ token: this.manager.token!,
261
+ uuid: this.uuid
262
+ },
263
+ {
264
+ 'accept-language': 'en',
265
+ 'accountId': this.manager.accountId!,
266
+ 'appVersion': '2.8.6',
267
+ 'content-type': 'application/json',
268
+ 'tk': this.manager.token!,
269
+ 'tz': this.manager.timeZone!
270
+ }
271
+ );
272
+
273
+ const success = this.checkResponse([response, status], 'setDisplay');
274
+ if (success) {
275
+ this.details.screenStatus = enabled ? 'on' : 'off';
276
+ return true;
277
+ } else {
278
+ logger.error(`Failed to set display to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`);
279
+ return false;
280
+ }
281
+ }
282
+
283
+ /**
284
+ * Set child lock
285
+ */
286
+ async setChildLock(enabled: boolean): Promise<boolean> {
287
+ if (!this.hasFeature('child_lock')) {
288
+ const error = 'Child lock not supported';
289
+ logger.error(`${error} for device: ${this.deviceName}`);
290
+ throw new Error(error);
291
+ }
292
+
293
+ // Check if device is in sleep mode - child lock may not work in sleep mode
294
+ if (this.details.mode === 'sleep') {
295
+ logger.warn(`Device ${this.deviceName} is in sleep mode, child lock control may not work`);
296
+ }
297
+
298
+ logger.debug(`Setting child lock to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`);
299
+
300
+ const [response, status] = await this.callApi(
301
+ '/131airPurifier/v1/device/updateChildLock',
302
+ 'put',
303
+ {
304
+ acceptLanguage: 'en',
305
+ accountID: this.manager.accountId!,
306
+ status: enabled ? 'on' : 'off',
307
+ timeZone: this.manager.timeZone!,
308
+ token: this.manager.token!,
309
+ uuid: this.uuid
310
+ },
311
+ {
312
+ 'accept-language': 'en',
313
+ 'accountId': this.manager.accountId!,
314
+ 'appVersion': '2.8.6',
315
+ 'content-type': 'application/json',
316
+ 'tk': this.manager.token!,
317
+ 'tz': this.manager.timeZone!
318
+ }
319
+ );
320
+
321
+ const success = this.checkResponse([response, status], 'setChildLock');
322
+ if (success) {
323
+ this.details.childLock = enabled;
324
+ return true;
325
+ } else {
326
+ // Check for error code 11000000 (feature not supported)
327
+ if (response?.code === 11000000) {
328
+ logger.warn(`Child lock control not supported via API for device: ${this.deviceName}`);
329
+ return false;
330
+ }
331
+ logger.error(`Failed to set child lock to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`);
332
+ return false;
333
+ }
334
+ }
335
+
336
+ /**
337
+ * Set timer
338
+ */
339
+ async setTimer(hours: number): Promise<boolean> {
340
+ if (!this.hasFeature('timer')) {
341
+ const error = 'Timer not supported';
342
+ logger.error(`${error} for device: ${this.deviceName}`);
343
+ throw new Error(error);
344
+ }
345
+
346
+ logger.debug(`Setting timer to ${hours} hours for device: ${this.deviceName}`);
347
+
348
+ const [response, status] = await this.callApi(
349
+ '/131airPurifier/v1/device/updateTimer',
350
+ 'put',
351
+ {
352
+ acceptLanguage: 'en',
353
+ accountID: this.manager.accountId!,
354
+ action: 'off',
355
+ duration: hours,
356
+ timeZone: this.manager.timeZone!,
357
+ token: this.manager.token!,
358
+ uuid: this.uuid
359
+ },
360
+ {
361
+ 'accept-language': 'en',
362
+ 'accountId': this.manager.accountId!,
363
+ 'appVersion': '2.8.6',
364
+ 'content-type': 'application/json',
365
+ 'tk': this.manager.token!,
366
+ 'tz': this.manager.timeZone!
367
+ }
368
+ );
369
+
370
+ const success = this.checkResponse([response, status], 'setTimer');
371
+ if (success) {
372
+ this.timer = { duration: hours * 3600, action: 'off' };
373
+ return true;
374
+ } else {
375
+ logger.error(`Failed to set timer to ${hours} hours for device: ${this.deviceName}`);
376
+ return false;
377
+ }
378
+ }
379
+
380
+ /**
381
+ * Clear timer
382
+ */
383
+ async clearTimer(): Promise<boolean> {
384
+ if (!this.hasFeature('timer')) {
385
+ const error = 'Timer not supported';
386
+ logger.error(`${error} for device: ${this.deviceName}`);
387
+ throw new Error(error);
388
+ }
389
+
390
+ // If no timer is set, return success
391
+ if (!this.timer) {
392
+ logger.debug(`No timer to clear for device: ${this.deviceName}`);
393
+ return true;
394
+ }
395
+
396
+ logger.debug(`Clearing timer for device: ${this.deviceName}`);
397
+
398
+ const [response, status] = await this.callApi(
399
+ '/131airPurifier/v1/device/cancelTimer',
400
+ 'put',
401
+ {
402
+ acceptLanguage: 'en',
403
+ accountID: this.manager.accountId!,
404
+ timeZone: this.manager.timeZone!,
405
+ token: this.manager.token!,
406
+ uuid: this.uuid
407
+ },
408
+ {
409
+ 'accept-language': 'en',
410
+ 'accountId': this.manager.accountId!,
411
+ 'appVersion': '2.8.6',
412
+ 'content-type': 'application/json',
413
+ 'tk': this.manager.token!,
414
+ 'tz': this.manager.timeZone!
415
+ }
416
+ );
417
+
418
+ const success = this.checkResponse([response, status], 'clearTimer');
419
+ if (success) {
420
+ this.timer = null;
421
+ return true;
422
+ } else {
423
+ logger.error(`Failed to clear timer for device: ${this.deviceName}`);
424
+ return false;
425
+ }
426
+ }
427
+
428
+ /**
429
+ * Set auto mode
430
+ */
431
+ async autoMode(): Promise<boolean> {
432
+ logger.debug(`Setting auto mode for device: ${this.deviceName}`);
433
+ const success = await this.setMode('auto');
434
+ if (!success) {
435
+ logger.error(`Failed to set auto mode for device: ${this.deviceName}`);
436
+ }
437
+ return success;
438
+ }
439
+
440
+ /**
441
+ * Set manual mode
442
+ */
443
+ async manualMode(): Promise<boolean> {
444
+ logger.debug(`Setting manual mode for device: ${this.deviceName}`);
445
+ const success = await this.setMode('manual');
446
+ if (!success) {
447
+ logger.error(`Failed to set manual mode for device: ${this.deviceName}`);
448
+ }
449
+ return success;
450
+ }
451
+
452
+ /**
453
+ * Set sleep mode
454
+ */
455
+ async sleepMode(): Promise<boolean> {
456
+ logger.debug(`Setting sleep mode for device: ${this.deviceName}`);
457
+ const success = await this.setMode('sleep');
458
+ if (!success) {
459
+ logger.error(`Failed to set sleep mode for device: ${this.deviceName}`);
460
+ }
461
+ return success;
462
+ }
463
+
464
+ /**
465
+ * Turn off display
466
+ */
467
+ async turnOffDisplay(): Promise<boolean> {
468
+ logger.debug(`Turning off display for device: ${this.deviceName}`);
469
+ const success = await this.setDisplay(false);
470
+ if (!success) {
471
+ logger.error(`Failed to turn off display for device: ${this.deviceName}`);
472
+ }
473
+ return success;
474
+ }
475
+
476
+ /**
477
+ * Turn on display
478
+ */
479
+ async turnOnDisplay(): Promise<boolean> {
480
+ logger.debug(`Turning on display for device: ${this.deviceName}`);
481
+ const success = await this.setDisplay(true);
482
+ if (!success) {
483
+ logger.error(`Failed to turn on display for device: ${this.deviceName}`);
484
+ }
485
+ return success;
486
+ }
487
+
488
+ /**
489
+ * Get active time in minutes
490
+ */
491
+ get activeTime(): number {
492
+ return this.details.active_time || 0;
493
+ }
494
+
495
+ /**
496
+ * Display device info
497
+ */
498
+ override display(): void {
499
+ super.display();
500
+ const info = [
501
+ ['Mode: ', this.mode],
502
+ ['Speed: ', this.speed],
503
+ ['Filter Life: ', this.filterLife, '%'],
504
+ ['Screen Status: ', this.screenStatus],
505
+ ['Child Lock: ', this.childLock ? 'Enabled' : 'Disabled'],
506
+ ['Air Quality: ', this.airQuality],
507
+ ['Active Time: ', this.activeTime, 'minutes']
508
+ ];
509
+
510
+ for (const [key, value, unit = ''] of info) {
511
+ logger.info(`${key.toString().padEnd(30, '.')} ${value}${unit}`);
512
+ }
513
+ }
514
+
515
+ /**
516
+ * Return JSON details for device
517
+ */
518
+ override displayJSON(): string {
519
+ const baseInfo = JSON.parse(super.displayJSON());
520
+ const details: Record<string, string> = {
521
+ ...baseInfo,
522
+ 'Mode': this.mode,
523
+ 'Speed': this.speed.toString(),
524
+ 'Filter Life': this.filterLife.toString(),
525
+ 'Screen Status': this.screenStatus,
526
+ 'Child Lock': this.childLock ? 'Enabled' : 'Disabled',
527
+ 'Air Quality': this.airQuality,
528
+ 'Active Time': this.activeTime.toString()
529
+ };
530
+
531
+ return JSON.stringify(details, null, 4);
532
+ }
533
+ }