UNPKG

homebridge-miot

Version:

Homebridge plugin for devices supporting the miot protocol

104 lines (61 loc) 2.56 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 CookerAccessory 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.COOKER; } /*----------========== INIT ACCESSORIES ==========----------*/ initAccessories(name, uuid) { return [new Accessory(name, uuid, this.api.hap.Accessory.Categories.SWITCH)]; } /*----------========== SETUP SERVICES ==========----------*/ setupMainAccessoryService() { this.switchService = this.createStatefulSwitch(this.getName(), 'switchService', this.isCookerOn, this.setCookerOn); this.addAccessoryService(this.switchService); } setupAdditionalAccessoryServices() { super.setupAdditionalAccessoryServices(); // make sure we call super } /*----------========== CREATE ADDITIONAL SERVICES ==========----------*/ /*----------========== HOMEBRIDGE STATE SETTERS/GETTERS ==========----------*/ isCookerOn() { if (this.isMiotDeviceConnected()) { return this.getDevice().isCooking(); } return false; } setCookerOn(state) { if (this.isMiotDeviceConnected()) { this.getDevice().setCookActive(state); } else { throw new HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE); } } // ----- additional services /*----------========== STATUS ==========----------*/ updateAccessoryStatus() { if (this.switchService) this.switchService.getCharacteristic(Characteristic.On).updateValue(this.isCookerOn()); super.updateAccessoryStatus(); } /*----------========== MULTI-SWITCH SERVICE HELPERS ==========----------*/ /*----------========== GETTERS ==========----------*/ /*----------========== PROPERTY WRAPPERS ==========----------*/ /*----------========== PROPERTY HELPERS ==========----------*/ /*----------========== HELPERS ==========----------*/ } module.exports = CookerAccessory;