tsvesync
Version:
A TypeScript library for interacting with VeSync smart home devices
249 lines (248 loc) • 9.96 kB
JavaScript
"use strict";
/**
* VeSync Outlet Implementations
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.outletModules = exports.VeSyncOutdoorPlug = exports.VeSyncOutlet15A = exports.VeSyncOutlet10A = exports.VeSyncOutlet7A = void 0;
const vesyncOutlet_1 = require("./vesyncOutlet");
const helpers_1 = require("./helpers");
const logger_1 = require("./logger");
/**
* VeSync 7A Outlet
*/
class VeSyncOutlet7A extends vesyncOutlet_1.VeSyncOutlet {
constructor(details, manager) {
super(details, manager);
}
/**
* Turn outlet on
*/
async turnOn() {
logger_1.logger.debug(`[${this.deviceName}] Turning outlet on`);
const url = `/v1/wifi-switch-1.3/${this.cid}/status/on`;
const [response, statusCode] = await this.callApi(url, 'put', {}, helpers_1.Helpers.reqHeaders(this.manager));
if (statusCode === 200) {
this.deviceStatus = 'on';
logger_1.logger.info(`[${this.deviceName}] Successfully turned outlet on`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to turn outlet on: ${JSON.stringify(response)}`);
return false;
}
/**
* Turn outlet off
*/
async turnOff() {
logger_1.logger.debug(`[${this.deviceName}] Turning outlet off`);
const url = `/v1/wifi-switch-1.3/${this.cid}/status/off`;
const [response, statusCode] = await this.callApi(url, 'put', {}, helpers_1.Helpers.reqHeaders(this.manager));
if (statusCode === 200) {
this.deviceStatus = 'off';
logger_1.logger.info(`[${this.deviceName}] Successfully turned outlet off`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to turn outlet off: ${JSON.stringify(response)}`);
return false;
}
}
exports.VeSyncOutlet7A = VeSyncOutlet7A;
/**
* VeSync 10A Outlet
*/
class VeSyncOutlet10A extends vesyncOutlet_1.VeSyncOutlet {
constructor(details, manager) {
super(details, manager);
}
/**
* Turn outlet on
*/
async turnOn() {
logger_1.logger.debug(`[${this.deviceName}] Turning outlet on`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
uuid: this.uuid,
status: 'on'
};
const [response] = await this.callApi('/10a/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.info(`[${this.deviceName}] Successfully turned outlet on`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to turn outlet on: ${JSON.stringify(response)}`);
return false;
}
/**
* Turn outlet off
*/
async turnOff() {
logger_1.logger.debug(`[${this.deviceName}] Turning outlet off`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
uuid: this.uuid,
status: 'off'
};
const [response] = await this.callApi('/10a/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.info(`[${this.deviceName}] Successfully turned outlet off`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to turn outlet off: ${JSON.stringify(response)}`);
return false;
}
}
exports.VeSyncOutlet10A = VeSyncOutlet10A;
/**
* VeSync 15A Outlet
*/
class VeSyncOutlet15A extends vesyncOutlet_1.VeSyncOutlet {
constructor(details, manager) {
super(details, manager);
}
/**
* Turn outlet on
*/
async turnOn() {
logger_1.logger.debug(`[${this.deviceName}] Turning outlet on`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
uuid: this.uuid,
status: 'on'
};
const [response] = await this.callApi('/15a/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.info(`[${this.deviceName}] Successfully turned outlet on`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to turn outlet on: ${JSON.stringify(response)}`);
return false;
}
/**
* Turn outlet off
*/
async turnOff() {
logger_1.logger.debug(`[${this.deviceName}] Turning outlet off`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
uuid: this.uuid,
status: 'off'
};
const [response] = await this.callApi('/15a/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.info(`[${this.deviceName}] Successfully turned outlet off`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to turn outlet off: ${JSON.stringify(response)}`);
return false;
}
/**
* Turn on nightlight
*/
async turnOnNightlight() {
if (!this.hasNightlight()) {
logger_1.logger.error(`[${this.deviceName}] Nightlight feature not supported`);
return false;
}
logger_1.logger.debug(`[${this.deviceName}] Turning nightlight on`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
uuid: this.uuid,
mode: 'auto'
};
const [response] = await this.callApi('/15a/v1/device/nightlightstatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.details.nightLightStatus = 'on';
logger_1.logger.info(`[${this.deviceName}] Successfully turned nightlight on`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to turn nightlight on: ${JSON.stringify(response)}`);
return false;
}
/**
* Turn off nightlight
*/
async turnOffNightlight() {
if (!this.hasNightlight()) {
logger_1.logger.error(`[${this.deviceName}] Nightlight feature not supported`);
return false;
}
logger_1.logger.debug(`[${this.deviceName}] Turning nightlight off`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
uuid: this.uuid,
mode: 'manual'
};
const [response] = await this.callApi('/15a/v1/device/nightlightstatus', 'put', body, helpers_1.Helpers.reqHeaders(this.manager));
if ((response === null || response === void 0 ? void 0 : response.code) === 0) {
this.details.nightLightStatus = 'off';
logger_1.logger.info(`[${this.deviceName}] Successfully turned nightlight off`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to turn nightlight off: ${JSON.stringify(response)}`);
return false;
}
}
exports.VeSyncOutlet15A = VeSyncOutlet15A;
/**
* VeSync Outdoor Plug
*/
class VeSyncOutdoorPlug extends vesyncOutlet_1.VeSyncOutlet {
constructor(details, manager) {
super(details, manager);
this.isSubDevice = details.isSubDevice || false;
this.subDeviceNo = details.subDeviceNo;
}
/**
* Turn outlet on
*/
async turnOn() {
logger_1.logger.debug(`[${this.deviceName}] Turning outlet on`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
uuid: this.uuid,
status: 'on',
switchNo: this.subDeviceNo
};
const [response] = await this.callApi('/outdoorsocket15a/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.info(`[${this.deviceName}] Successfully turned outlet on`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to turn outlet on: ${JSON.stringify(response)}`);
return false;
}
/**
* Turn outlet off
*/
async turnOff() {
logger_1.logger.debug(`[${this.deviceName}] Turning outlet off`);
const body = {
...helpers_1.Helpers.reqBody(this.manager, 'devicestatus'),
uuid: this.uuid,
status: 'off',
switchNo: this.subDeviceNo
};
const [response] = await this.callApi('/outdoorsocket15a/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.info(`[${this.deviceName}] Successfully turned outlet off`);
return true;
}
logger_1.logger.error(`[${this.deviceName}] Failed to turn outlet off: ${JSON.stringify(response)}`);
return false;
}
}
exports.VeSyncOutdoorPlug = VeSyncOutdoorPlug;
// Export outlet modules dictionary
exports.outletModules = {
'wifi-switch-1.3': VeSyncOutlet7A,
'ESW03-USA': VeSyncOutlet10A,
'ESW01-EU': VeSyncOutlet10A,
'ESW10-USA': VeSyncOutlet10A,
'ESW15-USA': VeSyncOutlet15A,
'ESO15-TB': VeSyncOutdoorPlug
};