tsvesync 1.3.9 → 1.3.11

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,8 +4,19 @@
4
4
 
5
5
  import { VeSyncSwitch } from './vesyncSwitch';
6
6
  import { VeSync } from './vesync';
7
- import { Helpers } from './helpers';
7
+ import { Helpers, APP_VERSION, PHONE_BRAND, PHONE_OS, DEFAULT_TZ, DEFAULT_REGION, MOBILE_ID } from './helpers';
8
8
  import { logger } from './logger';
9
+ import { switchConfig } from './vesyncSwitch';
10
+
11
+ interface DimmerState {
12
+ activeTime: number;
13
+ connectionStatus: string;
14
+ brightness: number;
15
+ rgbStatus: string;
16
+ indicatorStatus: string;
17
+ rgbValue: { red: number; green: number; blue: number };
18
+ deviceStatus: string;
19
+ }
9
20
 
10
21
  /**
11
22
  * Basic Wall Switch Implementation (ESWL01, ESWL03)
@@ -130,10 +141,15 @@ export class VeSyncWallSwitch extends VeSyncSwitch {
130
141
  * Dimmer Switch Implementation (ESWD16)
131
142
  */
132
143
  export class VeSyncDimmerSwitch extends VeSyncSwitch {
133
- private _brightness: number = 0;
134
- private _rgbValue: Record<string, number> = { red: 0, blue: 0, green: 0 };
135
- private _rgbStatus: string = 'unknown';
136
- private _indicatorLight: string = 'unknown';
144
+ private stateSnapshot: DimmerState = {
145
+ activeTime: 0,
146
+ connectionStatus: 'offline',
147
+ brightness: 0,
148
+ rgbStatus: 'off',
149
+ indicatorStatus: 'off',
150
+ rgbValue: { red: 0, green: 0, blue: 0 },
151
+ deviceStatus: 'off'
152
+ };
137
153
 
138
154
  constructor(details: Record<string, any>, manager: VeSync) {
139
155
  super(details, manager);
@@ -145,35 +161,27 @@ export class VeSyncDimmerSwitch extends VeSyncSwitch {
145
161
  */
146
162
  async getDetails(): Promise<Boolean> {
147
163
  logger.debug(`Getting details for device: ${this.deviceName}`);
148
- const body = {
149
- ...Helpers.reqBody(this.manager, 'devicedetail'),
150
- uuid: this.uuid,
151
- method: 'devicedetail'
152
- };
153
-
154
- const [response, statusCode] = await this.callApi(
155
- '/dimmer/v1/device/devicedetail',
156
- 'post',
157
- body,
158
- Helpers.reqHeaders(this.manager)
159
- );
164
+ const [response, statusCode] = await this.callDimmerCommand('deviceDetail', {}, 'deviceDetail');
160
165
 
161
166
  const success = this.checkResponse([response, statusCode], 'getDetails');
162
167
  if (success && response?.result) {
163
168
  const result = response.result;
164
- this.deviceStatus = result.deviceStatus || this.deviceStatus;
165
- this.details.active_time = result.activeTime || 0;
166
- this.connectionStatus = result.connectionStatus || this.connectionStatus;
167
- this._brightness = result.brightness || this._brightness;
168
- this._rgbStatus = result.rgbStatus || this._rgbStatus;
169
- this._indicatorLight = result.indicatorlightStatus || this._indicatorLight;
170
- if (result.rgbValue) {
171
- this._rgbValue = {
172
- red: result.rgbValue.red || 0,
173
- green: result.rgbValue.green || 0,
174
- blue: result.rgbValue.blue || 0
175
- };
176
- }
169
+ this.stateSnapshot = {
170
+ activeTime: result.activeTime ?? this.stateSnapshot.activeTime,
171
+ connectionStatus: result.connectionStatus ?? this.stateSnapshot.connectionStatus,
172
+ brightness: Number(result.brightness ?? this.stateSnapshot.brightness),
173
+ rgbStatus: result.rgbStatus ?? this.stateSnapshot.rgbStatus,
174
+ indicatorStatus: result.indicatorlightStatus ?? this.stateSnapshot.indicatorStatus,
175
+ rgbValue: {
176
+ red: Number(result.rgbValue?.red ?? this.stateSnapshot.rgbValue.red),
177
+ green: Number(result.rgbValue?.green ?? this.stateSnapshot.rgbValue.green),
178
+ blue: Number(result.rgbValue?.blue ?? this.stateSnapshot.rgbValue.blue)
179
+ },
180
+ deviceStatus: result.deviceStatus ?? this.stateSnapshot.deviceStatus
181
+ };
182
+ this.details.active_time = this.stateSnapshot.activeTime;
183
+ this.connectionStatus = this.stateSnapshot.connectionStatus;
184
+ this.deviceStatus = this.stateSnapshot.deviceStatus;
177
185
  logger.debug(`Successfully got details for device: ${this.deviceName}`);
178
186
  }
179
187
  return success;
@@ -209,54 +217,14 @@ export class VeSyncDimmerSwitch extends VeSyncSwitch {
209
217
  * Turn off dimmer switch
210
218
  */
211
219
  async turnOff(): Promise<boolean> {
212
- logger.info(`Turning off device: ${this.deviceName}`);
213
- const body = {
214
- ...Helpers.reqBody(this.manager, 'devicestatus'),
215
- status: 'off',
216
- uuid: this.uuid
217
- };
218
-
219
- const [response] = await this.callApi(
220
- '/dimmer/v1/device/devicestatus',
221
- 'put',
222
- body,
223
- Helpers.reqHeaders(this.manager)
224
- );
225
-
226
- if (response?.code === 0) {
227
- this.deviceStatus = 'off';
228
- logger.debug(`Successfully turned off device: ${this.deviceName}`);
229
- return true;
230
- }
231
- logger.error(`Failed to turn off device: ${this.deviceName}`);
232
- return false;
220
+ return await this.toggleSwitch(false);
233
221
  }
234
222
 
235
223
  /**
236
224
  * Turn on dimmer switch
237
225
  */
238
226
  async turnOn(): Promise<boolean> {
239
- logger.info(`Turning on device: ${this.deviceName}`);
240
- const body = {
241
- ...Helpers.reqBody(this.manager, 'devicestatus'),
242
- status: 'on',
243
- uuid: this.uuid
244
- };
245
-
246
- const [response] = await this.callApi(
247
- '/dimmer/v1/device/devicestatus',
248
- 'put',
249
- body,
250
- Helpers.reqHeaders(this.manager)
251
- );
252
-
253
- if (response?.code === 0) {
254
- this.deviceStatus = 'on';
255
- logger.debug(`Successfully turned on device: ${this.deviceName}`);
256
- return true;
257
- }
258
- logger.error(`Failed to turn on device: ${this.deviceName}`);
259
- return false;
227
+ return await this.toggleSwitch(true);
260
228
  }
261
229
 
262
230
  /**
@@ -269,25 +237,19 @@ export class VeSyncDimmerSwitch extends VeSyncSwitch {
269
237
  }
270
238
 
271
239
  logger.debug(`Setting brightness to ${brightness} for device: ${this.deviceName}`);
272
- const body = {
273
- ...Helpers.reqBody(this.manager, 'devicestatus'),
274
- brightness,
275
- uuid: this.uuid
276
- };
277
-
278
- const [response] = await this.callApi(
279
- '/dimmer/v1/device/updatebrightness',
280
- 'put',
281
- body,
282
- Helpers.reqHeaders(this.manager)
283
- );
240
+ const value = Math.max(0, Math.min(100, Math.round(brightness)));
241
+ const [response] = await this.callDimmerCommand('dimmerBrightnessCtl', {
242
+ brightness: String(value)
243
+ }, 'dimmerBrightnessCtl');
284
244
 
285
245
  if (response?.code === 0) {
286
- this._brightness = brightness;
287
- logger.debug(`Successfully set brightness to ${brightness} for device: ${this.deviceName}`);
246
+ this.stateSnapshot.brightness = value;
247
+ this.stateSnapshot.deviceStatus = 'on';
248
+ this.deviceStatus = 'on';
249
+ logger.debug(`Successfully set brightness to ${value} for device: ${this.deviceName}`);
288
250
  return true;
289
251
  }
290
- logger.error(`Failed to set brightness to ${brightness} for device: ${this.deviceName}`);
252
+ logger.error(`Failed to set brightness to ${value} for device: ${this.deviceName}`);
291
253
  return false;
292
254
  }
293
255
 
@@ -296,27 +258,18 @@ export class VeSyncDimmerSwitch extends VeSyncSwitch {
296
258
  */
297
259
  async rgbColorSet(red: number, green: number, blue: number): Promise<boolean> {
298
260
  logger.debug(`Setting RGB color to (${red}, ${green}, ${blue}) for device: ${this.deviceName}`);
299
- const body = {
300
- ...Helpers.reqBody(this.manager, 'devicestatus'),
261
+ const [response] = await this.callDimmerCommand('dimmerRgbValueCtl', {
262
+ status: 'on',
301
263
  rgbValue: {
302
264
  red: Math.round(red),
303
265
  green: Math.round(green),
304
266
  blue: Math.round(blue)
305
- },
306
- status: 'on',
307
- uuid: this.uuid
308
- };
309
-
310
- const [response] = await this.callApi(
311
- '/dimmer/v1/device/devicergbstatus',
312
- 'put',
313
- body,
314
- Helpers.reqHeaders(this.manager)
315
- );
267
+ }
268
+ }, 'dimmerRgbValueCtl');
316
269
 
317
270
  if (response?.code === 0) {
318
- this._rgbValue = { red, green, blue };
319
- this._rgbStatus = 'on';
271
+ this.stateSnapshot.rgbValue = { red: Math.round(red), green: Math.round(green), blue: Math.round(blue) };
272
+ this.stateSnapshot.rgbStatus = 'on';
320
273
  logger.debug(`Successfully set RGB color for device: ${this.deviceName}`);
321
274
  return true;
322
275
  }
@@ -329,21 +282,12 @@ export class VeSyncDimmerSwitch extends VeSyncSwitch {
329
282
  */
330
283
  async rgbColorOff(): Promise<boolean> {
331
284
  logger.debug(`Turning off RGB color for device: ${this.deviceName}`);
332
- const body = {
333
- ...Helpers.reqBody(this.manager, 'devicestatus'),
334
- status: 'off',
335
- uuid: this.uuid
336
- };
337
-
338
- const [response] = await this.callApi(
339
- '/dimmer/v1/device/devicergbstatus',
340
- 'put',
341
- body,
342
- Helpers.reqHeaders(this.manager)
343
- );
285
+ const [response] = await this.callDimmerCommand('dimmerRgbValueCtl', {
286
+ status: 'off'
287
+ }, 'dimmerRgbValueCtl');
344
288
 
345
289
  if (response?.code === 0) {
346
- this._rgbStatus = 'off';
290
+ this.stateSnapshot.rgbStatus = 'off';
347
291
  logger.debug(`Successfully turned off RGB color for device: ${this.deviceName}`);
348
292
  return true;
349
293
  }
@@ -356,21 +300,12 @@ export class VeSyncDimmerSwitch extends VeSyncSwitch {
356
300
  */
357
301
  async rgbColorOn(): Promise<boolean> {
358
302
  logger.debug(`Turning on RGB color for device: ${this.deviceName}`);
359
- const body = {
360
- ...Helpers.reqBody(this.manager, 'devicestatus'),
361
- uuid: this.uuid,
303
+ const [response] = await this.callDimmerCommand('dimmerRgbValueCtl', {
362
304
  status: 'on'
363
- };
364
-
365
- const [response] = await this.callApi(
366
- '/dimmer/v1/device/devicergbstatus',
367
- 'put',
368
- body,
369
- Helpers.reqHeaders(this.manager)
370
- );
305
+ }, 'dimmerRgbValueCtl');
371
306
 
372
307
  if (response?.code === 0) {
373
- this._rgbStatus = 'on';
308
+ this.stateSnapshot.rgbStatus = 'on';
374
309
  logger.debug(`Successfully turned on RGB color for device: ${this.deviceName}`);
375
310
  return true;
376
311
  }
@@ -383,26 +318,7 @@ export class VeSyncDimmerSwitch extends VeSyncSwitch {
383
318
  */
384
319
  async indicatorLightOn(): Promise<boolean> {
385
320
  logger.debug(`Turning on indicator light for device: ${this.deviceName}`);
386
- const body = {
387
- ...Helpers.reqBody(this.manager, 'devicestatus'),
388
- uuid: this.uuid,
389
- status: 'on'
390
- };
391
-
392
- const [response] = await this.callApi(
393
- '/dimmer/v1/device/indicatorlightstatus',
394
- 'put',
395
- body,
396
- Helpers.reqHeaders(this.manager)
397
- );
398
-
399
- if (response?.code === 0) {
400
- this._indicatorLight = 'on';
401
- logger.debug(`Successfully turned on indicator light for device: ${this.deviceName}`);
402
- return true;
403
- }
404
- logger.error(`Failed to turn on indicator light for device: ${this.deviceName}`);
405
- return false;
321
+ return this.toggleIndicatorLight(true);
406
322
  }
407
323
 
408
324
  /**
@@ -410,43 +326,94 @@ export class VeSyncDimmerSwitch extends VeSyncSwitch {
410
326
  */
411
327
  async indicatorLightOff(): Promise<boolean> {
412
328
  logger.debug(`Turning off indicator light for device: ${this.deviceName}`);
413
- const body = {
414
- ...Helpers.reqBody(this.manager, 'devicestatus'),
415
- uuid: this.uuid,
416
- status: 'off'
417
- };
418
-
419
- const [response] = await this.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 = 'off';
428
- logger.debug(`Successfully turned off indicator light for device: ${this.deviceName}`);
429
- return true;
430
- }
431
- logger.error(`Failed to turn off indicator light for device: ${this.deviceName}`);
432
- return false;
329
+ return this.toggleIndicatorLight(false);
433
330
  }
434
331
 
435
332
  // Getters
436
333
  get brightness(): number {
437
- return this._brightness;
334
+ return this.stateSnapshot.brightness;
438
335
  }
439
336
 
440
337
  get indicatorLightStatus(): string {
441
- return this._indicatorLight;
338
+ return this.stateSnapshot.indicatorStatus;
442
339
  }
443
340
 
444
341
  get rgbLightStatus(): string {
445
- return this._rgbStatus;
342
+ return this.stateSnapshot.rgbStatus;
446
343
  }
447
344
 
448
345
  get rgbLightValue(): Record<string, number> {
449
- return { ...this._rgbValue };
346
+ return { ...this.stateSnapshot.rgbValue };
347
+ }
348
+
349
+ private async toggleSwitch(turnOn: boolean): Promise<boolean> {
350
+ const [response] = await this.callDimmerCommand('dimmerPowerSwitchCtl', {
351
+ status: turnOn ? 'on' : 'off'
352
+ }, 'dimmerPowerSwitchCtl');
353
+
354
+ if (response?.code === 0) {
355
+ this.stateSnapshot.deviceStatus = turnOn ? 'on' : 'off';
356
+ this.deviceStatus = this.stateSnapshot.deviceStatus;
357
+ logger.debug(`Successfully toggled device: ${this.deviceName} to ${this.deviceStatus}`);
358
+ return true;
359
+ }
360
+ logger.error(`Failed to toggle device: ${this.deviceName}`);
361
+ return false;
362
+ }
363
+
364
+ private async toggleIndicatorLight(turnOn: boolean): Promise<boolean> {
365
+ const [response] = await this.callDimmerCommand('dimmerIndicatorLightCtl', {
366
+ status: turnOn ? 'on' : 'off'
367
+ }, 'dimmerIndicatorLightCtl');
368
+
369
+ if (response?.code === 0) {
370
+ this.stateSnapshot.indicatorStatus = turnOn ? 'on' : 'off';
371
+ logger.debug(`Successfully toggled indicator light for device: ${this.deviceName}`);
372
+ return true;
373
+ }
374
+ logger.error(`Failed to toggle indicator light for device: ${this.deviceName}`);
375
+ return false;
376
+ }
377
+
378
+ private buildDimmerRequest(command: string, data: Record<string, any> = {}): Record<string, any> {
379
+ const manager = this.manager as unknown as { accountId?: string | null; token?: string | null; timeZone?: string; countryCode?: string | null; };
380
+ return {
381
+ acceptLanguage: 'en',
382
+ accountID: manager.accountId ?? '',
383
+ appVersion: APP_VERSION,
384
+ phoneBrand: PHONE_BRAND,
385
+ phoneOS: PHONE_OS,
386
+ cid: this.cid,
387
+ configModule: this.configModule,
388
+ configModel: this.configModule,
389
+ debugMode: false,
390
+ traceId: Date.now().toString(),
391
+ timeZone: manager.timeZone ?? DEFAULT_TZ,
392
+ token: manager.token ?? '',
393
+ userCountryCode: manager.countryCode ?? DEFAULT_REGION,
394
+ uuid: this.uuid,
395
+ deviceId: this.cid,
396
+ mobileId: MOBILE_ID,
397
+ method: command,
398
+ payload: {
399
+ method: command,
400
+ source: 'APP',
401
+ data: {
402
+ uuid: this.uuid,
403
+ ...data
404
+ }
405
+ }
406
+ };
407
+ }
408
+
409
+ private async callDimmerCommand(command: string, data: Record<string, any>, endpoint: string): Promise<[any, number]> {
410
+ const request = this.buildDimmerRequest(command, data);
411
+ return await this.callApi(
412
+ `/cloud/v1/deviceManaged/${endpoint}`,
413
+ 'post',
414
+ request,
415
+ Helpers.reqHeaderBypass()
416
+ );
450
417
  }
451
418
  }
452
419