tsvesync 1.0.73 → 1.0.74

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,486 @@
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
+ };
62
+ return true;
63
+ }
64
+ return false;
65
+ }
66
+
67
+ /**
68
+ * Turn device on
69
+ */
70
+ async turnOn(): Promise<boolean> {
71
+ logger.info(`Turning on device: ${this.deviceName}`);
72
+
73
+ const [response, status] = await this.callApi(
74
+ '/131airPurifier/v1/device/deviceStatus',
75
+ 'put',
76
+ {
77
+ acceptLanguage: 'en',
78
+ accountID: this.manager.accountId!,
79
+ status: 'on',
80
+ timeZone: this.manager.timeZone!,
81
+ token: this.manager.token!,
82
+ uuid: this.uuid
83
+ },
84
+ {
85
+ 'accept-language': 'en',
86
+ 'accountId': this.manager.accountId!,
87
+ 'appVersion': '2.8.6',
88
+ 'content-type': 'application/json',
89
+ 'tk': this.manager.token!,
90
+ 'tz': this.manager.timeZone!
91
+ }
92
+ );
93
+
94
+ const success = this.checkResponse([response, status], 'turnOn');
95
+ if (!success) {
96
+ logger.error(`Failed to turn on device: ${this.deviceName}`);
97
+ }
98
+ return success;
99
+ }
100
+
101
+ /**
102
+ * Turn device off
103
+ */
104
+ async turnOff(): Promise<boolean> {
105
+ logger.info(`Turning off device: ${this.deviceName}`);
106
+
107
+ const [response, status] = await this.callApi(
108
+ '/131airPurifier/v1/device/deviceStatus',
109
+ 'put',
110
+ {
111
+ acceptLanguage: 'en',
112
+ accountID: this.manager.accountId!,
113
+ status: 'off',
114
+ timeZone: this.manager.timeZone!,
115
+ token: this.manager.token!,
116
+ uuid: this.uuid
117
+ },
118
+ {
119
+ 'accept-language': 'en',
120
+ 'accountId': this.manager.accountId!,
121
+ 'appVersion': '2.8.6',
122
+ 'content-type': 'application/json',
123
+ 'tk': this.manager.token!,
124
+ 'tz': this.manager.timeZone!
125
+ }
126
+ );
127
+
128
+ const success = this.checkResponse([response, status], 'turnOff');
129
+ if (!success) {
130
+ logger.error(`Failed to turn off device: ${this.deviceName}`);
131
+ }
132
+ return success;
133
+ }
134
+
135
+ /**
136
+ * Change fan speed
137
+ */
138
+ async changeFanSpeed(speed: number): Promise<boolean> {
139
+ logger.info(`Changing fan speed to ${speed} for device: ${this.deviceName}`);
140
+
141
+ // Check if device is in manual mode
142
+ if (this.details.mode !== 'manual') {
143
+ logger.debug(`${this.deviceName} not in manual mode, cannot change speed`);
144
+ return false;
145
+ }
146
+
147
+ // Validate speed for LV series (1-3)
148
+ if (speed < 1 || speed > 3) {
149
+ logger.error(`Invalid fan speed: ${speed}. Must be between 1 and 3 for device: ${this.deviceName}`);
150
+ return false;
151
+ }
152
+
153
+ const [response, status] = await this.callApi(
154
+ '/131airPurifier/v1/device/updateSpeed',
155
+ 'put',
156
+ {
157
+ acceptLanguage: 'en',
158
+ accountID: this.manager.accountId!,
159
+ level: speed,
160
+ timeZone: this.manager.timeZone!,
161
+ token: this.manager.token!,
162
+ uuid: this.uuid
163
+ },
164
+ {
165
+ 'accept-language': 'en',
166
+ 'accountId': this.manager.accountId!,
167
+ 'appVersion': '2.8.6',
168
+ 'content-type': 'application/json',
169
+ 'tk': this.manager.token!,
170
+ 'tz': this.manager.timeZone!
171
+ }
172
+ );
173
+
174
+ const success = this.checkResponse([response, status], 'changeFanSpeed');
175
+ if (success) {
176
+ this.details.speed = speed;
177
+ return true;
178
+ } else {
179
+ logger.error(`Failed to change fan speed to ${speed} for device: ${this.deviceName}`);
180
+ return false;
181
+ }
182
+ }
183
+
184
+ /**
185
+ * Set device mode
186
+ */
187
+ async setMode(mode: string): Promise<boolean> {
188
+ if (!this.modes.includes(mode as any)) {
189
+ const error = `Invalid mode: ${mode}. Must be one of: ${this.modes.join(', ')}`;
190
+ logger.error(`${error} for device: ${this.deviceName}`);
191
+ throw new Error(error);
192
+ }
193
+
194
+ logger.debug(`Setting mode to ${mode} for device: ${this.deviceName}`);
195
+
196
+ const payload: Record<string, any> = {
197
+ acceptLanguage: 'en',
198
+ accountID: this.manager.accountId!,
199
+ mode: mode,
200
+ timeZone: this.manager.timeZone!,
201
+ token: this.manager.token!,
202
+ uuid: this.uuid
203
+ };
204
+
205
+ // For manual mode, we need to set the level
206
+ if (mode === 'manual') {
207
+ payload.level = this.details.speed || 1;
208
+ }
209
+
210
+ const [response, status] = await this.callApi(
211
+ '/131airPurifier/v1/device/updateMode',
212
+ 'put',
213
+ payload,
214
+ {
215
+ 'accept-language': 'en',
216
+ 'accountId': this.manager.accountId!,
217
+ 'appVersion': '2.8.6',
218
+ 'content-type': 'application/json',
219
+ 'tk': this.manager.token!,
220
+ 'tz': this.manager.timeZone!
221
+ }
222
+ );
223
+
224
+ const success = this.checkResponse([response, status], 'setMode');
225
+ if (success) {
226
+ this.details.mode = mode;
227
+ return true;
228
+ } else {
229
+ logger.error(`Failed to set mode to ${mode} for device: ${this.deviceName}`);
230
+ return false;
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Set display status
236
+ */
237
+ async setDisplay(enabled: boolean): Promise<boolean> {
238
+ if (!this.hasFeature('display')) {
239
+ const error = 'Display control not supported';
240
+ logger.error(`${error} for device: ${this.deviceName}`);
241
+ throw new Error(error);
242
+ }
243
+
244
+ // Check if device is in sleep mode - display control may not work in sleep mode
245
+ if (this.details.mode === 'sleep') {
246
+ logger.warn(`Device ${this.deviceName} is in sleep mode, display control may not work`);
247
+ }
248
+
249
+ logger.debug(`Setting display to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`);
250
+
251
+ const [response, status] = await this.callApi(
252
+ '/131airPurifier/v1/device/updateScreen',
253
+ 'put',
254
+ {
255
+ acceptLanguage: 'en',
256
+ accountID: this.manager.accountId!,
257
+ status: enabled ? 'on' : 'off',
258
+ timeZone: this.manager.timeZone!,
259
+ token: this.manager.token!,
260
+ uuid: this.uuid
261
+ },
262
+ {
263
+ 'accept-language': 'en',
264
+ 'accountId': this.manager.accountId!,
265
+ 'appVersion': '2.8.6',
266
+ 'content-type': 'application/json',
267
+ 'tk': this.manager.token!,
268
+ 'tz': this.manager.timeZone!
269
+ }
270
+ );
271
+
272
+ const success = this.checkResponse([response, status], 'setDisplay');
273
+ if (success) {
274
+ this.details.screenStatus = enabled ? 'on' : 'off';
275
+ return true;
276
+ } else {
277
+ logger.error(`Failed to set display to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`);
278
+ return false;
279
+ }
280
+ }
281
+
282
+ /**
283
+ * Set child lock
284
+ */
285
+ async setChildLock(enabled: boolean): Promise<boolean> {
286
+ if (!this.hasFeature('child_lock')) {
287
+ const error = 'Child lock not supported';
288
+ logger.error(`${error} for device: ${this.deviceName}`);
289
+ throw new Error(error);
290
+ }
291
+
292
+ // Check if device is in sleep mode - child lock may not work in sleep mode
293
+ if (this.details.mode === 'sleep') {
294
+ logger.warn(`Device ${this.deviceName} is in sleep mode, child lock control may not work`);
295
+ }
296
+
297
+ logger.debug(`Setting child lock to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`);
298
+
299
+ const [response, status] = await this.callApi(
300
+ '/131airPurifier/v1/device/updateChildLock',
301
+ 'put',
302
+ {
303
+ acceptLanguage: 'en',
304
+ accountID: this.manager.accountId!,
305
+ status: enabled ? 'on' : 'off',
306
+ timeZone: this.manager.timeZone!,
307
+ token: this.manager.token!,
308
+ uuid: this.uuid
309
+ },
310
+ {
311
+ 'accept-language': 'en',
312
+ 'accountId': this.manager.accountId!,
313
+ 'appVersion': '2.8.6',
314
+ 'content-type': 'application/json',
315
+ 'tk': this.manager.token!,
316
+ 'tz': this.manager.timeZone!
317
+ }
318
+ );
319
+
320
+ const success = this.checkResponse([response, status], 'setChildLock');
321
+ if (success) {
322
+ this.details.childLock = enabled;
323
+ return true;
324
+ } else {
325
+ // Check for error code 11000000 (feature not supported)
326
+ if (response?.code === 11000000) {
327
+ logger.warn(`Child lock control not supported via API for device: ${this.deviceName}`);
328
+ return false;
329
+ }
330
+ logger.error(`Failed to set child lock to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`);
331
+ return false;
332
+ }
333
+ }
334
+
335
+ /**
336
+ * Set timer
337
+ */
338
+ async setTimer(hours: number): Promise<boolean> {
339
+ if (!this.hasFeature('timer')) {
340
+ const error = 'Timer not supported';
341
+ logger.error(`${error} for device: ${this.deviceName}`);
342
+ throw new Error(error);
343
+ }
344
+
345
+ logger.debug(`Setting timer to ${hours} hours for device: ${this.deviceName}`);
346
+
347
+ const [response, status] = await this.callApi(
348
+ '/131airPurifier/v1/device/updateTimer',
349
+ 'put',
350
+ {
351
+ acceptLanguage: 'en',
352
+ accountID: this.manager.accountId!,
353
+ action: 'off',
354
+ duration: hours,
355
+ timeZone: this.manager.timeZone!,
356
+ token: this.manager.token!,
357
+ uuid: this.uuid
358
+ },
359
+ {
360
+ 'accept-language': 'en',
361
+ 'accountId': this.manager.accountId!,
362
+ 'appVersion': '2.8.6',
363
+ 'content-type': 'application/json',
364
+ 'tk': this.manager.token!,
365
+ 'tz': this.manager.timeZone!
366
+ }
367
+ );
368
+
369
+ const success = this.checkResponse([response, status], 'setTimer');
370
+ if (success) {
371
+ this.timer = { duration: hours * 3600, action: 'off' };
372
+ return true;
373
+ } else {
374
+ logger.error(`Failed to set timer to ${hours} hours for device: ${this.deviceName}`);
375
+ return false;
376
+ }
377
+ }
378
+
379
+ /**
380
+ * Clear timer
381
+ */
382
+ async clearTimer(): Promise<boolean> {
383
+ if (!this.hasFeature('timer')) {
384
+ const error = 'Timer not supported';
385
+ logger.error(`${error} for device: ${this.deviceName}`);
386
+ throw new Error(error);
387
+ }
388
+
389
+ // If no timer is set, return success
390
+ if (!this.timer) {
391
+ logger.debug(`No timer to clear for device: ${this.deviceName}`);
392
+ return true;
393
+ }
394
+
395
+ logger.debug(`Clearing timer for device: ${this.deviceName}`);
396
+
397
+ const [response, status] = await this.callApi(
398
+ '/131airPurifier/v1/device/cancelTimer',
399
+ 'put',
400
+ {
401
+ acceptLanguage: 'en',
402
+ accountID: this.manager.accountId!,
403
+ timeZone: this.manager.timeZone!,
404
+ token: this.manager.token!,
405
+ uuid: this.uuid
406
+ },
407
+ {
408
+ 'accept-language': 'en',
409
+ 'accountId': this.manager.accountId!,
410
+ 'appVersion': '2.8.6',
411
+ 'content-type': 'application/json',
412
+ 'tk': this.manager.token!,
413
+ 'tz': this.manager.timeZone!
414
+ }
415
+ );
416
+
417
+ const success = this.checkResponse([response, status], 'clearTimer');
418
+ if (success) {
419
+ this.timer = null;
420
+ return true;
421
+ } else {
422
+ logger.error(`Failed to clear timer for device: ${this.deviceName}`);
423
+ return false;
424
+ }
425
+ }
426
+
427
+ /**
428
+ * Set auto mode
429
+ */
430
+ async autoMode(): Promise<boolean> {
431
+ logger.debug(`Setting auto mode for device: ${this.deviceName}`);
432
+ const success = await this.setMode('auto');
433
+ if (!success) {
434
+ logger.error(`Failed to set auto mode for device: ${this.deviceName}`);
435
+ }
436
+ return success;
437
+ }
438
+
439
+ /**
440
+ * Set manual mode
441
+ */
442
+ async manualMode(): Promise<boolean> {
443
+ logger.debug(`Setting manual mode for device: ${this.deviceName}`);
444
+ const success = await this.setMode('manual');
445
+ if (!success) {
446
+ logger.error(`Failed to set manual mode for device: ${this.deviceName}`);
447
+ }
448
+ return success;
449
+ }
450
+
451
+ /**
452
+ * Set sleep mode
453
+ */
454
+ async sleepMode(): Promise<boolean> {
455
+ logger.debug(`Setting sleep mode for device: ${this.deviceName}`);
456
+ const success = await this.setMode('sleep');
457
+ if (!success) {
458
+ logger.error(`Failed to set sleep mode for device: ${this.deviceName}`);
459
+ }
460
+ return success;
461
+ }
462
+
463
+ /**
464
+ * Turn off display
465
+ */
466
+ async turnOffDisplay(): Promise<boolean> {
467
+ logger.debug(`Turning off display for device: ${this.deviceName}`);
468
+ const success = await this.setDisplay(false);
469
+ if (!success) {
470
+ logger.error(`Failed to turn off display for device: ${this.deviceName}`);
471
+ }
472
+ return success;
473
+ }
474
+
475
+ /**
476
+ * Turn on display
477
+ */
478
+ async turnOnDisplay(): Promise<boolean> {
479
+ logger.debug(`Turning on display for device: ${this.deviceName}`);
480
+ const success = await this.setDisplay(true);
481
+ if (!success) {
482
+ logger.error(`Failed to turn on display for device: ${this.deviceName}`);
483
+ }
484
+ return success;
485
+ }
486
+ }