UNPKG

homebridge-miot

Version:

Homebridge plugin for devices supporting the miot protocol

157 lines (103 loc) 4.46 kB
let Service, Characteristic, Accessory, HapStatusError, HAPStatus; const BaseAccessory = require('../../base/BaseAccessory.js'); const Constants = require('../../constants/Constants.js'); const DevTypes = require('../../constants/DevTypes.js'); class CeilingFanAccessory extends BaseAccessory { constructor(name, device, uuid, config, api, logger) { Service = api.hap.Service; Characteristic = api.hap.Characteristic; Accessory = api.platformAccessory; HapStatusError = api.hap.HapStatusError; HAPStatus = api.hap.HAPStatus; super(name, device, uuid, config, api, logger); } /*----------========== INIT ==========----------*/ initAccessoryObject() { this.fanLevelControl = this.getConfigValue('fanLevelControl', false); this.offDelayControl = this.getConfigValue('offDelayControl', false); super.initAccessoryObject(); } /*----------========== ACCESSORY INFO ==========----------*/ getAccessoryType() { return DevTypes.CEILING_FAN; } /*----------========== INIT ACCESSORIES ==========----------*/ initAccessories(name, uuid) { return [new Accessory(name, uuid, this.api.hap.Accessory.Categories.FAN)]; } /*----------========== SETUP SERVICES ==========----------*/ setupMainAccessoryService() { this.fanService = new Service.Fanv2(this.getName(), 'fanService'); this.fanService .getCharacteristic(Characteristic.Active) .onGet(this.getFanActiveState.bind(this)) .onSet(this.setFanActiveState.bind(this)); this.fanService .addCharacteristic(Characteristic.CurrentFanState) .onGet(this.getCurrentFanState.bind(this)); this.addRotationSpeedCharacteristic(this.fanService); this.addAccessoryService(this.fanService); } setupAdditionalAccessoryServices() { if (this.offDelayControl) this.prepareOffDelayService(); if (this.fanLevelControl) this.prepareFanLevelControlServices(); this.prepareLightService(); super.setupAdditionalAccessoryServices(); // make sure we call super } /*----------========== CREATE ADDITIONAL SERVICES ==========----------*/ prepareLightService() { if (this.getDevice().hasLightService()) { this.addLightService('lightService', this.getName() + ' Light', this.getDevice().getLightService()); } } prepareModeControlServices() { super.prepareModeControlServices(); if (this.getDevice().supportsLightModes()) { this.addPropWrapper('Light Mode', this.getDevice().lightModeProp(), this.getDevice().lightOnProp(), null, null, null); } } prepareOffDelayService() { super.prepareOffDelayService(); if (this.getDevice().supportsLightOffDelay()) { this.addOffDelayWrapper('Light off delay', this.getDevice().lightOffDelayProp(), this.getDevice().lightOnProp()); } } /*----------========== HOMEBRIDGE STATE SETTERS/GETTERS ==========----------*/ getFanActiveState() { if (this.isMiotDeviceConnected()) { return this.getDevice().isOn() ? Characteristic.Active.ACTIVE : Characteristic.Active.INACTIVE; } return Characteristic.Active.INACTIVE; } setFanActiveState(state) { if (this.isMiotDeviceConnected()) { let value = state === Characteristic.Active.ACTIVE; this.getDevice().setOn(value); } else { throw new HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE); } } getCurrentFanState() { if (this.isMiotDeviceConnected()) { if (!this.getDevice().isOn()) { return Characteristic.CurrentFanState.IDLE; } else { return Characteristic.CurrentFanState.BLOWING_AIR; } } return Characteristic.CurrentFanState.INACTIVE; } // ----- additional services /*----------========== STATUS ==========----------*/ updateAccessoryStatus() { if (this.fanService) this.fanService.getCharacteristic(Characteristic.Active).updateValue(this.getFanActiveState()); if (this.fanService) this.fanService.getCharacteristic(Characteristic.CurrentFanState).updateValue(this.getCurrentFanState()); super.updateAccessoryStatus(); } /*----------========== MULTI-SWITCH SERVICE HELPERS ==========----------*/ /*----------========== GETTERS ==========----------*/ /*----------========== PROPERTY WRAPPERS ==========----------*/ /*----------========== PROPERTY HELPERS ==========----------*/ /*----------========== HELPERS ==========----------*/ } module.exports = CeilingFanAccessory;