tsvesync
Version:
A TypeScript library for interacting with VeSync smart home devices
343 lines (342 loc) • 14.8 kB
JavaScript
"use strict";
/**
* VeSync Switch Implementations
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.switchModules = exports.VeSyncDimmerSwitch = exports.VeSyncWallSwitch = void 0;
const vesyncSwitch_1 = require("./vesyncSwitch");
const helpers_1 = require("./helpers");
const logger_1 = require("./logger");
/**
* Basic Wall Switch Implementation (ESWL01, ESWL03)
*/
class VeSyncWallSwitch extends vesyncSwitch_1.VeSyncSwitch {
constructor(details, manager) {
super(details, manager);
logger_1.logger.debug(`Initialized VeSyncWallSwitch device: ${this.deviceName}`);
}
/**
* Get wall switch details
*/
async getDetails() {
logger_1.logger.debug(`Getting details for device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicedetail'),
uuid: this.cid,
method: 'devicedetail'
};
const [response, statusCode] = await this.callApi('/inwallswitch/v1/device/devicedetail', '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)) {
const result = response.result;
this.deviceStatus = result.deviceStatus || this.deviceStatus;
this.details.active_time = result.activeTime || 0;
this.connectionStatus = result.connectionStatus || this.connectionStatus;
logger_1.logger.debug(`Successfully got details for device: ${this.deviceName}`);
}
return success;
}
/**
* Get switch device configuration info
*/
async getConfig() {
logger_1.logger.debug(`Getting configuration for device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicedetail'),
method: 'configurations',
uuid: this.cid
};
const [response] = await this.callApi('/inwallswitch/v1/device/configurations', 'post', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.config = helpers_1.Helpers.buildConfigDict(response);
logger_1.logger.debug(`Successfully got configuration for device: ${this.deviceName}`);
}
else {
logger_1.logger.error(`Failed to get configuration for device: ${this.deviceName}`);
}
}
/**
* Turn off wall switch
*/
async turnOff() {
logger_1.logger.info(`Turning off device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
status: 'off',
uuid: this.cid
};
const [response] = await this.callApi('/inwallswitch/v1/device/devicestatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.deviceStatus = 'off';
logger_1.logger.debug(`Successfully turned off device: ${this.deviceName}`);
return true;
}
logger_1.logger.error(`Failed to turn off device: ${this.deviceName}`);
return false;
}
/**
* Turn on wall switch
*/
async turnOn() {
logger_1.logger.info(`Turning on device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
status: 'on',
uuid: this.cid
};
const [response] = await this.callApi('/inwallswitch/v1/device/devicestatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.deviceStatus = 'on';
logger_1.logger.debug(`Successfully turned on device: ${this.deviceName}`);
return true;
}
logger_1.logger.error(`Failed to turn on device: ${this.deviceName}`);
return false;
}
}
exports.VeSyncWallSwitch = VeSyncWallSwitch;
/**
* Dimmer Switch Implementation (ESWD16)
*/
class VeSyncDimmerSwitch extends vesyncSwitch_1.VeSyncSwitch {
constructor(details, manager) {
super(details, manager);
this._brightness = 0;
this._rgbValue = { red: 0, blue: 0, green: 0 };
this._rgbStatus = 'unknown';
this._indicatorLight = 'unknown';
logger_1.logger.debug(`Initialized VeSyncDimmerSwitch device: ${this.deviceName}`);
}
/**
* Get dimmer switch details
*/
async getDetails() {
logger_1.logger.debug(`Getting details for device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicedetail'),
uuid: this.cid,
method: 'devicedetail'
};
const [response, statusCode] = await this.callApi('/dimmer/v1/device/devicedetail', '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)) {
const result = response.result;
this.deviceStatus = result.deviceStatus || this.deviceStatus;
this.details.active_time = result.activeTime || 0;
this.connectionStatus = result.connectionStatus || this.connectionStatus;
this._brightness = result.brightness || this._brightness;
this._rgbStatus = result.rgbStatus || this._rgbStatus;
this._indicatorLight = result.indicatorlightStatus || this._indicatorLight;
if (result.rgbValue) {
this._rgbValue = {
red: result.rgbValue.red || 0,
green: result.rgbValue.green || 0,
blue: result.rgbValue.blue || 0
};
}
logger_1.logger.debug(`Successfully got details for device: ${this.deviceName}`);
}
return success;
}
/**
* Get dimmer switch configuration info
*/
async getConfig() {
logger_1.logger.debug(`Getting configuration for device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicedetail'),
method: 'configurations',
uuid: this.cid
};
const [response] = await this.callApi('/dimmer/v1/device/configurations', 'post', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.config = helpers_1.Helpers.buildConfigDict(response);
logger_1.logger.debug(`Successfully got configuration for device: ${this.deviceName}`);
}
else {
logger_1.logger.error(`Failed to get configuration for device: ${this.deviceName}`);
}
}
/**
* Turn off dimmer switch
*/
async turnOff() {
logger_1.logger.info(`Turning off device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
status: 'off',
uuid: this.cid
};
const [response] = await this.callApi('/dimmer/v1/device/devicestatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.deviceStatus = 'off';
logger_1.logger.debug(`Successfully turned off device: ${this.deviceName}`);
return true;
}
logger_1.logger.error(`Failed to turn off device: ${this.deviceName}`);
return false;
}
/**
* Turn on dimmer switch
*/
async turnOn() {
logger_1.logger.info(`Turning on device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
status: 'on',
uuid: this.cid
};
const [response] = await this.callApi('/dimmer/v1/device/devicestatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.deviceStatus = 'on';
logger_1.logger.debug(`Successfully turned on device: ${this.deviceName}`);
return true;
}
logger_1.logger.error(`Failed to turn on device: ${this.deviceName}`);
return false;
}
/**
* Set brightness level
*/
async setBrightness(brightness) {
if (!this.isDimmable()) {
logger_1.logger.error(`Device ${this.deviceName} does not support dimming`);
return false;
}
logger_1.logger.debug(`Setting brightness to ${brightness} for device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
brightness: brightness.toString(),
uuid: this.cid
};
const [response] = await this.callApi('/dimmer/v1/device/updatebrightness', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this._brightness = brightness;
logger_1.logger.debug(`Successfully set brightness to ${brightness} for device: ${this.deviceName}`);
return true;
}
logger_1.logger.error(`Failed to set brightness to ${brightness} for device: ${this.deviceName}`);
return false;
}
/**
* Set RGB indicator color
*/
async rgbColorSet(red, green, blue) {
logger_1.logger.debug(`Setting RGB color to (${red}, ${green}, ${blue}) for device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
rgbValue: {
red: Math.round(red),
green: Math.round(green),
blue: Math.round(blue)
},
status: 'on',
uuid: this.cid
};
const [response] = await this.callApi('/dimmer/v1/device/devicergbstatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this._rgbValue = { red, green, blue };
this._rgbStatus = 'on';
logger_1.logger.debug(`Successfully set RGB color for device: ${this.deviceName}`);
return true;
}
logger_1.logger.error(`Failed to set RGB color for device: ${this.deviceName}`);
return false;
}
/**
* Turn on RGB indicator
*/
async rgbColorOff() {
logger_1.logger.debug(`Turning off RGB color for device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
status: 'off',
uuid: this.cid
};
const [response] = await this.callApi('/dimmer/v1/device/devicergbstatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this._rgbStatus = 'off';
logger_1.logger.debug(`Successfully turned off RGB color for device: ${this.deviceName}`);
return true;
}
logger_1.logger.error(`Failed to turn off RGB color for device: ${this.deviceName}`);
return false;
}
/**
* Turn RGB Color On
*/
async rgbColorOn() {
logger_1.logger.debug(`Turning on RGB color for device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
uuid: this.cid,
status: 'on'
};
const [response] = await this.callApi('/dimmer/v1/device/devicergbstatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this._rgbStatus = 'on';
logger_1.logger.debug(`Successfully turned on RGB color for device: ${this.deviceName}`);
return true;
}
logger_1.logger.error(`Failed to turn on RGB color for device: ${this.deviceName}`);
return false;
}
/**
* Turn indicator light on
*/
async indicatorLightOn() {
logger_1.logger.debug(`Turning on indicator light for device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
uuid: this.cid,
status: 'on'
};
const [response] = await this.callApi('/dimmer/v1/device/indicatorlightstatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this._indicatorLight = 'on';
logger_1.logger.debug(`Successfully turned on indicator light for device: ${this.deviceName}`);
return true;
}
logger_1.logger.error(`Failed to turn on indicator light for device: ${this.deviceName}`);
return false;
}
/**
* Turn indicator light off
*/
async indicatorLightOff() {
logger_1.logger.debug(`Turning off indicator light for device: ${this.deviceName}`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
uuid: this.cid,
status: 'off'
};
const [response] = await this.callApi('/dimmer/v1/device/indicatorlightstatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this._indicatorLight = 'off';
logger_1.logger.debug(`Successfully turned off indicator light for device: ${this.deviceName}`);
return true;
}
logger_1.logger.error(`Failed to turn off indicator light for device: ${this.deviceName}`);
return false;
}
// Getters
get brightness() {
return this._brightness;
}
get indicatorLightStatus() {
return this._indicatorLight;
}
get rgbLightStatus() {
return this._rgbStatus;
}
get rgbLightValue() {
return { ...this._rgbValue };
}
}
exports.VeSyncDimmerSwitch = VeSyncDimmerSwitch;
// Export switch modules dictionary
exports.switchModules = {
'ESWL01': VeSyncWallSwitch,
'ESWL03': VeSyncWallSwitch,
'ESWD16': VeSyncDimmerSwitch
};