UNPKG

homebridge-plugin-gsav-virtual-fan

Version:

virtual fan

146 lines (129 loc) 5.02 kB
var Service, Characteristic; var statePower=0; // init state, default is OFF var statePowerTemp=0; // init state, default is OFF var stateSpeed=2; // init speed, default is 2 var stateSpeedTemp=0; // init speed, default is 2 module.exports = function(homebridge) { Service = homebridge.hap.Service; Characteristic = homebridge.hap.Characteristic; homebridge.registerAccessory("homebridge-gsavfan", "VirtualFan", Virtual); } function Virtual(log, config) { this.log = log; this.name = config["name"]; this.area = config["area"]; this.channel = config["channel"]; this.statePower = 0; // init state, default is OFF this.stateSpeed = 0; // init speed, default is 0 this.log(" GS AV '"); this.log(" / \ '"); this.log(" | FAN | '"); this.log(" \ / '"); this.log(" V0.1.75 '"); } Virtual.prototype.getServices = function() { this.virtualService = new Service.Fan(); this.virtualService.getCharacteristic(Characteristic.On) .on('get', this.getOn.bind(this)) .on('set', this.setOn.bind(this)); this.virtualService.getCharacteristic(Characteristic.RotationSpeed) .setProps({minValue: 0, maxValue: 3, minStep: 1,}) .on('get', this.getSpeed.bind(this)) .on('set', this.setSpeed.bind(this)); this.virtualInfoService = new Service.AccessoryInformation(); this.virtualInfoService .setCharacteristic(Characteristic.Manufacturer, "GSAV") .setCharacteristic(Characteristic.Model, "Fan") .setCharacteristic(Characteristic.SerialNumber, "0"); return [this.virtualService, this.virtualInfoService]; } /************************************************************/ Virtual.prototype.getOn = function(callback) { //var power = this.statePower > 0; this.log("/ status / fan power / state=%s / area=%s / %s", this.statePower, this.area, this.name); callback(null, this.statePower); } Virtual.prototype.setOn = function(value1, callback) { this.statePowerTemp = value1; //this.log("/debug s$ton/ %s / %s / %s", this.statePowerTemp, this.area, this.name); switch(this.statePowerTemp) { //when number is said, comes here then goes to set speed. We want it to case true: if(this.statePower == false) { this.statePower = value1; this.log("/ set / fan power / fan=power:on:%s / %s", this.area, this.name); //this.stateSpeed = 3; //lets set to what it was on before } this.log("/ set / fan speed / fan=speed:%s:%s / %s", this.stateSpeed, this.area, this.name); callback(null); break; case false: if(this.statePower == true) { this.statePower = value1; this.log("/ set / fan power / fan=power:off:%s / %s", this.area, this.name); //this.stateSpeed = 0; } this.log("/ set / fan speed / fan=speed:0:%s / %s", this.area, this.name); callback(null); break; default: this.log("/ debug / error / statePower error"); this.stateSpeed = 0; this.statePower = false; callback(null); break; } } /************************************************************/ Virtual.prototype.getSpeed = function(callback) { //var speed = this.stateSpeed; this.log("/ status / fan speed / state=%s / area=%s / %s", this.stateSpeed, this.area, this.name); callback(null, this.stateSpeed); } Virtual.prototype.setSpeed = function(value2, callback) { this.stateSpeedTemp = value2; //this.log("/debug s$tspeed/ %s / %s / %s", this.stateSpeedTemp, this.area, this.name); switch(this.stateSpeedTemp) { case 0: this.statePower = false; this.stateSpeed = 0; this.log("/ set / fan power / fan=power:off:%s / %s", this.area, this.name); this.log("/ set / fan speed / fan=speed:%s:%s / %s", this.stateSpeed, this.area, this.name); callback(null); break; case 1: this.statePower = true; this.stateSpeed = 1; this.log("/ set / fan power / fan=power:on:%s / %s", this.area, this.name); this.log("/ set / fan speed / fan=speed:%s:%s / %s", this.stateSpeed, this.area, this.name); callback(null); break; case 2: this.statePower = true; this.stateSpeed = 2; this.log("/ set / fan power / fan=power:on:%s / %s", this.area, this.name); this.log("/ set / fan speed / fan=speed:%s:%s / %s", this.stateSpeed, this.area, this.name); callback(null); break; case 3: this.statePower = true; this.stateSpeed = 3; this.log("/ set / fan power / fan=power:on:%s / %s", this.area, this.name); this.log("/ set / fan speed / fan=speed:%s:%s / %s", this.stateSpeed, this.area, this.name); callback(null); break; } } /************************************************************/