This repository was 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
Expand file tree
/
Copy pathyamlGenerator.js
More file actions
114 lines (107 loc) · 3.73 KB
/
yamlGenerator.js
File metadata and controls
114 lines (107 loc) · 3.73 KB
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
#!/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 yaml = require('js-yaml')
const fs = require('fs')
fs.writeFile('mqtt.yaml','',function(err){});
var security_panel_zid = ''
var location_id = ''
var ring = RingAPI({
email: process.env.RING_USERNAME || 'abc@gmail.com',
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));
}
const configs_topic = 'homeassistant/binary_sensor/alarm/status/config';
const messages = { name : 'Alarm Status'
, device_class : 'connectivity'
, off_delay: 15
};
const sensor = {'binary_sensor connection': {
platform: 'mqtt',
state_topic: 'homeassistant/binary_sensor/alarm/status/state',
name : messages.name,
device_class : messages.device_class,
off_delay: messages.off_delay }};
console.log(yaml.dump(sensor));
fs.appendFile('mqtt.yaml',yaml.dump(sensor),function(err){});
message.body.forEach((device) => {
var sensor_name = device.general.v2.zid
if (device.general.v2.deviceType === 'sensor.motion') {
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));
const state_topic = 'homeassistant/binary_sensor/alarm/'+sensor_name+'/state';
const status = device.device.v1.faulted ? 'ON' : 'OFF';
const sensor = {['binary_sensor '+message.name]: {platform: 'mqtt', name: message.name, device_class : message.device_class, state_topic: state_topic}}
console.log(yaml.dump(sensor))
fs.appendFile('mqtt.yaml',yaml.dump(sensor),function(err){})
}
if (device.general.v2.deviceType === 'sensor.contact') {
const topic = 'homeassistant/binary_sensor/alarm/'+sensor_name+'/config';
const message = { name : device.general.v2.name
, device_class : 'door'
};
console.log(JSON.stringify(message));
const state_topic = 'homeassistant/binary_sensor/alarm/'+sensor_name+'/state';
const sensor = {['binary_sensor '+message.name] : {platform: 'mqtt', name: message.name, device_class : message.device_class, state_topic: state_topic}}
console.log(yaml.dump(sensor))
fs.appendFile('mqtt.yaml',yaml.dump(sensor),function(err){})
}
if (device.general.v2.deviceType === 'security-panel') {
security_panel_zid = sensor_name;
const message = { name : device.general.v2.name
, state_topic : 'home/alarm/state'
, command_topic : 'home/alarm/command'
};
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;
}
const sensor = {['alarm_control_panel '+message.name] :
{
platform: 'mqtt',
name: message.name,
state_topic: message.state_topic,
command_topic: message.command_topic
}
}
console.log(yaml.dump(sensor));
fs.appendFile('mqtt.yaml',yaml.dump(sensor),function(err){});
}
});
});
});
});
}
alarmContact();