tsvesync 1.0.12 → 1.0.13

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.
@@ -4,9 +4,7 @@
4
4
 
5
5
  import { VeSyncBaseDevice } from './vesyncBaseDevice';
6
6
  import { VeSync } from './vesync';
7
- import { Helpers } from './helpers';
8
-
9
- type DeviceConstructor = new (config: Record<string, any>, manager: VeSync) => VeSyncBaseDevice;
7
+ import { logger } from './logger';
10
8
 
11
9
  interface SwitchConfig {
12
10
  [key: string]: {
@@ -68,7 +66,7 @@ export abstract class VeSyncSwitch extends VeSyncBaseDevice {
68
66
  ];
69
67
 
70
68
  for (const [key, value] of info) {
71
- console.log(`${key.toString().padEnd(30, '.')} ${value}`);
69
+ logger.info(`${key.toString().padEnd(30, '.')} ${value}`);
72
70
  }
73
71
  }
74
72
 
@@ -94,453 +92,4 @@ export abstract class VeSyncSwitch extends VeSyncBaseDevice {
94
92
  async update(): Promise<void> {
95
93
  await this.getDetails();
96
94
  }
97
- }
98
-
99
- /**
100
- * VeSync Wall Switch
101
- */
102
- export class VeSyncWallSwitch extends VeSyncSwitch {
103
- constructor(details: Record<string, any>, manager: VeSync) {
104
- super(details, manager);
105
- }
106
-
107
- /**
108
- * Get wall switch details
109
- */
110
- async getDetails(): Promise<void> {
111
- const body = Helpers.reqBody(this.manager, 'devicedetail');
112
- body.uuid = this.uuid;
113
-
114
- const [response] = await Helpers.callApi(
115
- '/inwallswitch/v1/device/devicedetail',
116
- 'post',
117
- body,
118
- Helpers.reqHeaders(this.manager)
119
- );
120
-
121
- if (response?.code === 0 && response?.result) {
122
- const result = response.result;
123
- this.deviceStatus = result.deviceStatus ?? this.deviceStatus;
124
- this.details.active_time = result.activeTime ?? 0;
125
- this.connectionStatus = result.connectionStatus ?? this.connectionStatus;
126
- } else {
127
- console.debug(`Error getting ${this.deviceName} details`);
128
- }
129
- }
130
-
131
- /**
132
- * Get switch device configuration info
133
- */
134
- async getConfig(): Promise<void> {
135
- const body = Helpers.reqBody(this.manager, 'devicedetail');
136
- body.method = 'configurations';
137
- body.uuid = this.uuid;
138
-
139
- const [response] = await Helpers.callApi(
140
- '/inwallswitch/v1/device/configurations',
141
- 'post',
142
- body,
143
- Helpers.reqHeaders(this.manager)
144
- );
145
-
146
- if (response?.code === 0) {
147
- this.config = Helpers.buildConfigDict(response);
148
- } else {
149
- console.debug(`Unable to get ${this.deviceName} config info`);
150
- }
151
- }
152
-
153
- /**
154
- * Turn off wall switch
155
- */
156
- async turnOff(): Promise<boolean> {
157
- const body = Helpers.reqBody(this.manager, 'devicestatus');
158
- body.status = 'off';
159
- body.uuid = this.uuid;
160
-
161
- const [response] = await Helpers.callApi(
162
- '/inwallswitch/v1/device/devicestatus',
163
- 'put',
164
- body,
165
- Helpers.reqHeaders(this.manager)
166
- );
167
-
168
- if (response?.code === 0) {
169
- this.deviceStatus = 'off';
170
- return true;
171
- }
172
- console.debug(`Error turning ${this.deviceName} off`);
173
- return false;
174
- }
175
-
176
- /**
177
- * Turn on wall switch
178
- */
179
- async turnOn(): Promise<boolean> {
180
- const body = Helpers.reqBody(this.manager, 'devicestatus');
181
- body.status = 'on';
182
- body.uuid = this.uuid;
183
-
184
- const [response] = await Helpers.callApi(
185
- '/inwallswitch/v1/device/devicestatus',
186
- 'put',
187
- body,
188
- Helpers.reqHeaders(this.manager)
189
- );
190
-
191
- if (response?.code === 0) {
192
- this.deviceStatus = 'on';
193
- return true;
194
- }
195
- console.debug(`Error turning ${this.deviceName} on`);
196
- return false;
197
- }
198
- }
199
-
200
- /**
201
- * VeSync Dimmer Switch
202
- */
203
- export class VeSyncDimmerSwitch extends VeSyncSwitch {
204
- private _brightness: number = 0;
205
- private _rgbValue: Record<string, number> = { red: 0, blue: 0, green: 0 };
206
- private _rgbStatus: string = 'unknown';
207
- private _indicatorLight: string = 'unknown';
208
-
209
- constructor(details: Record<string, any>, manager: VeSync) {
210
- super(details, manager);
211
- }
212
-
213
- /**
214
- * Get dimmer switch details
215
- */
216
- async getDetails(): Promise<void> {
217
- const body = Helpers.reqBody(this.manager, 'devicedetail');
218
- body.uuid = this.uuid;
219
-
220
- const [response] = await Helpers.callApi(
221
- '/dimmer/v1/device/devicedetail',
222
- 'post',
223
- body,
224
- Helpers.reqHeaders(this.manager)
225
- );
226
-
227
- if (response?.code === 0 && response?.result) {
228
- const result = response.result;
229
- this.deviceStatus = result.deviceStatus ?? this.deviceStatus;
230
- this.details.active_time = result.activeTime ?? 0;
231
- this.connectionStatus = result.connectionStatus ?? this.connectionStatus;
232
- this._brightness = result.brightness ?? this._brightness;
233
- this._rgbStatus = result.rgbStatus ?? this._rgbStatus;
234
- this._indicatorLight = result.indicatorlightStatus ?? this._indicatorLight;
235
- if (result.rgbValue) {
236
- this._rgbValue = {
237
- red: result.rgbValue.red ?? 0,
238
- green: result.rgbValue.green ?? 0,
239
- blue: result.rgbValue.blue ?? 0
240
- };
241
- }
242
- } else {
243
- console.debug(`Error getting ${this.deviceName} details`);
244
- }
245
- }
246
-
247
- /**
248
- * Get dimmer switch configuration info
249
- */
250
- async getConfig(): Promise<void> {
251
- const body = Helpers.reqBody(this.manager, 'devicedetail');
252
- body.method = 'configurations';
253
- body.uuid = this.uuid;
254
-
255
- const [response] = await Helpers.callApi(
256
- '/dimmer/v1/device/configurations',
257
- 'post',
258
- body,
259
- Helpers.reqHeaders(this.manager)
260
- );
261
-
262
- if (response?.code === 0) {
263
- this.config = Helpers.buildConfigDict(response);
264
- } else {
265
- console.debug(`Unable to get ${this.deviceName} config info`);
266
- }
267
- }
268
-
269
- /**
270
- * Turn off dimmer switch
271
- */
272
- async turnOff(): Promise<boolean> {
273
- const body = Helpers.reqBody(this.manager, 'devicestatus');
274
- body.status = 'off';
275
- body.uuid = this.uuid;
276
-
277
- const [response] = await Helpers.callApi(
278
- '/dimmer/v1/device/devicestatus',
279
- 'put',
280
- body,
281
- Helpers.reqHeaders(this.manager)
282
- );
283
-
284
- if (response?.code === 0) {
285
- this.deviceStatus = 'off';
286
- return true;
287
- }
288
- console.debug(`Error turning ${this.deviceName} off`);
289
- return false;
290
- }
291
-
292
- /**
293
- * Turn on dimmer switch
294
- */
295
- async turnOn(): Promise<boolean> {
296
- const body = Helpers.reqBody(this.manager, 'devicestatus');
297
- body.status = 'on';
298
- body.uuid = this.uuid;
299
-
300
- const [response] = await Helpers.callApi(
301
- '/dimmer/v1/device/devicestatus',
302
- 'put',
303
- body,
304
- Helpers.reqHeaders(this.manager)
305
- );
306
-
307
- if (response?.code === 0) {
308
- this.deviceStatus = 'on';
309
- return true;
310
- }
311
- console.debug(`Error turning ${this.deviceName} on`);
312
- return false;
313
- }
314
-
315
- /**
316
- * Return brightness in percent
317
- */
318
- get brightness(): number {
319
- return this._brightness;
320
- }
321
-
322
- /**
323
- * Return faceplate brightness light status
324
- */
325
- get indicatorLightStatus(): string {
326
- return this._indicatorLight;
327
- }
328
-
329
- /**
330
- * Return RGB faceplate light status
331
- */
332
- get rgbLightStatus(): string {
333
- return this._rgbStatus;
334
- }
335
-
336
- /**
337
- * Return RGB light values
338
- */
339
- get rgbLightValue(): Record<string, number> {
340
- return this._rgbValue;
341
- }
342
-
343
- /**
344
- * Update device details
345
- */
346
- async update(): Promise<void> {
347
- await this.getDetails();
348
- }
349
-
350
- /**
351
- * Display device info
352
- */
353
- display(): void {
354
- super.display();
355
- const info = [
356
- ['Brightness:', this.brightness, '%'],
357
- ['RGB Status:', this.rgbLightStatus],
358
- ['RGB Values:', JSON.stringify(this.rgbLightValue)],
359
- ['Indicator Light:', this.indicatorLightStatus]
360
- ];
361
-
362
- for (const [key, value, unit = ''] of info) {
363
- console.log(`${key.toString().padEnd(30, '.')} ${value}${unit}`);
364
- }
365
- }
366
-
367
- /**
368
- * Return JSON details for dimmer switch
369
- */
370
- displayJSON(): string {
371
- const baseInfo = JSON.parse(super.displayJSON());
372
- return JSON.stringify({
373
- ...baseInfo,
374
- 'Brightness': this.brightness,
375
- 'RGB Status': this.rgbLightStatus,
376
- 'RGB Values': this.rgbLightValue,
377
- 'Indicator Light': this.indicatorLightStatus
378
- }, null, 4);
379
- }
380
-
381
- /**
382
- * Set brightness level
383
- * @param brightness - Brightness level (1-100)
384
- */
385
- async setBrightness(brightness: number): Promise<boolean> {
386
- const body = Helpers.reqBody(this.manager, 'devicestatus');
387
- body.uuid = this.uuid;
388
- body.brightness = brightness;
389
-
390
- const [response] = await Helpers.callApi(
391
- '/dimmer/v1/device/updatebrightness',
392
- 'put',
393
- body,
394
- Helpers.reqHeaders(this.manager)
395
- );
396
-
397
- if (response?.code === 0) {
398
- this._brightness = brightness;
399
- return true;
400
- }
401
- console.debug(`Error setting ${this.deviceName} brightness`);
402
- return false;
403
- }
404
-
405
- /**
406
- * Toggle indicator light status
407
- * @param status - Light status ('on' or 'off')
408
- */
409
- async indicatorLightToggle(status: string): Promise<boolean> {
410
- if (status !== 'on' && status !== 'off') {
411
- console.debug('Invalid status passed to indicator light toggle');
412
- return false;
413
- }
414
-
415
- const body = Helpers.reqBody(this.manager, 'devicestatus');
416
- body.uuid = this.uuid;
417
- body.status = status;
418
-
419
- const [response] = await Helpers.callApi(
420
- '/dimmer/v1/device/indicatorlightstatus',
421
- 'put',
422
- body,
423
- Helpers.reqHeaders(this.manager)
424
- );
425
-
426
- if (response?.code === 0) {
427
- this._indicatorLight = status;
428
- return true;
429
- }
430
- console.debug(`Error toggling ${this.deviceName} indicator light`);
431
- return false;
432
- }
433
-
434
- /**
435
- * Set RGB color values
436
- * @param red - Red value (0-255)
437
- * @param green - Green value (0-255)
438
- * @param blue - Blue value (0-255)
439
- */
440
- async rgbColorSet(red: number, green: number, blue: number): Promise<boolean> {
441
- const body = Helpers.reqBody(this.manager, 'devicestatus');
442
- body.uuid = this.uuid;
443
- body.rgbValue = { red, green, blue };
444
- body.status = 'on';
445
-
446
- const [response] = await Helpers.callApi(
447
- '/dimmer/v1/device/devicergbstatus',
448
- 'put',
449
- body,
450
- Helpers.reqHeaders(this.manager)
451
- );
452
-
453
- if (response?.code === 0) {
454
- this._rgbValue = { red, green, blue };
455
- this._rgbStatus = 'on';
456
- return true;
457
- }
458
- console.debug(`Error setting ${this.deviceName} RGB color`);
459
- return false;
460
- }
461
-
462
- /**
463
- * Toggle switch status
464
- * @param status - Switch status ('on' or 'off')
465
- */
466
- async switchToggle(status: string): Promise<boolean> {
467
- if (status !== 'on' && status !== 'off') {
468
- console.debug('Invalid status passed to switch toggle');
469
- return false;
470
- }
471
-
472
- return status === 'on' ? this.turnOn() : this.turnOff();
473
- }
474
-
475
- /**
476
- * Set faceplate RGB color status and optionally set RGB values
477
- * @param status - RGB status ('on' or 'off')
478
- * @param red - Optional red value (0-255)
479
- * @param green - Optional green value (0-255)
480
- * @param blue - Optional blue value (0-255)
481
- */
482
- async rgbColorStatus(status: string, red?: number, green?: number, blue?: number): Promise<boolean> {
483
- if (status !== 'on' && status !== 'off') {
484
- console.debug('Invalid status passed to RGB color status');
485
- return false;
486
- }
487
-
488
- const body = Helpers.reqBody(this.manager, 'devicestatus');
489
- body.uuid = this.uuid;
490
- body.status = status;
491
-
492
- if (red !== undefined && green !== undefined && blue !== undefined) {
493
- body.rgbValue = { red, green, blue };
494
- this._rgbValue = { red, green, blue };
495
- }
496
-
497
- const [response] = await Helpers.callApi(
498
- '/dimmer/v1/device/devicergbstatus',
499
- 'put',
500
- body,
501
- Helpers.reqHeaders(this.manager)
502
- );
503
-
504
- if (response?.code === 0) {
505
- this._rgbStatus = status;
506
- return true;
507
- }
508
- console.debug(`Error setting ${this.deviceName} RGB status`);
509
- return false;
510
- }
511
-
512
- /**
513
- * Turn RGB Color Off
514
- */
515
- async rgbColorOff(): Promise<boolean> {
516
- return this.rgbColorStatus('off');
517
- }
518
-
519
- /**
520
- * Turn RGB Color On
521
- */
522
- async rgbColorOn(): Promise<boolean> {
523
- return this.rgbColorStatus('on');
524
- }
525
-
526
- /**
527
- * Turn indicator light on
528
- */
529
- async indicatorLightOn(): Promise<boolean> {
530
- return this.indicatorLightToggle('on');
531
- }
532
-
533
- /**
534
- * Turn indicator light off
535
- */
536
- async indicatorLightOff(): Promise<boolean> {
537
- return this.indicatorLightToggle('off');
538
- }
539
- }
540
-
541
- // Export switch modules for device factory
542
- export const switchModules: Record<string, DeviceConstructor> = {
543
- 'ESWL01': VeSyncWallSwitch,
544
- 'ESWL03': VeSyncWallSwitch,
545
- 'ESWD16': VeSyncDimmerSwitch
546
- };
95
+ }