-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
65 lines (51 loc) · 1.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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.LockMechanism(this.name);
this.service
.getCharacteristic(Characteristic.LockCurrentState)
.on('get', this.getState.bind(this));
this.service
.getCharacteristic(Characteristic.LockTargetState)
.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 = (state == Characteristic.LockTargetState.SECURED) ? "arm" : "disarm";
console.log(`Set Alarm state to ${alarmState}`);
getAccessToken(
this.config.username,
this.config.password
).then((accessToken) => {
setStatus(accessToken, alarmState).then((response) => {
var currentState = (state == Characteristic.LockTargetState.SECURED) ?
Characteristic.LockCurrentState.SECURED : Characteristic.LockCurrentState.UNSECURED;
this.service.setCharacteristic(Characteristic.LockCurrentState, currentState);
callback(null); // success
}).catch((response) => {
console.log('catch')
console.log(response)
});
});
}
YaleAlarm.prototype.getServices = function() {
return [this.service];
}