-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
101 lines (85 loc) · 2.77 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var Service;
var Characteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-abode", "Abode", AbodeAlarmAccessory);
};
function AbodeAlarmAccessory(log, config) {
this.abode = require('abode-api').abode(config.abode.username, config.abode.password);
this.log = log;
this.name = config.name;
this.lockService = new Service.SecuritySystem(this.name);
this.lockService
.getCharacteristic(Characteristic.SecuritySystemTargetState)
.on('get', this.getAlarmStatus.bind(this))
.on('set', this.setAlarmStatus.bind(this));
this.lockService
.getCharacteristic(Characteristic.SecuritySystemCurrentState)
.on('get', this.getAlarmStatus.bind(this));
}
AbodeAlarmAccessory.prototype.getAlarmStatus = function (callback) {
this.log(`${this.name}: Getting Alarm Status`);
this.abode.panel()
.then(response => {
if (response.data.mode.area_1) {
let status = '';
switch (response.data.mode.area_1) {
case 'standby':
status = Characteristic.SecuritySystemCurrentState.DISARMED;
break;
case 'home':
status = Characteristic.SecuritySystemCurrentState.HOME_ARM;
break;
case 'away':
status = Characteristic.SecuritySystemCurrentState.AWAY_ARM;
break;
}
this.log(`${this.name}: Status is ${status}`);
this.lockService.setCharacteristic(Characteristic.SecuritySystemCurrentState, status);
return callback(null, status);
}
return callback(null);
})
.catch(err => {
this.log(`${this.name}: ERROR GETTING STATUS ${err}`);
return callback(null);
});
};
AbodeAlarmAccessory.prototype.setAlarmStatus = function (state, callback) {
let operation;
let status = '';
switch (state) {
case Characteristic.SecuritySystemTargetState.STAY_ARM:
status = 'home';
operation = this.abode.mode.home();
break;
case Characteristic.SecuritySystemTargetState.AWAY_ARM :
status = 'away';
operation = this.abode.mode.away();
break;
case Characteristic.SecuritySystemTargetState.NIGHT_ARM:
status = 'home';
operation = this.abode.mode.home();
break;
case Characteristic.SecuritySystemTargetState.DISARM:
status = 'standby';
operation = this.abode.mode.standby();
break;
}
this.log(`${this.name}: Setting status status to ${state}`);
return operation
.then(() => {
this.lockService.setCharacteristic(Characteristic.SecuritySystemCurrentState, state);
this.log(`${this.name}: Set status to ${status}`);
return callback(null);
})
.catch(err => {
this.log(`${this.name}: ERROR SETTING STATUS ${err}`);
return callback(null);
});
};
AbodeAlarmAccessory.prototype.getServices = function () {
this.log(`${this.name}: Getting Services`);
return [this.lockService];
};