UNPKG

tsvesync

Version:

A TypeScript library for interacting with VeSync smart home devices

170 lines (169 loc) 7.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VeSyncHumid200S = void 0; const humidifier_1 = require("./humidifier"); const helpers_1 = require("../helpers"); const logger_1 = require("../logger"); /** * VeSync Humid200S Humidifier Class * Implementation based on PyVeSync's VeSyncHumid200S class * For Classic200S models */ class VeSyncHumid200S extends humidifier_1.VeSyncHumidifier { constructor(details, manager) { super(details, manager); this.modes = ['auto', 'manual', 'sleep']; this.features = ['humidity', 'mist', 'display', 'timer', 'auto_mode']; this.mistLevels = [1, 2, 3]; this.humidityRange = { min: 30, max: 80 }; // Update config for Humid200S this.config = { module: 'VeSyncHumid200S', features: ['display', 'humidity', 'mist', 'timer', 'auto_mode'], levels: [1, 2, 3] }; logger_1.logger.debug(`Initialized VeSyncHumid200S device: ${this.deviceName}`); } /** * Get device details */ async getDetails() { var _a, _b, _c, _d; logger_1.logger.debug(`Getting details for device: ${this.deviceName}`); const [response, status] = await this.callApi('/cloud/v2/deviceManaged/bypassV2', 'post', { ...helpers_1.Helpers.reqBody(this.manager, 'bypassV2'), cid: this.cid, configModule: this.configModule, payload: { data: {}, method: 'getHumidifierStatus', source: 'APP' } }, helpers_1.Helpers.reqHeaderBypass()); const success = this.checkResponse([response, status], 'getDetails'); if (success && ((_a = response === null || response === void 0 ? void 0 : response.result) === null || _a === void 0 ? void 0 : _a.result)) { const result = response.result.result; // Update device status based on enabled field this.deviceStatus = result.enabled ? 'on' : 'off'; this.details = { mode: result.mode || '', humidity: result.humidity || 0, target_humidity: ((_b = result.configuration) === null || _b === void 0 ? void 0 : _b.auto_target_humidity) || result.target_humidity || 0, mist_level: result.mist_level || 0, water_lacks: result.water_lacks || false, humidity_high: result.humidity_high || false, water_tank_lifted: result.water_tank_lifted || false, display: result.display || false, automatic_stop: result.automatic_stop_reach_target || false, automatic_stop_configured: ((_c = result.configuration) === null || _c === void 0 ? void 0 : _c.automatic_stop) || false, auto_target_humidity: ((_d = result.configuration) === null || _d === void 0 ? void 0 : _d.auto_target_humidity) || 0, configuration: result.configuration || {} }; logger_1.logger.debug(`Successfully got details for device: ${this.deviceName}`); } return success; } /** * Set display * Override to use setIndicatorLightSwitch method */ async setDisplay(enabled) { logger_1.logger.info(`Setting display to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`); const [response, status] = await this.callApi('/cloud/v2/deviceManaged/bypassV2', 'post', { ...helpers_1.Helpers.reqBody(this.manager, 'bypassV2'), cid: this.cid, configModule: this.configModule, payload: { data: { enabled: enabled }, method: 'setIndicatorLightSwitch', source: 'APP' } }, helpers_1.Helpers.reqHeaderBypass()); const success = this.checkResponse([response, status], 'setDisplay'); if (success) { this.details.display = enabled; // Get latest state and log it await this.getDetails(); logger_1.logger.debug(`Device state after display change: ${JSON.stringify(this.details)}`); } else { logger_1.logger.error(`Failed to set display to ${enabled ? 'on' : 'off'} for device: ${this.deviceName}`); } return success; } /** * Set mist level */ async setMistLevel(level) { if (!this.mistLevels.includes(level)) { const error = `Invalid mist level: ${level}. Must be one of: ${this.mistLevels.join(', ')}`; logger_1.logger.error(`${error} for device: ${this.deviceName}`); throw new Error(error); } logger_1.logger.info(`Setting mist level to ${level} for device: ${this.deviceName}`); const [response, status] = await this.callApi('/cloud/v2/deviceManaged/bypassV2', 'post', { ...helpers_1.Helpers.reqBody(this.manager, 'bypassV2'), cid: this.cid, configModule: this.configModule, payload: { data: { level: level }, method: 'setMistLevel', // Use setMistLevel for Classic200S source: 'APP' } }, helpers_1.Helpers.reqHeaderBypass()); const success = this.checkResponse([response, status], 'setMistLevel'); if (success) { this.details.mist_level = level; // Get latest state and log it await this.getDetails(); logger_1.logger.debug(`Device state after mist level change: ${JSON.stringify(this.details)}`); } else { logger_1.logger.error(`Failed to set mist level to ${level} for device: ${this.deviceName}`); } return success; } /** * Get current humidity * Provides access to the current humidity reading */ get currentHumidity() { return this.details.humidity || 0; } /** * Get target humidity */ get targetHumidity() { return this.details.target_humidity || 0; } /** * Get water lacks status */ get waterLacks() { return this.details.water_lacks || false; } /** * Get water tank lifted status */ get waterTankLifted() { return this.details.water_tank_lifted || false; } /** * Return JSON details for humidifier * Override to include current humidity and target humidity */ displayJSON() { var _a, _b; const baseInfo = JSON.parse(super.displayJSON()); // Add current humidity from details baseInfo['Humidity'] = ((_a = this.details.humidity) === null || _a === void 0 ? void 0 : _a.toString()) || '0'; // Add target humidity from configuration baseInfo['Target Humidity'] = ((_b = this.details.target_humidity) === null || _b === void 0 ? void 0 : _b.toString()) || '0'; return JSON.stringify(baseInfo, null, 4); } } exports.VeSyncHumid200S = VeSyncHumid200S;