This repository has been archived by the owner on Mar 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mqttAlarm.js
225 lines (203 loc) · 6.68 KB
/
mqttAlarm.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env node
/* jshint esversion: 6, undef: true, unused: true, laxcomma: true */
/*
*
* To use this: npm install mqtt async doorbot
*
*/
const RingAPI = require('.');
const mqtt = require('mqtt')
const mqtt_username = process.env.MQTT_USER || '';
const mqtt_password = process.env.MQTT_PW || '';
const client = mqtt.connect(process.env.MQTT,{username: mqtt_username, password: mqtt_password});
var security_panel_zid = ''
var location_id = ''
const discovery = process.env.DISCOVERY;
client.on('connect', function () {
client.subscribe('homeassistant', function (err) {
if (!err) {
console.log('Connected to mqtt and subscribed to homeassistant channel');
}
})
client.subscribe('home/alarm/#', function (err) {});
})
client.on('message', function(topic, message) {
if (topic === 'home/alarm/command') {
var alarm_mode = '';
console.log(message.toString());
switch (message.toString()) {
case 'DISARM':
alarm_mode = 'none';
break;
case 'ARM_HOME':
alarm_mode = 'some';
break;
case 'ARM_AWAY':
alarm_mode = 'all';
break;
default:
break;
}
ring.stations((err, station) => {
ring.setAlarmMode(station[0],security_panel_zid,alarm_mode,[],(oops) => {});
})
}
if (topic === 'home/alarm/check') {
alarmContact();
}
})
var ring = RingAPI({
email: process.env.RING_USERNAME || '[email protected]',
password: process.env.RING_PASSPHRASE || 'mypassword',
retries: 1
});
function alarmContact() {
ring.stations((err, stations) => {
if (err) {
console.log('Error at stations')
return console.log(err);
}
location_id = stations[0].location_id;
console.log(JSON.stringify(stations,null,2))
stations.forEach((station)=> {
ring.getAlarmDevices(station, (err, station, message) => {
if (err) {
console.log('error at getAlarmDevices')
return console.log(JSON.stringify(err,null,2));
}
if (discovery == 'true'){
const configs_topic = 'homeassistant/binary_sensor/alarm/status/config';
const messages = { name : 'Alarm Status'
, device_class : 'connectivity'
, off_delay: 15
};
client.publish(configs_topic, JSON.stringify(messages));
}
client.publish('homeassistant/binary_sensor/alarm/status/state','ON',{retain: true});
message.body.forEach((device) => {
var sensor_name = device.general.v2.zid
if (device.general.v2.deviceType === 'sensor.motion') {
if (discovery == 'true'){
const config_topic = 'homeassistant/binary_sensor/alarm/'+sensor_name+'/config';
const message = { name : device.general.v2.name
, device_class : 'motion'
};
console.log(JSON.stringify(message));
client.publish(config_topic, JSON.stringify(message));
}
const state_topic = 'homeassistant/binary_sensor/alarm/'+sensor_name+'/state';
const status = device.device.v1.faulted ? 'ON' : 'OFF';
client.publish(state_topic,status,{retain: true});
}
if (device.general.v2.deviceType === 'sensor.contact') {
if (discovery == 'true'){
const topic = 'homeassistant/binary_sensor/alarm/'+sensor_name+'/config';
const message = { name : device.general.v2.name
, device_class : 'door'
};
console.log(JSON.stringify(message));
client.publish(topic, JSON.stringify(message));
}
}
if (device.general.v2.deviceType === 'security-panel') {
security_panel_zid = sensor_name;
if (discovery == 'true'){
const topic = 'homeassistant/alarm_control_panel/alarm/'+sensor_name+'/config';
const message = { name : device.general.v2.name
, state_topic : 'home/alarm/state'
, command_topic : 'home/alarm/command'
};
console.log(JSON.stringify(message));
client.publish(topic, JSON.stringify(message));
}
var state = 'disarmed'
switch (device.device.v1.mode) {
case 'none':
break;
case 'some':
state = 'armed_home';
break;
case 'all':
state = 'armed_away';
break;
default:
state = 'disarmed';
break;
}
client.publish('home/alarm/state',state,{retain: true});
}
});
});
ring.setAlarmCallback(station, 'DataUpdate', (err, station, message) => {
if (err) {
console.log('error at setCallback');
return console.log(JSON.stringify(err,null,2));
}
const body = message.body && message.body[0]
, info = body && body.general && body.general.v2
, context = body && body.context && body.context.v1 && body.context.v1.device && body.context.v1.device.v1
, update = {};
console.log('DataUpdate: errP=' + (!!err) + ' station=' + station.location_id + ' datatype=' + message.datatype);
if (message.datatype === 'HubDisconnectionEventType') {
console.log(JSON.stringify({ info, context, update: { statusActive: false } }, null, 2));
}
if (!(info && context && (message.datatype === 'DeviceInfoDocType'))) {
return console.log('message=' + JSON.stringify(message, null, 2));
}
update.deviceId = info.zid;
info.lastCommTime = new Date(info.lastCommTime).getTime();
//Construct topic & message to post to MQTT
var sensor_name = message.context.affectedEntityId;
var topic = '';
var status = '';
if (info.deviceType === 'security-panel') {
mode = message.body[0].device.v1.mode;
topic = 'home/alarm/state';
switch (mode) {
case 'none':
status = 'disarmed';
break;
case 'some':
status = 'armed_home';
break;
case 'all':
status = 'armed_away';
break;
default:
status = '';
break;
}
console.log(status);
return client.publish(topic, status,{retain: true});
}
if (info.deviceType === 'sensor.contact') {
update.faulted = context.faulted ? 'ON' : 'OFF';
topic = 'homeassistant/binary_sensor/alarm/'+sensor_name+'/state';
status = update.faulted;
return client.publish(topic, status,{retain: true});
};
if (info.deviceType === 'sensor.motion') {
update.faulted = context.faulted ? 'ON' : 'OFF';
topic = 'homeassistant/binary_sensor/alarm/'+sensor_name+'/state';
status = update.faulted;
return client.publish(topic, status,{retain: true});
};
if (info.tamperStatus) update.statusTampered = (info.tamperStatus === 'ok') ? 'NOT_TAMPERED' : 'TAMPERED';
client.publish(topic,status,{retain: true});
});
});
});
}
alarmContact();
function checkSocket(){
if (!ring.alarmSockets[location_id]){
console.log('Socket is disconnected')
ring.counter = 0;
ring.authQueue = [];
ring.authenticating = false;
return alarmContact();
}
client.publish('homeassistant/binary_sensor/alarm/status/state','ON');
console.log('Socket is connected')
}
setInterval(checkSocket,10000)