UNPKG

homebridge-miot

Version:

Homebridge plugin for devices supporting the miot protocol

105 lines (61 loc) 2.66 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 CoffeeMachineAccessory 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() { super.initAccessoryObject(); } /*----------========== ACCESSORY INFO ==========----------*/ getAccessoryType() { return DevTypes.COFFEE_MACHINE; } /*----------========== INIT ACCESSORIES ==========----------*/ initAccessories(name, uuid) { return [new Accessory(name, uuid, this.api.hap.Accessory.Categories.AIR_HEATER)]; } /*----------========== SETUP SERVICES ==========----------*/ setupMainAccessoryService() { this.coffeMachineSwitchService = this.createStatefulSwitch(this.getName(), 'coffeMachineSwitchService', this.isCoffeMachineOn, this.setCoffeMachineOn); this.addAccessoryService(this.coffeMachineSwitchService); } setupAdditionalAccessoryServices() { super.setupAdditionalAccessoryServices(); // make sure we call super } /*----------========== CREATE ADDITIONAL SERVICES ==========----------*/ /*----------========== HOMEBRIDGE STATE SETTERS/GETTERS ==========----------*/ isCoffeMachineOn() { if (this.isMiotDeviceConnected()) { return this.getDevice().isOn(); } return false; } setCoffeMachineOn(state) { if (this.isMiotDeviceConnected()) { this.getDevice().setOn(state); } else { throw new HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE); } } // ----- additional services /*----------========== STATUS ==========----------*/ updateAccessoryStatus() { if (this.coffeMachineSwitchService) this.coffeMachineSwitchService.getCharacteristic(Characteristic.On).updateValue(this.isCoffeMachineOn()); super.updateAccessoryStatus(); } /*----------========== MULTI-SWITCH SERVICE HELPERS ==========----------*/ /*----------========== GETTERS ==========----------*/ /*----------========== PROPERTY WRAPPERS ==========----------*/ /*----------========== PROPERTY HELPERS ==========----------*/ /*----------========== HELPERS ==========----------*/ } module.exports = CoffeeMachineAccessory;