tsvesync
Version:
A TypeScript library for interacting with VeSync smart home devices
360 lines (359 loc) • 16.2 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.uuid,
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.uuid
};
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.uuid
};
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.uuid
};
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.stateSnapshot = {
activeTime: 0,
connectionStatus: 'offline',
brightness: 0,
rgbStatus: 'off',
indicatorStatus: 'off',
rgbValue: { red: 0, green: 0, blue: 0 },
deviceStatus: 'off'
};
logger_1.logger.debug(`Initialized VeSyncDimmerSwitch device: ${this.deviceName}`);
}
/**
* Get dimmer switch details
*/
async getDetails() {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
logger_1.logger.debug(`Getting details for device: ${this.deviceName}`);
const [response, statusCode] = await this.callDimmerCommand('deviceDetail', {});
const success = this.checkResponse([response, statusCode], 'getDetails');
const resultWrapper = (_b = (_a = response === null || response === void 0 ? void 0 : response.result) === null || _a === void 0 ? void 0 : _a.result) !== null && _b !== void 0 ? _b : response === null || response === void 0 ? void 0 : response.result;
if (success && resultWrapper) {
const result = resultWrapper;
this.stateSnapshot = {
activeTime: (_c = result.activeTime) !== null && _c !== void 0 ? _c : this.stateSnapshot.activeTime,
connectionStatus: (_d = result.connectionStatus) !== null && _d !== void 0 ? _d : this.stateSnapshot.connectionStatus,
brightness: Number((_e = result.brightness) !== null && _e !== void 0 ? _e : this.stateSnapshot.brightness),
rgbStatus: (_f = result.rgbStatus) !== null && _f !== void 0 ? _f : this.stateSnapshot.rgbStatus,
indicatorStatus: (_g = result.indicatorlightStatus) !== null && _g !== void 0 ? _g : this.stateSnapshot.indicatorStatus,
rgbValue: {
red: Number((_j = (_h = result.rgbValue) === null || _h === void 0 ? void 0 : _h.red) !== null && _j !== void 0 ? _j : this.stateSnapshot.rgbValue.red),
green: Number((_l = (_k = result.rgbValue) === null || _k === void 0 ? void 0 : _k.green) !== null && _l !== void 0 ? _l : this.stateSnapshot.rgbValue.green),
blue: Number((_o = (_m = result.rgbValue) === null || _m === void 0 ? void 0 : _m.blue) !== null && _o !== void 0 ? _o : this.stateSnapshot.rgbValue.blue)
},
deviceStatus: (_p = result.deviceStatus) !== null && _p !== void 0 ? _p : this.stateSnapshot.deviceStatus
};
this.details.active_time = this.stateSnapshot.activeTime;
this.connectionStatus = this.stateSnapshot.connectionStatus;
this.deviceStatus = this.stateSnapshot.deviceStatus;
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.uuid
};
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() {
return await this.toggleSwitch(false);
}
/**
* Turn on dimmer switch
*/
async turnOn() {
return await this.toggleSwitch(true);
}
/**
* 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 value = Math.max(0, Math.min(100, Math.round(brightness)));
const [response] = await this.callDimmerCommand('dimmerBrightnessCtl', {
brightness: value
});
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.stateSnapshot.brightness = value;
this.stateSnapshot.deviceStatus = 'on';
this.deviceStatus = 'on';
this.stateSnapshot.connectionStatus = 'online';
this.connectionStatus = 'online';
logger_1.logger.debug(`Successfully set brightness to ${value} for device: ${this.deviceName}`);
return true;
}
logger_1.logger.error(`Failed to set brightness to ${value} 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 [response] = await this.callDimmerCommand('dimmerRgbValueCtl', {
status: 'on',
rgbValue: {
red: Math.round(red),
green: Math.round(green),
blue: Math.round(blue)
}
});
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.stateSnapshot.rgbValue = { red: Math.round(red), green: Math.round(green), blue: Math.round(blue) };
this.stateSnapshot.rgbStatus = 'on';
this.stateSnapshot.deviceStatus = 'on';
this.deviceStatus = 'on';
this.stateSnapshot.connectionStatus = 'online';
this.connectionStatus = 'online';
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 [response] = await this.callDimmerCommand('dimmerRgbValueCtl', {
status: 'off'
});
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.stateSnapshot.rgbStatus = 'off';
this.stateSnapshot.deviceStatus = 'on';
this.deviceStatus = 'on';
this.stateSnapshot.connectionStatus = 'online';
this.connectionStatus = 'online';
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 [response] = await this.callDimmerCommand('dimmerRgbValueCtl', {
status: 'on'
});
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.stateSnapshot.rgbStatus = 'on';
this.stateSnapshot.deviceStatus = 'on';
this.deviceStatus = 'on';
this.stateSnapshot.connectionStatus = 'online';
this.connectionStatus = 'online';
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}`);
return this.toggleIndicatorLight(true);
}
/**
* Turn indicator light off
*/
async indicatorLightOff() {
logger_1.logger.debug(`Turning off indicator light for device: ${this.deviceName}`);
return this.toggleIndicatorLight(false);
}
// Getters
get brightness() {
return this.stateSnapshot.brightness;
}
get indicatorLightStatus() {
return this.stateSnapshot.indicatorStatus;
}
get rgbLightStatus() {
return this.stateSnapshot.rgbStatus;
}
get rgbLightValue() {
return { ...this.stateSnapshot.rgbValue };
}
async toggleSwitch(turnOn) {
const [response] = await this.callDimmerCommand('dimmerPowerSwitchCtl', {
status: turnOn ? 'on' : 'off'
});
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.stateSnapshot.deviceStatus = turnOn ? 'on' : 'off';
this.deviceStatus = this.stateSnapshot.deviceStatus;
this.stateSnapshot.connectionStatus = 'online';
this.connectionStatus = 'online';
logger_1.logger.debug(`Successfully toggled device: ${this.deviceName} to ${this.deviceStatus}`);
return true;
}
logger_1.logger.error(`Failed to toggle device: ${this.deviceName}`);
return false;
}
async toggleIndicatorLight(turnOn) {
const [response] = await this.callDimmerCommand('dimmerIndicatorLightCtl', {
status: turnOn ? 'on' : 'off'
});
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.stateSnapshot.indicatorStatus = turnOn ? 'on' : 'off';
this.stateSnapshot.connectionStatus = 'online';
this.connectionStatus = 'online';
logger_1.logger.debug(`Successfully toggled indicator light for device: ${this.deviceName}`);
return true;
}
logger_1.logger.error(`Failed to toggle indicator light for device: ${this.deviceName}`);
return false;
}
buildDimmerRequest(command, data = {}) {
var _a, _b, _c, _d;
const manager = this.manager;
const traceId = helpers_1.Helpers.generateTraceId();
return {
acceptLanguage: 'en',
accountID: (_a = manager.accountId) !== null && _a !== void 0 ? _a : '',
appVersion: helpers_1.APP_VERSION,
cid: this.cid,
configModule: this.configModule,
configModel: this.configModule,
debugMode: false,
deviceId: this.cid,
method: command,
phoneBrand: helpers_1.PHONE_BRAND,
phoneOS: helpers_1.PHONE_OS,
traceId,
timeZone: (_b = manager.timeZone) !== null && _b !== void 0 ? _b : helpers_1.DEFAULT_TZ,
token: (_c = manager.token) !== null && _c !== void 0 ? _c : '',
userCountryCode: (_d = manager.countryCode) !== null && _d !== void 0 ? _d : helpers_1.DEFAULT_REGION,
uuid: this.uuid,
...data
};
}
async callDimmerCommand(command, data) {
const request = this.buildDimmerRequest(command, data);
return await this.callApi(`/cloud/v1/deviceManaged/${command}`, 'post', request, helpers_1.Helpers.reqHeaderBypass());
}
}
exports.VeSyncDimmerSwitch = VeSyncDimmerSwitch;
// Export switch modules dictionary
exports.switchModules = {
'ESWL01': VeSyncWallSwitch,
'ESWL03': VeSyncWallSwitch,
'ESWD16': VeSyncDimmerSwitch
};