forked from leinich/MMM-homeassistant-sensors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
54 lines (53 loc) · 1.87 KB
/
node_helper.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
var NodeHelper = require('node_helper');
var request = require('request');
module.exports = NodeHelper.create({
start: function () {
if(config.debuglogging) { console.log('MMM-homeassistant-sensors helper started...') };
},
getStats: function (config) {
var self = this;
var url = self.buildUrl(config);
var get_options = {
url: url,
json: true
};
if(config.token.length > 1) {
if(config.debuglogging) {console.error('MMM-homeassistant-sensors: Adding token', config.token)}
get_options.headers = { 'Authorization' : 'Bearer ' + config.token };
}
request(get_options, function (error, response, body) {
if(config.debuglogging) {
console.error('MMM-homeassistant-sensors ERROR:', error);
console.error('MMM-homeassistant-sensors statusCode:', response.statusCode);
console.error('MMM-homeassistant-sensors Body:', body);
}
if (!error && response.statusCode == 200) {
if(config.debuglogging) { console.log('MMM-homeassistant-sensors response successfull. calling STATS_RESULT') };
self.sendSocketNotification('STATS_RESULT', body);
}
});
},
buildUrl: function(config) {
var url = config.host;
if (config.port) {
url = url + ':' + config.port;
}
url = url + '/api/states'
if (config.apipassword.length > 1) {
url = url + '?api_password=' + config.apipassword;
}
if (config.https) {
url = 'https://' + url;
} else {
url = 'http://' + url;
}
if(config.debuglogging) { console.error("MMM-homeassistant-sensors - buildUrl:", url);}
return url;
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === 'GET_STATS') {
this.getStats(payload);
}
}
});