homebridge-miot
Version:
Homebridge plugin for devices supporting the miot protocol
96 lines (55 loc) • 2.39 kB
JavaScript
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 PetFeederAccessory 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.foodAmount = this.getConfigValue('foodAmount', 7);
super.initAccessoryObject();
}
/*----------========== ACCESSORY INFO ==========----------*/
getAccessoryType() {
return DevTypes.PET_FEEDER;
}
/*----------========== INIT ACCESSORIES ==========----------*/
initAccessories(name, uuid) {
return [new Accessory(name, uuid, this.api.hap.Accessory.Categories.SWITCH)];
}
/*----------========== SETUP SERVICES ==========----------*/
setupMainAccessoryService() {
this.switchService = this.createStatlessSwitch(this.getName(), 'switchService', this.executePetFoodOut);
this.addAccessoryService(this.switchService);
}
setupAdditionalAccessoryServices() {
super.setupAdditionalAccessoryServices(); // make sure we call super
}
/*----------========== CREATE ADDITIONAL SERVICES ==========----------*/
/*----------========== HOMEBRIDGE STATE SETTERS/GETTERS ==========----------*/
executePetFoodOut(state) {
if (this.isMiotDeviceConnected()) {
this.getDevice().firePetFoodOut(this.foodAmount);
} else {
throw new HapStatusError(HAPStatus.SERVICE_COMMUNICATION_FAILURE);
}
}
// ----- additional services
/*----------========== STATUS ==========----------*/
updateAccessoryStatus() {
super.updateAccessoryStatus();
}
/*----------========== MULTI-SWITCH SERVICE HELPERS ==========----------*/
/*----------========== GETTERS ==========----------*/
/*----------========== PROPERTY WRAPPERS ==========----------*/
/*----------========== PROPERTY HELPERS ==========----------*/
/*----------========== HELPERS ==========----------*/
}
module.exports = PetFeederAccessory;