UNPKG

tsvesync

Version:

A TypeScript library for interacting with VeSync smart home devices

231 lines (230 loc) 9.28 kB
"use strict"; /** * VeSync Bulb Implementations */ Object.defineProperty(exports, "__esModule", { value: true }); exports.bulbModules = exports.VeSyncBulbESL100MC = exports.VeSyncBulbXYD0001 = exports.VeSyncBulbESL100CW = exports.VeSyncBulbESL100 = void 0; const vesyncBulb_1 = require("./vesyncBulb"); const helpers_1 = require("./helpers"); const logger_1 = require("./logger"); /** * ESL100 Bulb Implementation */ class VeSyncBulbESL100 extends vesyncBulb_1.VeSyncBulb { constructor(details, manager) { super(details, manager); } async setColorTemp(colorTemp) { logger_1.logger.error(`[${this.deviceName}] Color temperature control not supported`); return false; } } exports.VeSyncBulbESL100 = VeSyncBulbESL100; /** * ESL100CW Bulb Implementation */ class VeSyncBulbESL100CW extends vesyncBulb_1.VeSyncBulb { constructor(details, manager) { super(details, manager); } async setColorTemp(colorTemp) { if (!this.features.includes('color_temp')) { logger_1.logger.error(`[${this.deviceName}] Color temperature control not supported`); return false; } logger_1.logger.debug(`[${this.deviceName}] Setting color temperature to ${colorTemp}`); const body = { ...helpers_1.Helpers.reqBody(this.manager, 'bypass'), cid: this.cid, configModule: this.configModule, jsonCmd: { light: { colorTempe: colorTemp } } }; const [response] = await this.callApi('/cloud/v1/deviceManaged/bypass', 'post', body, helpers_1.Helpers.reqHeaders(this.manager)); if ((response === null || response === void 0 ? void 0 : response.code) === 0) { this.colorTemp = colorTemp; logger_1.logger.info(`[${this.deviceName}] Successfully set color temperature to ${colorTemp}`); return true; } logger_1.logger.error(`[${this.deviceName}] Failed to set color temperature: ${JSON.stringify(response)}`); return false; } } exports.VeSyncBulbESL100CW = VeSyncBulbESL100CW; /** * XYD0001 Bulb Implementation */ class VeSyncBulbXYD0001 extends vesyncBulb_1.VeSyncBulb { constructor(details, manager) { super(details, manager); } async setColorTemp(colorTemp) { if (!this.features.includes('color_temp')) { logger_1.logger.error(`[${this.deviceName}] Color temperature control not supported`); return false; } logger_1.logger.debug(`[${this.deviceName}] Setting color temperature to ${colorTemp}`); const body = { ...helpers_1.Helpers.reqBody(this.manager, 'bypassV2'), cid: this.cid, configModule: this.configModule, payload: { data: { brightness: '', colorMode: 'white', colorTemp: colorTemp, force: 1, hue: '', saturation: '', value: '' }, method: 'setLightStatusV2', source: 'APP' } }; const [response] = await this.callApi('/cloud/v2/deviceManaged/bypassV2', 'post', body, helpers_1.Helpers.reqHeaders(this.manager)); if ((response === null || response === void 0 ? void 0 : response.code) === 0) { this.colorTemp = colorTemp; logger_1.logger.info(`[${this.deviceName}] Successfully set color temperature to ${colorTemp}`); return true; } logger_1.logger.error(`[${this.deviceName}] Failed to set color temperature: ${JSON.stringify(response)}`); return false; } async setHsv(hue, saturation, value) { if (!this.features.includes('rgb_shift')) { logger_1.logger.error(`[${this.deviceName}] Color control not supported`); return false; } logger_1.logger.debug(`[${this.deviceName}] Setting HSV color to H:${hue} S:${saturation} V:${value}`); const body = { ...helpers_1.Helpers.reqBody(this.manager, 'bypassV2'), cid: this.cid, configModule: this.configModule, payload: { data: { brightness: value, colorMode: 'hsv', colorTemp: '', force: 1, hue: hue, saturation: saturation, value: value }, method: 'setLightStatusV2', source: 'APP' } }; const [response] = await this.callApi('/cloud/v2/deviceManaged/bypassV2', 'post', body, helpers_1.Helpers.reqHeaders(this.manager)); if ((response === null || response === void 0 ? void 0 : response.code) === 0) { this.colorHue = hue; this.colorSaturation = saturation; this.colorValue = value; logger_1.logger.info(`[${this.deviceName}] Successfully set HSV color`); return true; } logger_1.logger.error(`[${this.deviceName}] Failed to set HSV color: ${JSON.stringify(response)}`); return false; } async enableWhiteMode() { logger_1.logger.debug(`[${this.deviceName}] Enabling white mode`); const body = { ...helpers_1.Helpers.reqBody(this.manager, 'bypassV2'), cid: this.cid, configModule: this.configModule, payload: { data: { brightness: '', colorMode: 'white', colorTemp: '', force: 1, red: '', green: '', blue: '' }, method: 'setLightStatusV2', source: 'APP' } }; const [response] = await this.callApi('/cloud/v2/deviceManaged/bypassV2', 'post', body, helpers_1.Helpers.reqHeaders(this.manager)); if ((response === null || response === void 0 ? void 0 : response.code) === 0) { this.colorMode = 'white'; logger_1.logger.info(`[${this.deviceName}] Successfully enabled white mode`); return true; } logger_1.logger.error(`[${this.deviceName}] Failed to enable white mode: ${JSON.stringify(response)}`); return false; } } exports.VeSyncBulbXYD0001 = VeSyncBulbXYD0001; /** * ESL100MC Bulb Implementation */ class VeSyncBulbESL100MC extends vesyncBulb_1.VeSyncBulb { constructor(details, manager) { super(details, manager); } async setColorTemp(colorTemp) { logger_1.logger.error(`[${this.deviceName}] Color temperature control not supported`); return false; } async setRgb(red, green, blue) { if (!this.features.includes('rgb_shift')) { logger_1.logger.error(`[${this.deviceName}] Color control not supported`); return false; } logger_1.logger.debug(`[${this.deviceName}] Setting RGB color to R:${red} G:${green} B:${blue}`); const body = { ...helpers_1.Helpers.reqBody(this.manager, 'bypass'), cid: this.cid, configModule: this.configModule, jsonCmd: { light: { red: Math.round(red), green: Math.round(green), blue: Math.round(blue) } } }; const [response] = await this.callApi('/cloud/v1/deviceManaged/bypass', 'post', body, helpers_1.Helpers.reqHeaders(this.manager)); if ((response === null || response === void 0 ? void 0 : response.code) === 0) { this.rgbValues = { red, green, blue }; logger_1.logger.info(`[${this.deviceName}] Successfully set RGB color`); return true; } logger_1.logger.error(`[${this.deviceName}] Failed to set RGB color: ${JSON.stringify(response)}`); return false; } async enableWhiteMode() { logger_1.logger.debug(`[${this.deviceName}] Enabling white mode`); const body = { ...helpers_1.Helpers.reqBody(this.manager, 'bypass'), cid: this.cid, configModule: this.configModule, jsonCmd: { light: { mode: 'white' } } }; const [response] = await this.callApi('/cloud/v1/deviceManaged/bypass', 'post', body, helpers_1.Helpers.reqHeaders(this.manager)); if ((response === null || response === void 0 ? void 0 : response.code) === 0) { this.colorMode = 'white'; logger_1.logger.info(`[${this.deviceName}] Successfully enabled white mode`); return true; } logger_1.logger.error(`[${this.deviceName}] Failed to enable white mode: ${JSON.stringify(response)}`); return false; } } exports.VeSyncBulbESL100MC = VeSyncBulbESL100MC; // Export the bulb modules dictionary exports.bulbModules = { 'ESL100': VeSyncBulbESL100, 'ESL100CW': VeSyncBulbESL100CW, 'XYD0001': VeSyncBulbXYD0001, 'ESL100MC': VeSyncBulbESL100MC };