Open
Description
`var Service, Characteristic;
const { getAccessToken, getStatus, setStatus } = require('yalealarmsystem');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-yalealarmsystem", "YaleAlarm", YaleAlarm);
}
function YaleAlarm(log, config) {
this.log = log;
this.name = config["name"];
this.config = config;
this.service = new Service.SecuritySystem(this.name);
console.log(this.service);
this.service
.getCharacteristic(Characteristic.SecuritySystemCurrentState)
.on('get', this.getState.bind(this));
this.service
.getCharacteristic(Characteristic.SecuritySystemTargetState)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this));
}
YaleAlarm.prototype.getState = function(callback) {
this.log("Getting current state...");
getAccessToken(
this.config.username,
this.config.password
).then(getStatus).then((response) => {
console.log(response);
callback(null, response === 'arm');
}).catch(console.log);
}
YaleAlarm.prototype.setState = function(state, callback) {
var alarmState = null;
console.log(`Incoming Alarm state is ${state}`);
switch(state){
case Characteristic.SecuritySystemTargetState.STAY_ARM:
case Characteristic.SecuritySystemTargetState.NIGHT_ARM:
var alarmState = "home";
break;
case Characteristic.SecuritySystemTargetState.AWAY_ARM:
var alarmState = "arm";
break;
case Characteristic.SecuritySystemTargetState.DISARM:
default:
var alarmState = "disarm";
break;
}
console.log(`Set Alarm state to ${alarmState}`);
getAccessToken(
this.config.username,
this.config.password
).then((accessToken) => {
setStatus(accessToken, alarmState).then((response) => {
var currentState = "";
switch(state){
case Characteristic.SecuritySystemTargetState.STAY_ARM:
currentState = Characteristic.SecuritySystemCurrentState.STAY_ARM
break;
case Characteristic.SecuritySystemTargetState.NIGHT_ARM:
currentState = Characteristic.SecuritySystemCurrentState.NIGHT_ARM
break;
case Characteristic.SecuritySystemTargetState.AWAY_ARM:
currentState = Characteristic.SecuritySystemCurrentState.AWAY_ARM
break;
case Characteristic.SecuritySystemTargetState.DISARM:
currentState = Characteristic.SecuritySystemCurrentState.DISARMED
break;
}
console.log(Characteristic.SecuritySystemCurrentState);
this.service.setCharacteristic(Characteristic.SecuritySystemCurrentState, currentState);
callback(null); // success
}).catch((response) => {
console.log('catch')
console.log(response)
});
});
}
YaleAlarm.prototype.getServices = function() {
return [this.service];
}`
Metadata
Metadata
Assignees
Labels
No labels