forked from chowielin/homebridge-simplisafe-security-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
168 lines (147 loc) · 4.41 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var SS3Client = require("simplisafe-ss3");
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory(
"homebridge-simplisafe",
"Homebridge-SimpliSafe",
SimpliSafeSecuritySystemAccessory
);
};
var ss3Client;
function SimpliSafeSecuritySystemAccessory(log, config) {
this.log = log;
ss3Client = new SS3Client(
config.auth.username,
config.auth.password,
config.auth.retryInSec,
config.auth.retries,
config.v2,
log
);
ss3Client
.login()
.then(
function() {
log("User ID: " + ss3Client.userId);
log("Sub ID: " + ss3Client.subId);
log("Initial token: " + ss3Client.token);
return ss3Client.getAlarmState();
},
function(err) {
log("Login failed due to: " + err.message);
throw err;
}
)
.then(function(alarmState) {
log("Initial alarm state: " + alarmState);
});
this.name = config["name"];
this.convertHomeKitStateToSimpliSafeState = function(homeKitState) {
switch (homeKitState) {
case Characteristic.SecuritySystemTargetState.STAY_ARM:
case Characteristic.SecuritySystemTargetState.NIGHT_ARM:
return "home";
break;
case Characteristic.SecuritySystemTargetState.AWAY_ARM:
return "away";
break;
case Characteristic.SecuritySystemTargetState.DISARM:
return "off";
break;
}
};
this.convertSimpliSafeStateToHomeKitState = function(simpliSafeState) {
switch (simpliSafeState.toUpperCase()) {
case "HOME":
case "HOME_COUNT":
return Characteristic.SecuritySystemTargetState.STAY_ARM;
break;
case "AWAY":
case "AWAY_COUNT":
case "ALARM_COUNT":
return Characteristic.SecuritySystemTargetState.AWAY_ARM;
break;
case "OFF":
return Characteristic.SecuritySystemTargetState.DISARM;
break;
default:
log(
"Could not resolve SS state: " +
simpliSafeState +
" to Homekit security system state"
);
return null;
break;
}
};
}
SimpliSafeSecuritySystemAccessory.prototype = {
setTargetState: function(state, callback) {
this.log("Setting state to %s", state);
var self = this;
// Set state in simplisafe 'off' or 'home' or 'away'
self.securityService.setCharacteristic(
Characteristic.SecuritySystemCurrentState,
state
);
var ssState = self.convertHomeKitStateToSimpliSafeState(state);
this.log("Converted state %s", ssState);
ss3Client
.setState(ssState)
.then(
function(response) {
this.log(response);
// Important: after a successful server response, we update the current state of the system
self.securityService.setCharacteristic(
Characteristic.SecuritySystemCurrentState,
self.convertSimpliSafeStateToHomeKitState(state)
);
callback(null, state);
},
function() {
callback(new Error("Failed to set target state to " + state));
}
)
.catch(function(error) {
this.log(error);
callback(new Error("Failed to set target state to " + state));
});
},
getState: function(callback) {
var self = this;
ss3Client.getAlarmState().then(
function(state) {
self.log("got SS alarm state:", state);
callback(null, self.convertSimpliSafeStateToHomeKitState(state));
},
function() {
callback(new Error("Failed to get alarm state"));
}
);
},
getCurrentState: function(callback) {
this.log("Getting current state");
this.getState(callback);
},
getTargetState: function(callback) {
this.log("Getting target state");
this.getState(callback);
},
identify: function(callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function() {
this.securityService = new Service.SecuritySystem(this.name);
this.securityService
.getCharacteristic(Characteristic.SecuritySystemCurrentState)
.on("get", this.getCurrentState.bind(this));
this.securityService
.getCharacteristic(Characteristic.SecuritySystemTargetState)
.on("get", this.getTargetState.bind(this))
.on("set", this.setTargetState.bind(this));
return [this.securityService];
}
};