tsvesync 1.0.64 → 1.0.66

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,594 @@
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 with Bypass
8
+ */
9
+ export class VeSyncAirBypass extends VeSyncFan {
10
+ protected readonly modes = ['auto', 'manual', 'sleep'] as const;
11
+ protected readonly displayModes = ['on', 'off'] as const;
12
+ protected readonly childLockModes = ['on', 'off'] as const;
13
+
14
+ constructor(details: Record<string, any>, manager: VeSync) {
15
+ super(details, manager);
16
+ logger.debug(`Initialized VeSyncAirBypass device: ${this.deviceName}`);
17
+ }
18
+
19
+ /**
20
+ * Build API dictionary
21
+ */
22
+ protected buildApiDict(method: string): [Record<string, any>, Record<string, any>] {
23
+ const head = Helpers.reqHeaderBypass();
24
+ const body = {
25
+ ...Helpers.reqBody(this.manager, 'bypassV2'),
26
+ cid: this.cid,
27
+ configModule: this.configModule,
28
+ payload: {
29
+ method,
30
+ source: 'APP',
31
+ data: {}
32
+ }
33
+ };
34
+ return [head, body];
35
+ }
36
+
37
+ /**
38
+ * Get device details
39
+ */
40
+ async getDetails(): Promise<Boolean> {
41
+ logger.debug(`Getting details for device: ${this.deviceName}`);
42
+
43
+ // Special handling for LV series
44
+ if (this.deviceType.startsWith('LV-')) {
45
+ const [response, status] = await this.callApi(
46
+ '/131airPurifier/v1/device/devicedetail',
47
+ 'post',
48
+ {
49
+ acceptLanguage: 'en',
50
+ accountID: this.manager.accountId!,
51
+ appVersion: '2.8.6',
52
+ method: 'devicedetail',
53
+ mobileId: '1234567890123456',
54
+ phoneBrand: 'SM N9005',
55
+ phoneOS: 'Android',
56
+ timeZone: this.manager.timeZone!,
57
+ token: this.manager.token!,
58
+ uuid: this.uuid
59
+ },
60
+ {
61
+ 'accept-language': 'en',
62
+ 'accountId': this.manager.accountId!,
63
+ 'appVersion': '2.8.6',
64
+ 'content-type': 'application/json',
65
+ 'tk': this.manager.token!,
66
+ 'tz': this.manager.timeZone!
67
+ }
68
+ );
69
+
70
+ const success = this.checkResponse([response, status], 'getDetails');
71
+ if (success && response) {
72
+ this.deviceStatus = response.result?.result?.deviceStatus === 'on' ? 'on' : 'off';
73
+ this.details = {
74
+ mode: response.result?.result?.mode || '',
75
+ speed: response.result?.result?.level || 0,
76
+ filterLife: response.result?.result?.filterLife || 0,
77
+ screenStatus: response.result?.result?.display ? 'on' : 'off',
78
+ childLock: response.result?.result?.childLock || false,
79
+ airQuality: response.result?.result?.airQuality || 0
80
+ };
81
+ return true;
82
+ }
83
+ return false;
84
+ }
85
+
86
+ // Original implementation for other devices
87
+ const [response, status] = await this.callApi(
88
+ '/cloud/v2/deviceManaged/bypassV2',
89
+ 'post',
90
+ {
91
+ ...Helpers.reqBody(this.manager, 'bypassV2'),
92
+ cid: this.cid,
93
+ configModule: this.configModule,
94
+ payload: {
95
+ data: {},
96
+ method: 'getPurifierStatus',
97
+ source: 'APP'
98
+ }
99
+ },
100
+ Helpers.reqHeaderBypass()
101
+ );
102
+
103
+ const success = this.checkResponse([response, status], 'getDetails');
104
+ if (success) {
105
+ const result = response?.result?.result;
106
+ if (result) {
107
+ this.deviceStatus = result.enabled ? 'on' : 'off';
108
+ this.details = {
109
+ mode: result.mode || '',
110
+ speed: result.level || 0,
111
+ filterLife: result.filter_life || 0,
112
+ screenStatus: result.display ? 'on' : 'off',
113
+ childLock: result.child_lock || false,
114
+ airQuality: result.air_quality || 0
115
+ };
116
+
117
+ // Don't overwrite config as it contains features array
118
+ if (result.configuration) {
119
+ this.details.configuration = result.configuration;
120
+ }
121
+ logger.debug(`Successfully got details for device: ${this.deviceName}`);
122
+ }
123
+ }
124
+ return success;
125
+ }
126
+
127
+ /**
128
+ * Turn device on
129
+ */
130
+ async turnOn(): Promise<boolean> {
131
+ logger.info(`Turning on device: ${this.deviceName}`);
132
+
133
+ // Special handling for LV series
134
+ if (this.deviceType.startsWith('LV-')) {
135
+ const [response, status] = await this.callApi(
136
+ '/131airPurifier/v1/device/deviceStatus',
137
+ 'put',
138
+ {
139
+ acceptLanguage: 'en',
140
+ accountID: this.manager.accountId!,
141
+ status: 'on',
142
+ timeZone: this.manager.timeZone!,
143
+ token: this.manager.token!,
144
+ uuid: this.uuid
145
+ },
146
+ {
147
+ 'accept-language': 'en',
148
+ 'accountId': this.manager.accountId!,
149
+ 'appVersion': '2.8.6',
150
+ 'content-type': 'application/json',
151
+ 'tk': this.manager.token!,
152
+ 'tz': this.manager.timeZone!
153
+ }
154
+ );
155
+
156
+ const success = this.checkResponse([response, status], 'turnOn');
157
+ if (!success) {
158
+ logger.error(`Failed to turn on device: ${this.deviceName}`);
159
+ }
160
+ return success;
161
+ }
162
+
163
+ // Original implementation for other devices
164
+ const [response, status] = await this.callApi(
165
+ '/cloud/v2/deviceManaged/bypassV2',
166
+ 'post',
167
+ {
168
+ ...Helpers.reqBody(this.manager, 'bypassV2'),
169
+ cid: this.cid,
170
+ configModule: this.configModule,
171
+ payload: {
172
+ data: {
173
+ enabled: true,
174
+ id: 0
175
+ },
176
+ method: 'setSwitch',
177
+ source: 'APP'
178
+ }
179
+ },
180
+ Helpers.reqHeaderBypass()
181
+ );
182
+
183
+ const success = this.checkResponse([response, status], 'turnOn');
184
+ if (!success) {
185
+ logger.error(`Failed to turn on device: ${this.deviceName}`);
186
+ }
187
+ return success;
188
+ }
189
+
190
+ /**
191
+ * Turn device off
192
+ */
193
+ async turnOff(): Promise<boolean> {
194
+ logger.info(`Turning off device: ${this.deviceName}`);
195
+
196
+ // Special handling for LV series
197
+ if (this.deviceType.startsWith('LV-')) {
198
+ const [response, status] = await this.callApi(
199
+ '/131airPurifier/v1/device/deviceStatus',
200
+ 'put',
201
+ {
202
+ acceptLanguage: 'en',
203
+ accountID: this.manager.accountId!,
204
+ status: 'off',
205
+ timeZone: this.manager.timeZone!,
206
+ token: this.manager.token!,
207
+ uuid: this.uuid
208
+ },
209
+ {
210
+ 'accept-language': 'en',
211
+ 'accountId': this.manager.accountId!,
212
+ 'appVersion': '2.8.6',
213
+ 'content-type': 'application/json',
214
+ 'tk': this.manager.token!,
215
+ 'tz': this.manager.timeZone!
216
+ }
217
+ );
218
+
219
+ const success = this.checkResponse([response, status], 'turnOff');
220
+ if (!success) {
221
+ logger.error(`Failed to turn off device: ${this.deviceName}`);
222
+ }
223
+ return success;
224
+ }
225
+
226
+ // Original implementation for other devices
227
+ const [response, status] = await this.callApi(
228
+ '/cloud/v2/deviceManaged/bypassV2',
229
+ 'post',
230
+ {
231
+ ...Helpers.reqBody(this.manager, 'bypassV2'),
232
+ cid: this.cid,
233
+ configModule: this.configModule,
234
+ payload: {
235
+ data: {
236
+ enabled: false,
237
+ id: 0
238
+ },
239
+ method: 'setSwitch',
240
+ source: 'APP'
241
+ }
242
+ },
243
+ Helpers.reqHeaderBypass()
244
+ );
245
+
246
+ const success = this.checkResponse([response, status], 'turnOff');
247
+ if (!success) {
248
+ logger.error(`Failed to turn off device: ${this.deviceName}`);
249
+ }
250
+ return success;
251
+ }
252
+
253
+ /**
254
+ * Change fan speed
255
+ */
256
+ async changeFanSpeed(speed: number): Promise<boolean> {
257
+ // const speeds = this.config.levels ?? [];
258
+ // if (!speeds.includes(speed)) {
259
+ // logger.error(`Invalid speed: ${speed}. Must be one of: ${speeds.join(', ')} for device: ${this.deviceName}`);
260
+ // return false;
261
+ // }
262
+
263
+ logger.info(`Changing fan speed to ${speed} for device: ${this.deviceName}`);
264
+ const [response, status] = await this.callApi(
265
+ '/cloud/v2/deviceManaged/bypassV2',
266
+ 'post',
267
+ {
268
+ ...Helpers.reqBody(this.manager, 'bypassV2'),
269
+ cid: this.cid,
270
+ configModule: this.configModule,
271
+ payload: {
272
+ data: {
273
+ id: 0,
274
+ level: speed,
275
+ mode: 'manual',
276
+ type: 'wind'
277
+ },
278
+ method: 'setLevel',
279
+ source: 'APP'
280
+ }
281
+ },
282
+ Helpers.reqHeaderBypass()
283
+ );
284
+
285
+ const success = this.checkResponse([response, status], 'changeFanSpeed');
286
+ if (!success) {
287
+ logger.error(`Failed to change fan speed to ${speed} for device: ${this.deviceName}`);
288
+ }
289
+ return success;
290
+ }
291
+
292
+ /**
293
+ * Set device mode
294
+ */
295
+ async setMode(mode: string): Promise<boolean> {
296
+ if (!this.modes.includes(mode as any)) {
297
+ const error = `Invalid mode: ${mode}. Must be one of: ${this.modes.join(', ')}`;
298
+ logger.error(`${error} for device: ${this.deviceName}`);
299
+ throw new Error(error);
300
+ }
301
+
302
+ logger.debug(`Setting mode to ${mode} for device: ${this.deviceName}`);
303
+ const [response, status] = await this.callApi(
304
+ '/cloud/v2/deviceManaged/bypassV2',
305
+ 'post',
306
+ {
307
+ ...Helpers.reqBody(this.manager, 'bypassV2'),
308
+ cid: this.cid,
309
+ configModule: this.configModule,
310
+ payload: {
311
+ data: {
312
+ mode
313
+ },
314
+ method: 'setPurifierMode',
315
+ source: 'APP'
316
+ }
317
+ },
318
+ Helpers.reqHeaderBypass()
319
+ );
320
+
321
+ const success = this.checkResponse([response, status], 'setMode');
322
+ if (!success) {
323
+ logger.error(`Failed to set mode to ${mode} for device: ${this.deviceName}`);
324
+ }
325
+ return success;
326
+ }
327
+
328
+ /**
329
+ * Set display status
330
+ */
331
+ async setDisplay(enabled: boolean): Promise<boolean> {
332
+ if (!this.hasFeature('display')) {
333
+ const error = 'Display control not supported';
334
+ logger.error(`${error} for device: ${this.deviceName}`);
335
+ throw new Error(error);
336
+ }
337
+
338
+ logger.debug(`Setting display to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`);
339
+ const [response, status] = await this.callApi(
340
+ '/cloud/v2/deviceManaged/bypassV2',
341
+ 'post',
342
+ {
343
+ ...Helpers.reqBody(this.manager, 'bypassV2'),
344
+ cid: this.cid,
345
+ configModule: this.configModule,
346
+ payload: {
347
+ data: {
348
+ state: enabled
349
+ },
350
+ method: 'setDisplay',
351
+ source: 'APP'
352
+ }
353
+ },
354
+ Helpers.reqHeaderBypass()
355
+ );
356
+
357
+ const success = this.checkResponse([response, status], 'setDisplay');
358
+ if (success) {
359
+ this.details.screenStatus = enabled ? 'on' : 'off';
360
+ } else {
361
+ logger.error(`Failed to set display to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`);
362
+ }
363
+ return success;
364
+ }
365
+
366
+ /**
367
+ * Set child lock
368
+ */
369
+ async setChildLock(enabled: boolean): Promise<boolean> {
370
+ if (!this.hasFeature('child_lock')) {
371
+ const error = 'Child lock not supported';
372
+ logger.error(`${error} for device: ${this.deviceName}`);
373
+ throw new Error(error);
374
+ }
375
+
376
+ logger.debug(`Setting child lock to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`);
377
+ const [response, status] = await this.callApi(
378
+ '/cloud/v2/deviceManaged/bypassV2',
379
+ 'post',
380
+ {
381
+ ...Helpers.reqBody(this.manager, 'bypassV2'),
382
+ cid: this.cid,
383
+ configModule: this.configModule,
384
+ payload: {
385
+ data: {
386
+ state: enabled
387
+ },
388
+ method: 'setChildLock',
389
+ source: 'APP'
390
+ }
391
+ },
392
+ Helpers.reqHeaderBypass()
393
+ );
394
+
395
+ const success = this.checkResponse([response, status], 'setChildLock');
396
+ if (success) {
397
+ this.details.child_lock = enabled;
398
+ } else {
399
+ logger.error(`Failed to set child lock to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`);
400
+ }
401
+ return success;
402
+ }
403
+
404
+ /**
405
+ * Set timer
406
+ */
407
+ async setTimer(hours: number): Promise<boolean> {
408
+ if (!this.hasFeature('timer')) {
409
+ const error = 'Timer not supported';
410
+ logger.error(`${error} for device: ${this.deviceName}`);
411
+ throw new Error(error);
412
+ }
413
+
414
+ logger.debug(`Setting timer to ${hours} hours for device: ${this.deviceName}`);
415
+ const [response, status] = await this.callApi(
416
+ '/cloud/v2/deviceManaged/bypassV2',
417
+ 'post',
418
+ {
419
+ ...Helpers.reqBody(this.manager, 'bypassV2'),
420
+ cid: this.cid,
421
+ configModule: this.configModule,
422
+ payload: {
423
+ data: {
424
+ action: 'off',
425
+ total: hours * 3600
426
+ },
427
+ method: 'addTimer',
428
+ source: 'APP'
429
+ }
430
+ },
431
+ Helpers.reqHeaderBypass()
432
+ );
433
+
434
+ const success = this.checkResponse([response, status], 'setTimer');
435
+ if (!success) {
436
+ logger.error(`Failed to set timer to ${hours} hours for device: ${this.deviceName}`);
437
+ }
438
+ return success;
439
+ }
440
+
441
+ /**
442
+ * Clear timer
443
+ */
444
+ async clearTimer(): Promise<boolean> {
445
+ if (!this.hasFeature('timer')) {
446
+ const error = 'Timer not supported';
447
+ logger.error(`${error} for device: ${this.deviceName}`);
448
+ throw new Error(error);
449
+ }
450
+
451
+ logger.debug(`Clearing timer for device: ${this.deviceName}`);
452
+ const [response, status] = await this.callApi(
453
+ '/cloud/v2/deviceManaged/bypassV2',
454
+ 'post',
455
+ {
456
+ ...Helpers.reqBody(this.manager, 'bypassV2'),
457
+ cid: this.cid,
458
+ configModule: this.configModule,
459
+ payload: {
460
+ data: {},
461
+ method: 'deleteTimer',
462
+ source: 'APP'
463
+ }
464
+ },
465
+ Helpers.reqHeaderBypass()
466
+ );
467
+
468
+ const success = this.checkResponse([response, status], 'clearTimer');
469
+ if (!success) {
470
+ logger.error(`Failed to clear timer for device: ${this.deviceName}`);
471
+ }
472
+ return success;
473
+ }
474
+
475
+ /**
476
+ * Turn automatic stop on
477
+ */
478
+ async automaticStopOn(): Promise<boolean> {
479
+ logger.debug(`Setting automatic stop on for device: ${this.deviceName}`);
480
+ const [response, status] = await this.callApi(
481
+ '/cloud/v2/deviceManaged/bypassV2',
482
+ 'post',
483
+ {
484
+ ...Helpers.reqBody(this.manager, 'bypassV2'),
485
+ cid: this.cid,
486
+ configModule: this.configModule,
487
+ payload: {
488
+ data: {
489
+ enabled: true
490
+ },
491
+ method: 'setAutomaticStop',
492
+ source: 'APP'
493
+ }
494
+ },
495
+ Helpers.reqHeaderBypass()
496
+ );
497
+
498
+ const success = this.checkResponse([response, status], 'automaticStopOn');
499
+ if (!success) {
500
+ logger.error(`Failed to set automatic stop on for device: ${this.deviceName}`);
501
+ }
502
+ return success;
503
+ }
504
+
505
+ /**
506
+ * Turn automatic stop off
507
+ */
508
+ async automaticStopOff(): Promise<boolean> {
509
+ logger.debug(`Setting automatic stop off for device: ${this.deviceName}`);
510
+ const [response, status] = await this.callApi(
511
+ '/cloud/v2/deviceManaged/bypassV2',
512
+ 'post',
513
+ {
514
+ ...Helpers.reqBody(this.manager, 'bypassV2'),
515
+ cid: this.cid,
516
+ configModule: this.configModule,
517
+ payload: {
518
+ data: {
519
+ enabled: false
520
+ },
521
+ method: 'setAutomaticStop',
522
+ source: 'APP'
523
+ }
524
+ },
525
+ Helpers.reqHeaderBypass()
526
+ );
527
+
528
+ const success = this.checkResponse([response, status], 'automaticStopOff');
529
+ if (!success) {
530
+ logger.error(`Failed to set automatic stop off for device: ${this.deviceName}`);
531
+ }
532
+ return success;
533
+ }
534
+
535
+ /**
536
+ * Set auto mode
537
+ */
538
+ async autoMode(): Promise<boolean> {
539
+ logger.debug(`Setting auto mode for device: ${this.deviceName}`);
540
+ const success = await this.setMode('auto');
541
+ if (!success) {
542
+ logger.error(`Failed to set auto mode for device: ${this.deviceName}`);
543
+ }
544
+ return success;
545
+ }
546
+
547
+ /**
548
+ * Set manual mode
549
+ */
550
+ async manualMode(): Promise<boolean> {
551
+ logger.debug(`Setting manual mode for device: ${this.deviceName}`);
552
+ const success = await this.setMode('manual');
553
+ if (!success) {
554
+ logger.error(`Failed to set manual mode for device: ${this.deviceName}`);
555
+ }
556
+ return success;
557
+ }
558
+
559
+ /**
560
+ * Set sleep mode
561
+ */
562
+ async sleepMode(): Promise<boolean> {
563
+ logger.debug(`Setting sleep mode for device: ${this.deviceName}`);
564
+ const success = await this.setMode('sleep');
565
+ if (!success) {
566
+ logger.error(`Failed to set sleep mode for device: ${this.deviceName}`);
567
+ }
568
+ return success;
569
+ }
570
+
571
+ /**
572
+ * Turn off display
573
+ */
574
+ async turnOffDisplay(): Promise<boolean> {
575
+ logger.debug(`Turning off display for device: ${this.deviceName}`);
576
+ const success = await this.setDisplay(false);
577
+ if (!success) {
578
+ logger.error(`Failed to turn off display for device: ${this.deviceName}`);
579
+ }
580
+ return success;
581
+ }
582
+
583
+ /**
584
+ * Turn on display
585
+ */
586
+ async turnOnDisplay(): Promise<boolean> {
587
+ logger.debug(`Turning on display for device: ${this.deviceName}`);
588
+ const success = await this.setDisplay(true);
589
+ if (!success) {
590
+ logger.error(`Failed to turn on display for device: ${this.deviceName}`);
591
+ }
592
+ return success;
593
+ }
594
+ }