UNPKG

homebridge-tsvesync

Version:

Homebridge plugin for VeSync devices including Levoit air purifiers, humidifiers, and Etekcity smart outlets

68 lines 2.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SwitchAccessory = void 0; const base_accessory_1 = require("./base.accessory"); class SwitchAccessory extends base_accessory_1.BaseAccessory { constructor(platform, accessory, device) { super(platform, accessory, device); this.device = device; } setupService() { // Get the switch service if it exists, otherwise create a new switch service this.service = this.accessory.getService(this.platform.Service.Switch) || this.accessory.addService(this.platform.Service.Switch); // Set up handlers for the On/Off characteristic this.service.getCharacteristic(this.platform.Characteristic.On) .onSet(this.handleOnSet.bind(this)) .onGet(this.handleOnGet.bind(this)); // Add Name characteristic this.service.setCharacteristic(this.platform.Characteristic.Name, this.device.deviceName); } /** * Update device states based on the latest details */ async updateDeviceSpecificStates(details) { const isOn = details.deviceStatus === 'on'; this.updateCharacteristicValue(this.platform.Characteristic.On, isOn); } getDeviceCapabilities() { return { hasBrightness: false, hasColorTemp: false, hasColor: false, hasSpeed: false, hasHumidity: false, hasAirQuality: false, hasWaterLevel: false, hasChildLock: false, hasSwingMode: false, }; } async handleOnGet() { try { return this.device.power || this.device.deviceStatus === 'on'; } catch (error) { this.platform.log.error('Failed to get switch state:', error); throw error; } } async handleOnSet(value) { try { const isOn = value; const success = isOn ? await this.device.turnOn() : await this.device.turnOff(); if (!success) { throw new Error(`Failed to turn ${isOn ? 'on' : 'off'} switch`); } // Update the device state after successful change } catch (error) { this.platform.log.error('Failed to set switch state:', error); throw error; } } } exports.SwitchAccessory = SwitchAccessory; //# sourceMappingURL=switch.accessory.js.map