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.
- package/dist/lib/vesyncBulb.d.ts +23 -6
- package/dist/lib/vesyncBulb.js +144 -32
- package/dist/lib/vesyncBulbImpl.d.ts +16 -1
- package/dist/lib/vesyncBulbImpl.js +296 -47
- package/dist/lib/vesyncSwitchImpl.d.ts +5 -4
- package/dist/lib/vesyncSwitchImpl.js +117 -112
- package/package.json +1 -1
- package/src/lib/vesyncBulb.ts +160 -34
- package/src/lib/vesyncBulbImpl.ts +404 -52
- package/src/lib/vesyncSwitchImpl.ts +138 -171
|
@@ -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.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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
|
|
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.
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
208
|
-
|
|
209
|
-
brightness
|
|
210
|
-
|
|
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.
|
|
215
|
-
|
|
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 ${
|
|
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
|
|
227
|
-
|
|
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
|
-
|
|
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.
|
|
239
|
-
this.
|
|
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
|
|
252
|
-
|
|
253
|
-
|
|
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.
|
|
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
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
269
|
+
return this.stateSnapshot.brightness;
|
|
325
270
|
}
|
|
326
271
|
get indicatorLightStatus() {
|
|
327
|
-
return this.
|
|
272
|
+
return this.stateSnapshot.indicatorStatus;
|
|
328
273
|
}
|
|
329
274
|
get rgbLightStatus() {
|
|
330
|
-
return this.
|
|
275
|
+
return this.stateSnapshot.rgbStatus;
|
|
331
276
|
}
|
|
332
277
|
get rgbLightValue() {
|
|
333
|
-
return { ...this.
|
|
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
package/src/lib/vesyncBulb.ts
CHANGED
|
@@ -7,6 +7,12 @@ import { VeSync } from './vesync';
|
|
|
7
7
|
import { Helpers } from './helpers';
|
|
8
8
|
import { logger } from './logger';
|
|
9
9
|
|
|
10
|
+
interface RgbValues {
|
|
11
|
+
red: number;
|
|
12
|
+
green: number;
|
|
13
|
+
blue: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
10
16
|
interface BulbConfig {
|
|
11
17
|
[key: string]: {
|
|
12
18
|
module: string;
|
|
@@ -50,7 +56,7 @@ export abstract class VeSyncBulb extends VeSyncBaseDevice {
|
|
|
50
56
|
protected colorSaturation: number;
|
|
51
57
|
protected colorMode: string;
|
|
52
58
|
protected features: string[];
|
|
53
|
-
protected rgbValues:
|
|
59
|
+
protected rgbValues: RgbValues;
|
|
54
60
|
|
|
55
61
|
constructor(details: Record<string, any>, manager: VeSync) {
|
|
56
62
|
super(details, manager);
|
|
@@ -68,6 +74,18 @@ export abstract class VeSyncBulb extends VeSyncBaseDevice {
|
|
|
68
74
|
};
|
|
69
75
|
}
|
|
70
76
|
|
|
77
|
+
hasFeature(feature: string): boolean {
|
|
78
|
+
return this.features.includes(feature);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
getColorModel(): string {
|
|
82
|
+
return bulbConfig[this.deviceType]?.colorModel ?? 'none';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
getBrightness(): number {
|
|
86
|
+
return this.brightness;
|
|
87
|
+
}
|
|
88
|
+
|
|
71
89
|
/**
|
|
72
90
|
* Get bulb details
|
|
73
91
|
*/
|
|
@@ -101,29 +119,7 @@ export abstract class VeSyncBulb extends VeSyncBaseDevice {
|
|
|
101
119
|
Helpers.reqHeaders(this.manager)
|
|
102
120
|
);
|
|
103
121
|
|
|
104
|
-
|
|
105
|
-
if (success && response?.result) {
|
|
106
|
-
this.deviceStatus = response.result.enabled ? 'on' : 'off';
|
|
107
|
-
this.brightness = response.result.brightness || this.brightness;
|
|
108
|
-
if (this.features.includes('color_temp')) {
|
|
109
|
-
this.colorTemp = response.result.colorTemp || this.colorTemp;
|
|
110
|
-
}
|
|
111
|
-
if (this.features.includes('rgb_shift')) {
|
|
112
|
-
if (bulbConfig[this.deviceType].colorModel === 'rgb') {
|
|
113
|
-
this.rgbValues = {
|
|
114
|
-
red: response.result.red || this.rgbValues.red,
|
|
115
|
-
green: response.result.green || this.rgbValues.green,
|
|
116
|
-
blue: response.result.blue || this.rgbValues.blue
|
|
117
|
-
};
|
|
118
|
-
} else {
|
|
119
|
-
this.colorHue = response.result.hue || this.colorHue;
|
|
120
|
-
this.colorSaturation = response.result.saturation || this.colorSaturation;
|
|
121
|
-
this.colorValue = response.result.value || this.colorValue;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
logger.debug(`[${this.deviceName}] Successfully retrieved bulb details`);
|
|
125
|
-
}
|
|
126
|
-
return success;
|
|
122
|
+
return this.processStandardBypassDetails(response, statusCode);
|
|
127
123
|
}
|
|
128
124
|
|
|
129
125
|
/**
|
|
@@ -239,7 +235,8 @@ export abstract class VeSyncBulb extends VeSyncBaseDevice {
|
|
|
239
235
|
return false;
|
|
240
236
|
}
|
|
241
237
|
|
|
242
|
-
|
|
238
|
+
const brightnessLevel = VeSyncBulb.clamp(Math.round(brightness), 0, 100);
|
|
239
|
+
logger.debug(`[${this.deviceName}] Setting brightness to ${brightnessLevel}`);
|
|
243
240
|
|
|
244
241
|
const isV2Device = this.deviceType === 'XYD0001';
|
|
245
242
|
const endpoint = isV2Device ? '/cloud/v2/deviceManaged/bypassV2' : '/cloud/v1/deviceManaged/bypass';
|
|
@@ -249,7 +246,7 @@ export abstract class VeSyncBulb extends VeSyncBaseDevice {
|
|
|
249
246
|
configModule: this.configModule,
|
|
250
247
|
payload: {
|
|
251
248
|
data: {
|
|
252
|
-
brightness:
|
|
249
|
+
brightness: brightnessLevel,
|
|
253
250
|
colorMode: '',
|
|
254
251
|
colorTemp: '',
|
|
255
252
|
force: 0,
|
|
@@ -266,7 +263,7 @@ export abstract class VeSyncBulb extends VeSyncBaseDevice {
|
|
|
266
263
|
configModule: this.configModule,
|
|
267
264
|
jsonCmd: {
|
|
268
265
|
light: {
|
|
269
|
-
brightness:
|
|
266
|
+
brightness: brightnessLevel
|
|
270
267
|
}
|
|
271
268
|
}
|
|
272
269
|
};
|
|
@@ -279,8 +276,8 @@ export abstract class VeSyncBulb extends VeSyncBaseDevice {
|
|
|
279
276
|
);
|
|
280
277
|
|
|
281
278
|
if (response?.code === 0) {
|
|
282
|
-
this.brightness =
|
|
283
|
-
logger.info(`[${this.deviceName}] Successfully set brightness to ${
|
|
279
|
+
this.brightness = brightnessLevel;
|
|
280
|
+
logger.info(`[${this.deviceName}] Successfully set brightness to ${brightnessLevel}`);
|
|
284
281
|
return true;
|
|
285
282
|
}
|
|
286
283
|
logger.error(`[${this.deviceName}] Failed to set brightness: ${JSON.stringify(response)}`);
|
|
@@ -290,10 +287,6 @@ export abstract class VeSyncBulb extends VeSyncBaseDevice {
|
|
|
290
287
|
/**
|
|
291
288
|
* Get bulb brightness
|
|
292
289
|
*/
|
|
293
|
-
getBrightness(): number {
|
|
294
|
-
return this.brightness;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
290
|
/**
|
|
298
291
|
* Get color temperature in Kelvin
|
|
299
292
|
*/
|
|
@@ -354,8 +347,141 @@ export abstract class VeSyncBulb extends VeSyncBaseDevice {
|
|
|
354
347
|
return this.rgbValues;
|
|
355
348
|
}
|
|
356
349
|
|
|
350
|
+
/**
|
|
351
|
+
* Set color temperature wrapper to maintain compatibility with consumers
|
|
352
|
+
*/
|
|
353
|
+
async setColorTemperature(colorTemp: number): Promise<boolean> {
|
|
354
|
+
return this.setColorTemp(colorTemp);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Set color via hue/saturation/value when supported
|
|
359
|
+
*/
|
|
360
|
+
async setColor(hue: number, saturation: number, value: number = 100): Promise<boolean> {
|
|
361
|
+
if (!this.features.includes('rgb_shift')) {
|
|
362
|
+
logger.error(`[${this.deviceName}] Color control not supported`);
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const colorModel = bulbConfig[this.deviceType]?.colorModel;
|
|
367
|
+
if (colorModel === 'rgb' && typeof (this as any).setRgb === 'function') {
|
|
368
|
+
const rgb = VeSyncBulb.hsvToRgb(hue, saturation, value);
|
|
369
|
+
return (this as any).setRgb(rgb.red, rgb.green, rgb.blue);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (typeof (this as any).setHsv === 'function') {
|
|
373
|
+
return (this as any).setHsv(hue, saturation, value);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
logger.error(`[${this.deviceName}] No compatible color handler found`);
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Process standard bypass (v1) detail responses shared by multiple bulbs
|
|
382
|
+
*/
|
|
383
|
+
protected processStandardBypassDetails(response: any, statusCode: number): boolean {
|
|
384
|
+
const success = this.checkResponse([response, statusCode], 'getDetails');
|
|
385
|
+
if (success && response?.result) {
|
|
386
|
+
const innerResult = response.result?.result || response.result;
|
|
387
|
+
if (innerResult) {
|
|
388
|
+
if (typeof innerResult.enabled === 'boolean') {
|
|
389
|
+
this.deviceStatus = innerResult.enabled ? 'on' : 'off';
|
|
390
|
+
} else if (innerResult.action) {
|
|
391
|
+
this.deviceStatus = innerResult.action === 'on' ? 'on' : 'off';
|
|
392
|
+
}
|
|
393
|
+
if (innerResult.connectionStatus) {
|
|
394
|
+
this.connectionStatus = innerResult.connectionStatus;
|
|
395
|
+
}
|
|
396
|
+
if (this.features.includes('dimmable') && innerResult.brightness !== undefined) {
|
|
397
|
+
this.brightness = Number(innerResult.brightness);
|
|
398
|
+
}
|
|
399
|
+
if (this.features.includes('color_temp')) {
|
|
400
|
+
if (innerResult.colorTemp !== undefined) {
|
|
401
|
+
this.colorTemp = Number(innerResult.colorTemp);
|
|
402
|
+
} else if (innerResult.colorTempe !== undefined) {
|
|
403
|
+
this.colorTemp = Number(innerResult.colorTempe);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
if (this.features.includes('rgb_shift')) {
|
|
407
|
+
if (bulbConfig[this.deviceType]?.colorModel === 'rgb') {
|
|
408
|
+
this.rgbValues = {
|
|
409
|
+
red: Number(innerResult.red ?? this.rgbValues.red),
|
|
410
|
+
green: Number(innerResult.green ?? this.rgbValues.green),
|
|
411
|
+
blue: Number(innerResult.blue ?? this.rgbValues.blue)
|
|
412
|
+
};
|
|
413
|
+
} else {
|
|
414
|
+
this.colorHue = Number(innerResult.hue ?? this.colorHue);
|
|
415
|
+
this.colorSaturation = Number(innerResult.saturation ?? this.colorSaturation);
|
|
416
|
+
this.colorValue = Number(innerResult.value ?? this.colorValue);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
logger.debug(`[${this.deviceName}] Successfully retrieved bulb details`);
|
|
421
|
+
}
|
|
422
|
+
return success;
|
|
423
|
+
}
|
|
424
|
+
|
|
357
425
|
/**
|
|
358
426
|
* Set color temperature - Abstract method to be implemented by subclasses
|
|
359
427
|
*/
|
|
360
428
|
abstract setColorTemp(colorTemp: number): Promise<boolean>;
|
|
361
|
-
|
|
429
|
+
|
|
430
|
+
protected static clamp(value: number, min: number, max: number): number {
|
|
431
|
+
return Math.min(Math.max(value, min), max);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
protected static hsvToRgb(h: number, s: number, v: number): RgbValues {
|
|
435
|
+
const saturation = VeSyncBulb.clamp(s, 0, 100) / 100;
|
|
436
|
+
const value = VeSyncBulb.clamp(v, 0, 100) / 100;
|
|
437
|
+
const hue = ((h % 360) + 360) % 360 / 60;
|
|
438
|
+
const i = Math.floor(hue);
|
|
439
|
+
const f = hue - i;
|
|
440
|
+
const p = value * (1 - saturation);
|
|
441
|
+
const q = value * (1 - saturation * f);
|
|
442
|
+
const t = value * (1 - saturation * (1 - f));
|
|
443
|
+
|
|
444
|
+
let r = 0;
|
|
445
|
+
let g = 0;
|
|
446
|
+
let b = 0;
|
|
447
|
+
|
|
448
|
+
switch (i) {
|
|
449
|
+
case 0:
|
|
450
|
+
r = value;
|
|
451
|
+
g = t;
|
|
452
|
+
b = p;
|
|
453
|
+
break;
|
|
454
|
+
case 1:
|
|
455
|
+
r = q;
|
|
456
|
+
g = value;
|
|
457
|
+
b = p;
|
|
458
|
+
break;
|
|
459
|
+
case 2:
|
|
460
|
+
r = p;
|
|
461
|
+
g = value;
|
|
462
|
+
b = t;
|
|
463
|
+
break;
|
|
464
|
+
case 3:
|
|
465
|
+
r = p;
|
|
466
|
+
g = q;
|
|
467
|
+
b = value;
|
|
468
|
+
break;
|
|
469
|
+
case 4:
|
|
470
|
+
r = t;
|
|
471
|
+
g = p;
|
|
472
|
+
b = value;
|
|
473
|
+
break;
|
|
474
|
+
default:
|
|
475
|
+
r = value;
|
|
476
|
+
g = p;
|
|
477
|
+
b = q;
|
|
478
|
+
break;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
return {
|
|
482
|
+
red: Math.round(r * 255),
|
|
483
|
+
green: Math.round(g * 255),
|
|
484
|
+
blue: Math.round(b * 255)
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
}
|