This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
MMM-homeassistant-sensors.js
141 lines (117 loc) · 3.48 KB
/
MMM-homeassistant-sensors.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
'use strict';
Module.register("MMM-homeassistant-sensors", {
result: {},
defaults: {
prettyName: true,
stripName: true,
title: 'Home Assistant',
url: '',
updateInterval: 3000,
values: []
},
start: function() {
this.getStats();
this.scheduleUpdate();
},
isEmpty: function(obj) {
for(var key in obj) {
if(obj.hasOwnProperty(key)) {
return false;
}
}
return true;
},
getDom: function() {
var wrapper = document.createElement("ticker");
wrapper.className = 'dimmed small';
var data = this.result;
var statElement = document.createElement("header");
var title = this.config.title;
statElement.innerHTML = title;
wrapper.appendChild(statElement);
if (data && !this.isEmpty(data)) {
var tableElement = document.createElement("table");
var values = this.config.values;
// console.log(values);
if (values.length > 0) {
for (var i = 0; i < values.length; i++) {
// console.log(data);
var val = this.getValue(data, values[i]);
var name=this.getName(data, values[i]);
// console.log(val);
if (val) {
tableElement.appendChild(this.addValue(values[i], val));
}
}
} else {
for (var key in data) {
if (data.hasOwnProperty(key)) {
tableElement.appendChild(this.addValue(key, data[key]));
}
}
}
wrapper.appendChild(tableElement);
} else {
var error = document.createElement("span");
error.innerHTML = "Error fetching stats.";
wrapper.appendChild(error);
}
return wrapper;
},
getValue: function(data, value) {
for(var i=0; i<data.length;i++){
// console.log(data[i].entity_id + " == " + value);
if (data[i].entity_id == value){
console.log(data[i].state + " " + data[i].attributes.unit_of_measurement);
return data[i].state + " " + data[i].attributes.unit_of_measurement;
}
}
return null;
},
getName: function(data, value) {
for(var i=0; i<data.length;i++){
// console.log(data[i].entity_id + " == " + value);
if (data[i].entity_id == value){
console.log(data[i].attributes.friendly_name);
return data[i].attributes.friendly_name;
}
}
return null;
},
addValue: function(name, value) {
var row = document.createElement("tr");
if (this.config.stripName) {
var split = name.split(".");
name = split[split.length - 1];
}
if (this.config.prettyName) {
name = name.replace(/([A-Z])/g, function($1){return "_"+$1.toLowerCase();});
name = name.split("_").join(" ");
name = name.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
console.log(name + ": " + value);
// row.innerHTML = name + ": " + JSON.stringify(value);
row.innerHTML = name + ": " + value;
return row;
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setInterval(function() {
self.getStats();
}, nextLoad);
},
getStats: function () {
this.sendSocketNotification('GET_STATS', this.config.url);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "STATS_RESULT") {
this.result = payload;
var fade = 500;
this.updateDom(fade);
}
},
});