UNPKG

homebridge-miot

Version:

Homebridge plugin for devices supporting the miot protocol

199 lines (153 loc) 7.12 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'); const Properties = require('../../constants/Properties.js'); class FreshAirSystemAccessory extends BaseAccessory { constructor(name, miotDevice, uuid, config, api, logger) { super(name, miotDevice, uuid, config, api, logger); } /*----------========== SETUP ACCESSORY ==========----------*/ initHapConstants() { Service = this.api.hap.Service; Characteristic = this.api.hap.Characteristic; Accessory = this.api.platformAccessory; HapStatusError = this.api.hap.HapStatusError; HAPStatus = this.api.hap.HAPStatus; } initConfigProperties(config) { this.buzzerControl = this.getPropValue(config['buzzerControl'], true); this.ledControl = this.getPropValue(config['ledControl'], true); this.actionButtons = this.getPropValue(config['actionButtons'], false); this.fanLevelControl = this.getPropValue(config['fanLevelControl'], true); this.modeControl = this.getPropValue(config['modeControl'], false); this.heaterControl = this.getPropValue(config['heaterControl'], true); this.heatLevelControl = this.getPropValue(config['heatLevelControl'], false); this.pm25Breakpoints = this.getPropValue(config['pm25Breakpoints'], [7, 15, 30, 55]); this.co2AbnormalThreshold = this.getPropValue(config['co2AbnormalThreshold'], 1000); } getAccessoryType() { return DevTypes.FRESH_AIR_SYSTEM; } /*----------========== SETUP SERVICES ==========----------*/ initAccessory() { return new Accessory(this.getName(), this.getUuid(), this.api.hap.Accessory.Categories.FAN); } 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.addChildLockCharacteristic(this.fanService); this.addAccessoryService(this.fanService); } setupAdditionalAccessoryServices() { if (this.buzzerControl) this.prepareBuzzerControlService(); if (this.ledControl) this.prepareLedControlService(); if (this.actionButtons) this.prepareActionButtonServices(this.actionButtons); if (this.fanLevelControl) this.prepareFanLevelControlServices(); if (this.modeControl) this.prepareModeControlServices(); if (this.heaterControl) this.prepareHeaterControlService(); if (this.heatLevelControl) this.prepareHeatLevelControlServices(); this.prepareFilterMaintenanceService(); this.prepareTemperatureService(); this.prepareAirQualityService(this.pm25Breakpoints); this.prepareCarbonDioxideService(this.co2AbnormalThreshold); } /*----------========== CREATE ADDITIONAL SERVICES ==========----------*/ prepareFilterMaintenanceService() { if (this.getDevice().supportsFilterLifeLevelReporting()) { this.filterMaintenanceService = new Service.FilterMaintenance('Filter Maintenance', 'filterMaintenanceService'); this.filterMaintenanceService .getCharacteristic(Characteristic.FilterChangeIndication) .onGet(this.getFilterChangeIndicationState.bind(this)); this.filterMaintenanceService .addCharacteristic(Characteristic.FilterLifeLevel) .onGet(this.getFilterLifeLevel.bind(this)); this.addAccessoryService(this.filterMaintenanceService); } } prepareHeaterControlService() { if (this.getDevice().supportsHeater()) { this.heaterService = this.createStatefulSwitch('Heater', 'heaterService', this.isHeaterOn, this.setHeaterOn); this.addAccessoryService(this.heaterService); } } prepareHeatLevelControlServices() { if (this.getDevice().supportsHeatLevels()) { this.addPropValueListWrapper('Heat', Properties.HEAT_LEVEL, Properties.POWER); } } /*----------========== HOMEBRIDGE STATE SETTERS/GETTERS ==========----------*/ getFanActiveState() { if (this.isMiotDeviceConnected()) { return this.getDevice().isPowerOn() ? Characteristic.Active.ACTIVE : Characteristic.Active.INACTIVE; } return Characteristic.Active.INACTIVE; } setFanActiveState(state) { if (this.isMiotDeviceConnected()) { let isPowerOn = state === Characteristic.Active.ACTIVE; this.getDevice().setPowerOn(isPowerOn); } else { throw new HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE); } } getCurrentFanState() { if (this.isMiotDeviceConnected() && this.getDevice().isPowerOn()) { if (this.getDevice().isIdle()) { return Characteristic.CurrentFanState.IDLE; } else { return Characteristic.CurrentFanState.BLOWING_AIR; } } return Characteristic.CurrentFanState.INACTIVE; } // ----- additional services getFilterChangeIndicationState() { if (this.isMiotDeviceConnected()) { let lifeLevel = this.getDevice().getFilterLifeLevel(); if (lifeLevel <= 5) { return Characteristic.FilterChangeIndication.CHANGE_FILTER; } } return Characteristic.FilterChangeIndication.FILTER_OK; } getFilterLifeLevel() { if (this.isMiotDeviceConnected()) { return this.getDevice().getFilterLifeLevel(); } return 0; } isHeaterOn() { if (this.isMiotDeviceConnected()) { return this.getDevice().isHeaterEnabled(); } return false; } setHeaterOn(value) { if (this.isMiotDeviceConnected()) { this.getDevice().setHeaterEnabled(value); } else { throw new HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE); } } /*----------========== STATUS ==========----------*/ updateDeviceStatus() { if (this.fanService) this.fanService.getCharacteristic(Characteristic.Active).updateValue(this.getFanActiveState()); if (this.fanService) this.fanService.getCharacteristic(Characteristic.CurrentFanState).updateValue(this.getCurrentFanState()); if (this.filterMaintenanceService && this.getDevice().supportsFilterLifeLevelReporting()) this.filterMaintenanceService.getCharacteristic(Characteristic.FilterChangeIndication).updateValue(this.getFilterChangeIndicationState()); if (this.filterMaintenanceService && this.getDevice().supportsFilterLifeLevelReporting()) this.filterMaintenanceService.getCharacteristic(Characteristic.FilterLifeLevel).updateValue(this.getFilterLifeLevel()); if (this.heaterService) this.heaterService.getCharacteristic(Characteristic.On).updateValue(this.isHeaterOn()); super.updateDeviceStatus(); } /*----------========== MULTI-SWITCH SERVICE HELPERS ==========----------*/ /*----------========== GETTERS ==========----------*/ /*----------========== HELPERS ==========----------*/ } module.exports = FreshAirSystemAccessory;