UNPKG

homebridge-virtual-accessories

Version:
121 lines 5.86 kB
import { Accessory } from './accessory.js'; /** * Fan - Accessory implementation */ export class Fan extends Accessory { static ACCESSORY_TYPE_NAME = 'Fan'; static ON = true; static OFF = false; static CLOCKWISE = 0; // Characteristic.ProgrammableSwitchEvent.RotationDirection.CLOCKWISE static COUNTER_CLOCKWISE = 1; // Characteristic.ProgrammableSwitchEvent.RotationDirection.COUNTER_CLOCKWISE stateStorageKey = 'FanState'; rotatioDirectionStorageKey = 'FanRotationDirection'; rotatioSpeedStorageKey = 'FanRotationSpeed'; states = { FanState: Fan.OFF, FanRotationDirection: Fan.CLOCKWISE, FanRotationSpeed: 100, }; constructor(platform, accessory, accessoryConfiguration) { super(platform, accessory, accessoryConfiguration); // First configure the device based on the accessory details const rotationDirection = this.accessoryConfiguration.fan.rotationDirection === 'clockwise' ? Fan.CLOCKWISE : Fan.COUNTER_CLOCKWISE; const rotationSpeed = this.accessoryConfiguration.fan.rotationSpeed; this.states.FanState = Fan.OFF; this.states.FanRotationDirection = rotationDirection; this.states.FanRotationSpeed = rotationSpeed; // If the accessory is stateful retrieve stored state if (this.accessoryConfiguration.accessoryIsStateful) { const accessoryState = this.loadAccessoryState(this.storagePath); const cachedState = accessoryState[this.stateStorageKey]; const cachedRotationDirection = accessoryState[this.rotatioDirectionStorageKey]; const cachedRotationSpeed = accessoryState[this.rotatioSpeedStorageKey]; if (cachedState !== undefined && cachedRotationDirection !== undefined && cachedRotationSpeed !== undefined) { this.states.FanState = cachedState; this.states.FanRotationDirection = cachedRotationDirection; this.states.FanRotationSpeed = cachedRotationSpeed; } } this.service = this.accessory.getService(this.platform.Service.Fan) || this.accessory.addService(this.platform.Service.Fan); this.service.setCharacteristic(this.platform.Characteristic.Name, this.accessoryConfiguration.accessoryName); // Update the initial state of the accessory this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Setting Fan Current State: ${Fan.getStateName(this.states.FanState)}`); this.service.updateCharacteristic(this.platform.Characteristic.On, (this.states.FanState)); this.service.updateCharacteristic(this.platform.Characteristic.RotationDirection, (this.states.FanRotationDirection)); this.service.updateCharacteristic(this.platform.Characteristic.RotationSpeed, (this.states.FanRotationSpeed)); // register handlers this.service.getCharacteristic(this.platform.Characteristic.On) .onSet(this.setOn.bind(this)) .onGet(this.getOn.bind(this)); this.service.getCharacteristic(this.platform.Characteristic.RotationDirection) .onSet(this.setRotationDirection.bind(this)) .onGet(this.getRotationDirection.bind(this)); this.service.getCharacteristic(this.platform.Characteristic.RotationSpeed) .onSet(this.setRotationSpeed.bind(this)) .onGet(this.getRotationSpeed.bind(this)); } // Handlers async setOn(value) { this.states.FanState = value; this.storeState(); this.log.info(`[${this.accessoryConfiguration.accessoryName}] Setting State: ${Fan.getStateName(this.states.FanState)}`); } async getOn() { const fanState = this.states.FanState; this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Getting State: ${Fan.getStateName(fanState)}`); return fanState; } async setRotationDirection(value) { this.states.FanRotationDirection = value; this.storeState(); this.log.info(`[${this.accessoryConfiguration.accessoryName}] Setting Rotation Direction: ${this.states.FanRotationDirection}`); } async getRotationDirection() { const fanRotationDirection = this.states.FanRotationDirection; this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Getting Rotation Direction: ${fanRotationDirection}`); return fanRotationDirection; } async setRotationSpeed(value) { this.states.FanRotationSpeed = value; this.storeState(); this.log.info(`[${this.accessoryConfiguration.accessoryName}] Setting Rotation Speed: ${this.states.FanRotationSpeed}%`); } async getRotationSpeed() { const fanRotationSpeed = this.states.FanRotationSpeed; this.log.debug(`[${this.accessoryConfiguration.accessoryName}] Getting Rotation Speed: ${fanRotationSpeed}%`); return fanRotationSpeed; } getJsonState() { const json = JSON.stringify({ [this.stateStorageKey]: this.states.FanState, [this.rotatioDirectionStorageKey]: this.states.FanRotationDirection, [this.rotatioSpeedStorageKey]: this.states.FanRotationSpeed, }); return json; } getAccessoryTypeName() { return Fan.ACCESSORY_TYPE_NAME; } static getStateName(state) { let stateName; switch (state) { case undefined: { stateName = 'undefined'; break; } case Fan.ON: { stateName = 'ON'; break; } case Fan.OFF: { stateName = 'OFF'; break; } default: { stateName = state.toString(); } } return stateName; } } //# sourceMappingURL=virtualAccessoryFan.js.map