UNPKG

homebridge-miot

Version:

Homebridge plugin for devices supporting the miot protocol

157 lines (105 loc) 4.26 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 OutletAccessory 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.offDelayControl = this.getConfigValue('offDelayControl', false); this.showTemperature = this.getConfigValue('showTemperature', true); super.initAccessoryObject(); } /*----------========== ACCESSORY INFO ==========----------*/ getAccessoryType() { return DevTypes.OUTLET; } /*----------========== INIT ACCESSORIES ==========----------*/ initAccessories(name, uuid) { return [new Accessory(name, uuid, this.api.hap.Accessory.Categories.OUTLET)]; } /*----------========== SETUP SERVICES ==========----------*/ setupMainAccessoryService() { this.outletService = new Service.Outlet(this.getName(), 'outletService'); this.outletService .getCharacteristic(Characteristic.On) .onGet(this.isOutletOn.bind(this)) .onSet(this.setOutletOn.bind(this)); this.outletService .addCharacteristic(Characteristic.OutletInUse) .onGet(this.isOutletInUse.bind(this)); this.addAccessoryService(this.outletService); } setupAdditionalAccessoryServices() { this.prepareOutletServices(); if (this.offDelayControl) this.prepareOffDelayService(); super.setupAdditionalAccessoryServices(); // make sure we call super } /*----------========== CREATE ADDITIONAL SERVICES ==========----------*/ prepareOutletServices() { if (this.getDevice().hasMultiplySwitchServices()) { let allSwitchServices = this.getDevice().getAllSwitchServices(); allSwitchServices.shift(); // remove first one since that should be our main one! let outletCounter = 1; allSwitchServices.forEach((service, i) => { let serviceDesc = service.getDescription(); let serviceOnProp = service.getPropertyByType('on'); if (serviceOnProp) { if (serviceDesc.toLowerCase().includes('usb')) { serviceDesc = `USB`; } else { serviceDesc = `Outlet ${outletCounter}`; outletCounter++; } this.addOutletService(null, serviceDesc, service); //id will be autogenerated by the service } }); } } prepareTemperatureService() { if (this.showTemperature) { super.prepareTemperatureService(); } } /*----------========== HOMEBRIDGE STATE SETTERS/GETTERS ==========----------*/ isOutletOn() { if (this.isMiotDeviceConnected()) { return this.getDevice().isOn(); } return false; } setOutletOn(state) { if (this.isMiotDeviceConnected()) { this.getDevice().setOn(state); } else { throw new HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE); } } isOutletInUse() { if (this.isMiotDeviceConnected()) { return this.getDevice().isOn(); } return false; } // ----- additional services /*----------========== STATUS ==========----------*/ updateAccessoryStatus() { if (this.outletService) this.outletService.getCharacteristic(Characteristic.On).updateValue(this.isOutletOn()); if (this.outletService) this.outletService.getCharacteristic(Characteristic.OutletInUse).updateValue(this.isOutletInUse()); super.updateAccessoryStatus(); } /*----------========== MULTI-SWITCH SERVICE HELPERS ==========----------*/ /*----------========== GETTERS ==========----------*/ /*----------========== PROPERTY WRAPPERS ==========----------*/ /*----------========== CUSTOM SERVICES ==========----------*/ /*----------========== PROPERTY HELPERS ==========----------*/ /*----------========== HELPERS ==========----------*/ } module.exports = OutletAccessory;