tsvesync
Version:
A TypeScript library for interacting with VeSync smart home devices
297 lines (296 loc) • 10.3 kB
JavaScript
"use strict";
/**
* VeSync Bulb Base Class
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.VeSyncBulb = exports.bulbConfig = void 0;
const vesyncBaseDevice_1 = require("./vesyncBaseDevice");
const helpers_1 = require("./helpers");
const logger_1 = require("./logger");
// Bulb configuration
exports.bulbConfig = {
'ESL100': {
module: 'VeSyncBulbESL100',
features: ['dimmable'],
colorModel: 'none'
},
'ESL100CW': {
module: 'VeSyncBulbESL100CW',
features: ['dimmable', 'color_temp'],
colorModel: 'none'
},
'XYD0001': {
module: 'VeSyncBulbXYD0001',
features: ['dimmable', 'color_temp', 'rgb_shift'],
colorModel: 'hsv'
},
'ESL100MC': {
module: 'VeSyncBulbESL100MC',
features: ['dimmable', 'rgb_shift'],
colorModel: 'rgb'
}
};
/**
* VeSync Bulb Base Class
*/
class VeSyncBulb extends vesyncBaseDevice_1.VeSyncBaseDevice {
constructor(details, manager) {
var _a;
super(details, manager);
this.brightness = 0;
this.colorTemp = 0;
this.colorValue = 0;
this.colorHue = 0;
this.colorSaturation = 0;
this.colorMode = '';
this.features = ((_a = exports.bulbConfig[this.deviceType]) === null || _a === void 0 ? void 0 : _a.features) || [];
this.rgbValues = {
red: 0,
green: 0,
blue: 0
};
}
/**
* Get bulb details
*/
async getDetails() {
logger_1.logger.debug(`[${this.deviceName}] Getting bulb details`);
const isV2Device = this.deviceType === 'XYD0001';
const endpoint = isV2Device ? '/cloud/v2/deviceManaged/bypassV2' : '/cloud/v1/deviceManaged/bypass';
const body = isV2Device ? {
...helpers_1.Helpers.reqBody(this.manager, 'bypassV2'),
cid: this.cid,
configModule: this.configModule,
payload: {
data: {},
method: 'getLightStatusV2',
source: 'APP'
}
} : {
...helpers_1.Helpers.reqBody(this.manager, 'bypass'),
cid: this.cid,
configModule: this.configModule,
jsonCmd: {
getLightStatus: 'get'
}
};
const [response, statusCode] = await this.callApi(endpoint, 'post', body, helpers_1.Helpers.reqHeaders(this.manager));
const success = this.checkResponse([response, statusCode], 'getDetails');
if (success && (response === null || response === void 0 ? void 0 : response.result)) {
this.deviceStatus = response.result.enabled ? 'on' : 'off';
this.brightness = response.result.brightness || this.brightness;
if (this.features.includes('color_temp')) {
this.colorTemp = response.result.colorTemp || this.colorTemp;
}
if (this.features.includes('rgb_shift')) {
if (exports.bulbConfig[this.deviceType].colorModel === 'rgb') {
this.rgbValues = {
red: response.result.red || this.rgbValues.red,
green: response.result.green || this.rgbValues.green,
blue: response.result.blue || this.rgbValues.blue
};
}
else {
this.colorHue = response.result.hue || this.colorHue;
this.colorSaturation = response.result.saturation || this.colorSaturation;
this.colorValue = response.result.value || this.colorValue;
}
}
logger_1.logger.debug(`[${this.deviceName}] Successfully retrieved bulb details`);
}
return success;
}
/**
* Update bulb details
*/
async update() {
logger_1.logger.debug(`[${this.deviceName}] Updating bulb information`);
const success = await this.getDetails();
logger_1.logger.info(`[${this.deviceName}] Successfully updated bulb information`);
return success;
}
/**
* Turn bulb on
*/
async turnOn() {
logger_1.logger.debug(`[${this.deviceName}] Turning bulb on`);
const isV2Device = this.deviceType === 'XYD0001';
const endpoint = isV2Device ? '/cloud/v2/deviceManaged/bypassV2' : '/cloud/v1/deviceManaged/bypass';
const body = isV2Device ? {
...helpers_1.Helpers.reqBody(this.manager, 'bypassV2'),
cid: this.cid,
configModule: this.configModule,
payload: {
data: {
enabled: true,
id: 0
},
method: 'setSwitch',
source: 'APP'
}
} : {
...helpers_1.Helpers.reqBody(this.manager, 'bypass'),
cid: this.cid,
configModule: this.configModule,
jsonCmd: {
light: {
action: 'on'
}
}
};
const [response] = await this.callApi(endpoint, 'post', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.deviceStatus = 'on';
logger_1.logger.info(`[${this.deviceName}] Successfully turned bulb on`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to turn bulb on: ${JSON.stringify(response)}`);
return false;
}
/**
* Turn bulb off
*/
async turnOff() {
logger_1.logger.debug(`[${this.deviceName}] Turning bulb off`);
const isV2Device = this.deviceType === 'XYD0001';
const endpoint = isV2Device ? '/cloud/v2/deviceManaged/bypassV2' : '/cloud/v1/deviceManaged/bypass';
const body = isV2Device ? {
...helpers_1.Helpers.reqBody(this.manager, 'bypassV2'),
cid: this.cid,
configModule: this.configModule,
payload: {
data: {
enabled: false,
id: 0
},
method: 'setSwitch',
source: 'APP'
}
} : {
...helpers_1.Helpers.reqBody(this.manager, 'bypass'),
cid: this.cid,
configModule: this.configModule,
jsonCmd: {
light: {
action: 'off'
}
}
};
const [response] = await this.callApi(endpoint, 'post', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.deviceStatus = 'off';
logger_1.logger.info(`[${this.deviceName}] Successfully turned bulb off`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to turn bulb off: ${JSON.stringify(response)}`);
return false;
}
/**
* Set bulb brightness
*/
async setBrightness(brightness) {
if (!this.features.includes('dimmable')) {
logger_1.logger.error(`[${this.deviceName}] Dimming not supported`);
return false;
}
logger_1.logger.debug(`[${this.deviceName}] Setting brightness to ${brightness}`);
const isV2Device = this.deviceType === 'XYD0001';
const endpoint = isV2Device ? '/cloud/v2/deviceManaged/bypassV2' : '/cloud/v1/deviceManaged/bypass';
const body = isV2Device ? {
...helpers_1.Helpers.reqBody(this.manager, 'bypassV2'),
cid: this.cid,
configModule: this.configModule,
payload: {
data: {
brightness: brightness,
colorMode: '',
colorTemp: '',
force: 0,
hue: '',
saturation: '',
value: ''
},
method: 'setLightStatusV2',
source: 'APP'
}
} : {
...helpers_1.Helpers.reqBody(this.manager, 'bypass'),
cid: this.cid,
configModule: this.configModule,
jsonCmd: {
light: {
brightness: brightness
}
}
};
const [response] = await this.callApi(endpoint, 'post', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.brightness = brightness;
logger_1.logger.info(`[${this.deviceName}] Successfully set brightness to ${brightness}`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to set brightness: ${JSON.stringify(response)}`);
return false;
}
/**
* Get bulb brightness
*/
getBrightness() {
return this.brightness;
}
/**
* Get color temperature in Kelvin
*/
getColorTempKelvin() {
if (!this.features.includes('color_temp')) {
return 0;
}
return ((6500 - 2700) * this.colorTemp / 100) + 2700;
}
/**
* Get color temperature in percent
*/
getColorTempPercent() {
if (!this.features.includes('color_temp')) {
return 0;
}
return this.colorTemp;
}
/**
* Get color hue
*/
getColorHue() {
if (!this.features.includes('rgb_shift')) {
return 0;
}
return this.colorHue;
}
/**
* Get color saturation
*/
getColorSaturation() {
if (!this.features.includes('rgb_shift')) {
return 0;
}
return this.colorSaturation;
}
/**
* Get color value
*/
getColorValue() {
if (!this.features.includes('rgb_shift')) {
return 0;
}
return this.colorValue;
}
/**
* Get RGB values
*/
getRGBValues() {
if (!this.features.includes('rgb_shift') || exports.bulbConfig[this.deviceType].colorModel !== 'rgb') {
return { red: 0, green: 0, blue: 0 };
}
return this.rgbValues;
}
}
exports.VeSyncBulb = VeSyncBulb;