tsvesync 1.3.10 → 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.
@@ -29,10 +29,7 @@ export declare class VeSyncWallSwitch extends VeSyncSwitch {
29
29
  * Dimmer Switch Implementation (ESWD16)
30
30
  */
31
31
  export declare class VeSyncDimmerSwitch extends VeSyncSwitch {
32
- private _brightness;
33
- private _rgbValue;
34
- private _rgbStatus;
35
- private _indicatorLight;
32
+ private stateSnapshot;
36
33
  constructor(details: Record<string, any>, manager: VeSync);
37
34
  /**
38
35
  * Get dimmer switch details
@@ -78,5 +75,9 @@ export declare class VeSyncDimmerSwitch extends VeSyncSwitch {
78
75
  get indicatorLightStatus(): string;
79
76
  get rgbLightStatus(): string;
80
77
  get rgbLightValue(): Record<string, number>;
78
+ private toggleSwitch;
79
+ private toggleIndicatorLight;
80
+ private buildDimmerRequest;
81
+ private callDimmerCommand;
81
82
  }
82
83
  export declare const switchModules: Record<string, new (details: Record<string, any>, manager: VeSync) => VeSyncSwitch>;
@@ -101,39 +101,43 @@ exports.VeSyncWallSwitch = VeSyncWallSwitch;
101
101
  class VeSyncDimmerSwitch extends vesyncSwitch_1.VeSyncSwitch {
102
102
  constructor(details, manager) {
103
103
  super(details, manager);
104
- this._brightness = 0;
105
- this._rgbValue = { red: 0, blue: 0, green: 0 };
106
- this._rgbStatus = 'unknown';
107
- this._indicatorLight = 'unknown';
104
+ this.stateSnapshot = {
105
+ activeTime: 0,
106
+ connectionStatus: 'offline',
107
+ brightness: 0,
108
+ rgbStatus: 'off',
109
+ indicatorStatus: 'off',
110
+ rgbValue: { red: 0, green: 0, blue: 0 },
111
+ deviceStatus: 'off'
112
+ };
108
113
  logger_1.logger.debug(`Initialized VeSyncDimmerSwitch device: ${this.deviceName}`);
109
114
  }
110
115
  /**
111
116
  * Get dimmer switch details
112
117
  */
113
118
  async getDetails() {
119
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
114
120
  logger_1.logger.debug(`Getting details for device: ${this.deviceName}`);
115
- const body = {
116
- ...helpers_1.Helpers.reqBody(this.manager, 'devicedetail'),
117
- uuid: this.uuid,
118
- method: 'devicedetail'
119
- };
120
- const [response, statusCode] = await this.callApi('/dimmer/v1/device/devicedetail', 'post', body, helpers_1.Helpers.reqHeaders(this.manager));
121
+ const [response, statusCode] = await this.callDimmerCommand('deviceDetail', {}, 'deviceDetail');
121
122
  const success = this.checkResponse([response, statusCode], 'getDetails');
122
123
  if (success && (response === null || response === void 0 ? void 0 : response.result)) {
123
124
  const result = response.result;
124
- this.deviceStatus = result.deviceStatus || this.deviceStatus;
125
- this.details.active_time = result.activeTime || 0;
126
- this.connectionStatus = result.connectionStatus || this.connectionStatus;
127
- this._brightness = result.brightness || this._brightness;
128
- this._rgbStatus = result.rgbStatus || this._rgbStatus;
129
- this._indicatorLight = result.indicatorlightStatus || this._indicatorLight;
130
- if (result.rgbValue) {
131
- this._rgbValue = {
132
- red: result.rgbValue.red || 0,
133
- green: result.rgbValue.green || 0,
134
- blue: result.rgbValue.blue || 0
135
- };
136
- }
125
+ this.stateSnapshot = {
126
+ activeTime: (_a = result.activeTime) !== null && _a !== void 0 ? _a : this.stateSnapshot.activeTime,
127
+ connectionStatus: (_b = result.connectionStatus) !== null && _b !== void 0 ? _b : this.stateSnapshot.connectionStatus,
128
+ brightness: Number((_c = result.brightness) !== null && _c !== void 0 ? _c : this.stateSnapshot.brightness),
129
+ rgbStatus: (_d = result.rgbStatus) !== null && _d !== void 0 ? _d : this.stateSnapshot.rgbStatus,
130
+ indicatorStatus: (_e = result.indicatorlightStatus) !== null && _e !== void 0 ? _e : this.stateSnapshot.indicatorStatus,
131
+ rgbValue: {
132
+ red: Number((_g = (_f = result.rgbValue) === null || _f === void 0 ? void 0 : _f.red) !== null && _g !== void 0 ? _g : this.stateSnapshot.rgbValue.red),
133
+ green: Number((_j = (_h = result.rgbValue) === null || _h === void 0 ? void 0 : _h.green) !== null && _j !== void 0 ? _j : this.stateSnapshot.rgbValue.green),
134
+ blue: Number((_l = (_k = result.rgbValue) === null || _k === void 0 ? void 0 : _k.blue) !== null && _l !== void 0 ? _l : this.stateSnapshot.rgbValue.blue)
135
+ },
136
+ deviceStatus: (_m = result.deviceStatus) !== null && _m !== void 0 ? _m : this.stateSnapshot.deviceStatus
137
+ };
138
+ this.details.active_time = this.stateSnapshot.activeTime;
139
+ this.connectionStatus = this.stateSnapshot.connectionStatus;
140
+ this.deviceStatus = this.stateSnapshot.deviceStatus;
137
141
  logger_1.logger.debug(`Successfully got details for device: ${this.deviceName}`);
138
142
  }
139
143
  return success;
@@ -161,39 +165,13 @@ class VeSyncDimmerSwitch extends vesyncSwitch_1.VeSyncSwitch {
161
165
  * Turn off dimmer switch
162
166
  */
163
167
  async turnOff() {
164
- logger_1.logger.info(`Turning off device: ${this.deviceName}`);
165
- const body = {
166
- ...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
167
- status: 'off',
168
- uuid: this.uuid
169
- };
170
- const [response] = await this.callApi('/dimmer/v1/device/devicestatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
171
- if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
172
- this.deviceStatus = 'off';
173
- logger_1.logger.debug(`Successfully turned off device: ${this.deviceName}`);
174
- return true;
175
- }
176
- logger_1.logger.error(`Failed to turn off device: ${this.deviceName}`);
177
- return false;
168
+ return await this.toggleSwitch(false);
178
169
  }
179
170
  /**
180
171
  * Turn on dimmer switch
181
172
  */
182
173
  async turnOn() {
183
- logger_1.logger.info(`Turning on device: ${this.deviceName}`);
184
- const body = {
185
- ...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
186
- status: 'on',
187
- uuid: this.uuid
188
- };
189
- const [response] = await this.callApi('/dimmer/v1/device/devicestatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
190
- if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
191
- this.deviceStatus = 'on';
192
- logger_1.logger.debug(`Successfully turned on device: ${this.deviceName}`);
193
- return true;
194
- }
195
- logger_1.logger.error(`Failed to turn on device: ${this.deviceName}`);
196
- return false;
174
+ return await this.toggleSwitch(true);
197
175
  }
198
176
  /**
199
177
  * Set brightness level
@@ -204,18 +182,18 @@ class VeSyncDimmerSwitch extends vesyncSwitch_1.VeSyncSwitch {
204
182
  return false;
205
183
  }
206
184
  logger_1.logger.debug(`Setting brightness to ${brightness} for device: ${this.deviceName}`);
207
- const body = {
208
- ...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
209
- brightness,
210
- uuid: this.uuid
211
- };
212
- const [response] = await this.callApi('/dimmer/v1/device/updatebrightness', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
185
+ const value = Math.max(0, Math.min(100, Math.round(brightness)));
186
+ const [response] = await this.callDimmerCommand('dimmerBrightnessCtl', {
187
+ brightness: String(value)
188
+ }, 'dimmerBrightnessCtl');
213
189
  if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
214
- this._brightness = brightness;
215
- logger_1.logger.debug(`Successfully set brightness to ${brightness} for device: ${this.deviceName}`);
190
+ this.stateSnapshot.brightness = value;
191
+ this.stateSnapshot.deviceStatus = 'on';
192
+ this.deviceStatus = 'on';
193
+ logger_1.logger.debug(`Successfully set brightness to ${value} for device: ${this.deviceName}`);
216
194
  return true;
217
195
  }
218
- logger_1.logger.error(`Failed to set brightness to ${brightness} for device: ${this.deviceName}`);
196
+ logger_1.logger.error(`Failed to set brightness to ${value} for device: ${this.deviceName}`);
219
197
  return false;
220
198
  }
221
199
  /**
@@ -223,20 +201,17 @@ class VeSyncDimmerSwitch extends vesyncSwitch_1.VeSyncSwitch {
223
201
  */
224
202
  async rgbColorSet(red, green, blue) {
225
203
  logger_1.logger.debug(`Setting RGB color to (${red}, ${green}, ${blue}) for device: ${this.deviceName}`);
226
- const body = {
227
- ...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
204
+ const [response] = await this.callDimmerCommand('dimmerRgbValueCtl', {
205
+ status: 'on',
228
206
  rgbValue: {
229
207
  red: Math.round(red),
230
208
  green: Math.round(green),
231
209
  blue: Math.round(blue)
232
- },
233
- status: 'on',
234
- uuid: this.uuid
235
- };
236
- const [response] = await this.callApi('/dimmer/v1/device/devicergbstatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
210
+ }
211
+ }, 'dimmerRgbValueCtl');
237
212
  if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
238
- this._rgbValue = { red, green, blue };
239
- this._rgbStatus = 'on';
213
+ this.stateSnapshot.rgbValue = { red: Math.round(red), green: Math.round(green), blue: Math.round(blue) };
214
+ this.stateSnapshot.rgbStatus = 'on';
240
215
  logger_1.logger.debug(`Successfully set RGB color for device: ${this.deviceName}`);
241
216
  return true;
242
217
  }
@@ -248,14 +223,11 @@ class VeSyncDimmerSwitch extends vesyncSwitch_1.VeSyncSwitch {
248
223
  */
249
224
  async rgbColorOff() {
250
225
  logger_1.logger.debug(`Turning off RGB color for device: ${this.deviceName}`);
251
- const body = {
252
- ...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
253
- status: 'off',
254
- uuid: this.uuid
255
- };
256
- const [response] = await this.callApi('/dimmer/v1/device/devicergbstatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
226
+ const [response] = await this.callDimmerCommand('dimmerRgbValueCtl', {
227
+ status: 'off'
228
+ }, 'dimmerRgbValueCtl');
257
229
  if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
258
- this._rgbStatus = 'off';
230
+ this.stateSnapshot.rgbStatus = 'off';
259
231
  logger_1.logger.debug(`Successfully turned off RGB color for device: ${this.deviceName}`);
260
232
  return true;
261
233
  }
@@ -267,14 +239,11 @@ class VeSyncDimmerSwitch extends vesyncSwitch_1.VeSyncSwitch {
267
239
  */
268
240
  async rgbColorOn() {
269
241
  logger_1.logger.debug(`Turning on RGB color for device: ${this.deviceName}`);
270
- const body = {
271
- ...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
272
- uuid: this.uuid,
242
+ const [response] = await this.callDimmerCommand('dimmerRgbValueCtl', {
273
243
  status: 'on'
274
- };
275
- const [response] = await this.callApi('/dimmer/v1/device/devicergbstatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
244
+ }, 'dimmerRgbValueCtl');
276
245
  if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
277
- this._rgbStatus = 'on';
246
+ this.stateSnapshot.rgbStatus = 'on';
278
247
  logger_1.logger.debug(`Successfully turned on RGB color for device: ${this.deviceName}`);
279
248
  return true;
280
249
  }
@@ -286,51 +255,87 @@ class VeSyncDimmerSwitch extends vesyncSwitch_1.VeSyncSwitch {
286
255
  */
287
256
  async indicatorLightOn() {
288
257
  logger_1.logger.debug(`Turning on indicator light for device: ${this.deviceName}`);
289
- const body = {
290
- ...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
291
- uuid: this.uuid,
292
- status: 'on'
293
- };
294
- const [response] = await this.callApi('/dimmer/v1/device/indicatorlightstatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
295
- if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
296
- this._indicatorLight = 'on';
297
- logger_1.logger.debug(`Successfully turned on indicator light for device: ${this.deviceName}`);
298
- return true;
299
- }
300
- logger_1.logger.error(`Failed to turn on indicator light for device: ${this.deviceName}`);
301
- return false;
258
+ return this.toggleIndicatorLight(true);
302
259
  }
303
260
  /**
304
261
  * Turn indicator light off
305
262
  */
306
263
  async indicatorLightOff() {
307
264
  logger_1.logger.debug(`Turning off indicator light for device: ${this.deviceName}`);
308
- const body = {
309
- ...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
310
- uuid: this.uuid,
311
- status: 'off'
312
- };
313
- const [response] = await this.callApi('/dimmer/v1/device/indicatorlightstatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
314
- if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
315
- this._indicatorLight = 'off';
316
- logger_1.logger.debug(`Successfully turned off indicator light for device: ${this.deviceName}`);
317
- return true;
318
- }
319
- logger_1.logger.error(`Failed to turn off indicator light for device: ${this.deviceName}`);
320
- return false;
265
+ return this.toggleIndicatorLight(false);
321
266
  }
322
267
  // Getters
323
268
  get brightness() {
324
- return this._brightness;
269
+ return this.stateSnapshot.brightness;
325
270
  }
326
271
  get indicatorLightStatus() {
327
- return this._indicatorLight;
272
+ return this.stateSnapshot.indicatorStatus;
328
273
  }
329
274
  get rgbLightStatus() {
330
- return this._rgbStatus;
275
+ return this.stateSnapshot.rgbStatus;
331
276
  }
332
277
  get rgbLightValue() {
333
- return { ...this._rgbValue };
278
+ return { ...this.stateSnapshot.rgbValue };
279
+ }
280
+ async toggleSwitch(turnOn) {
281
+ const [response] = await this.callDimmerCommand('dimmerPowerSwitchCtl', {
282
+ status: turnOn ? 'on' : 'off'
283
+ }, 'dimmerPowerSwitchCtl');
284
+ if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
285
+ this.stateSnapshot.deviceStatus = turnOn ? 'on' : 'off';
286
+ this.deviceStatus = this.stateSnapshot.deviceStatus;
287
+ logger_1.logger.debug(`Successfully toggled device: ${this.deviceName} to ${this.deviceStatus}`);
288
+ return true;
289
+ }
290
+ logger_1.logger.error(`Failed to toggle device: ${this.deviceName}`);
291
+ return false;
292
+ }
293
+ async toggleIndicatorLight(turnOn) {
294
+ const [response] = await this.callDimmerCommand('dimmerIndicatorLightCtl', {
295
+ status: turnOn ? 'on' : 'off'
296
+ }, 'dimmerIndicatorLightCtl');
297
+ if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
298
+ this.stateSnapshot.indicatorStatus = turnOn ? 'on' : 'off';
299
+ logger_1.logger.debug(`Successfully toggled indicator light for device: ${this.deviceName}`);
300
+ return true;
301
+ }
302
+ logger_1.logger.error(`Failed to toggle indicator light for device: ${this.deviceName}`);
303
+ return false;
304
+ }
305
+ buildDimmerRequest(command, data = {}) {
306
+ var _a, _b, _c, _d;
307
+ const manager = this.manager;
308
+ return {
309
+ acceptLanguage: 'en',
310
+ accountID: (_a = manager.accountId) !== null && _a !== void 0 ? _a : '',
311
+ appVersion: helpers_1.APP_VERSION,
312
+ phoneBrand: helpers_1.PHONE_BRAND,
313
+ phoneOS: helpers_1.PHONE_OS,
314
+ cid: this.cid,
315
+ configModule: this.configModule,
316
+ configModel: this.configModule,
317
+ debugMode: false,
318
+ traceId: Date.now().toString(),
319
+ timeZone: (_b = manager.timeZone) !== null && _b !== void 0 ? _b : helpers_1.DEFAULT_TZ,
320
+ token: (_c = manager.token) !== null && _c !== void 0 ? _c : '',
321
+ userCountryCode: (_d = manager.countryCode) !== null && _d !== void 0 ? _d : helpers_1.DEFAULT_REGION,
322
+ uuid: this.uuid,
323
+ deviceId: this.cid,
324
+ mobileId: helpers_1.MOBILE_ID,
325
+ method: command,
326
+ payload: {
327
+ method: command,
328
+ source: 'APP',
329
+ data: {
330
+ uuid: this.uuid,
331
+ ...data
332
+ }
333
+ }
334
+ };
335
+ }
336
+ async callDimmerCommand(command, data, endpoint) {
337
+ const request = this.buildDimmerRequest(command, data);
338
+ return await this.callApi(`/cloud/v1/deviceManaged/${endpoint}`, 'post', request, helpers_1.Helpers.reqHeaderBypass());
334
339
  }
335
340
  }
336
341
  exports.VeSyncDimmerSwitch = VeSyncDimmerSwitch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsvesync",
3
- "version": "1.3.10",
3
+ "version": "1.3.11",
4
4
  "description": "A TypeScript library for interacting with VeSync smart home devices",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -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