forked from leinich/MMM-homeassistant-sensors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-homeassistant-sensors.js
218 lines (213 loc) · 6.62 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
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
"use strict";
Module.register("MMM-homeassistant-sensors", {
result: {},
defaults: {
prettyName: true,
stripName: true,
title: "Home Assistant",
host: "hassio.local",
port: "8321",
https: false,
token: "",
apipassword: "",
updateInterval: 300000,
displaySymbol: true,
debuglogging: false,
values: []
},
getStyles: function() {
return [
"modules/MMM-homeassistant-sensors/MaterialDesign-Webfont-master/css/materialdesignicons.min.css",
"modules/MMM-homeassistant-sensors/hassio.css"
];
},
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;
if (values.length > 0) {
for (var i = 0; i < values.length; i++) {
var icons = values[i].icons[0];
var sensor = values[i].sensor;
var attributes = values[i].attributes;
var val = this.getValue(data, sensor, attributes);
var name = this.getName(data, sensor);
var unit = this.getUnit(data, sensor);
var alertThreshold = values[i].alertThreshold;
if (val) {
tableElement.appendChild(
this.addValue(name, val, unit, icons, alertThreshold)
);
}
}
} else {
for (var key in data) {
if (data.hasOwnProperty(key)) {
tableElement.appendChild(
this.addValue(key, data[key], "", "", false)
);
}
}
}
wrapper.appendChild(tableElement);
} else {
var error = document.createElement("span");
error.innerHTML = "Error fetching stats.";
wrapper.appendChild(error);
}
return wrapper;
},
getValue: function(data, value, attributes=[]) {
for (var i = 0; i < data.length; i++) {
if (data[i].entity_id == value) {
if(attributes.length==0) {
return data[i].state;
}
var returnString = ' | ';
for(var j=0; j<attributes.length; j++) {
if(attributes[j] == 'state') {
returnString += data[i].state + ' | ';
} else {
if(data[i]['attributes'][attributes[j]] !== undefined) {
returnString += data[i]['attributes'][attributes[j]] + ' | ';
}
}
}
return returnString.slice(0, -3);
}
}
return null;
},
getUnit: function(data, value) {
for (var i = 0; i < data.length; i++) {
if (data[i].entity_id == value) {
if (typeof data[i].attributes.unit_of_measurement !== "undefined") {
return data[i].attributes.unit_of_measurement;
}
return "";
}
}
return "";
},
getName: function(data, value) {
for (var i = 0; i < data.length; i++) {
if (data[i].entity_id == value) {
return data[i].attributes.friendly_name;
}
}
return null;
},
addValue: function(name, value, unit, icons, alertThreshold) {
var newrow, newText, newCell;
newrow = document.createElement("tr");
if (!isNaN(alertThreshold)) {
console.log("alertThreshold is a number");
if (alertThreshold < value) {
console.log("Threshold exceeded - blink it");
newrow.className += "blink";
}
}
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();
});
}
// icons
newCell = newrow.insertCell(0);
newCell.className = "align-left";
if (this.config.displaySymbol) {
if (typeof icons === "object") {
var iconsinline;
//Change icons based on HA status
if (value == "on" && typeof icons.state_on === "string") {
iconsinline = document.createElement("i");
iconsinline.className = "mdi mdi-" + icons.state_on;
newCell.appendChild(iconsinline);
} else if (value == "off" && typeof icons.state_off === "string") {
iconsinline = document.createElement("i");
iconsinline.className = "mdi mdi-" + icons.state_off;
newCell.appendChild(iconsinline);
} else if (value == "open" && typeof icons.state_open === "string") {
iconsinline = document.createElement("i");
iconsinline.className = "mdi mdi-" + icons.state_open;
newCell.appendChild(iconsinline);
} else if (
value == "closed" &&
typeof icons.state_closed === "string"
) {
iconsinline = document.createElement("i");
iconsinline.className = "mdi mdi-" + icons.state_closed;
newCell.appendChild(iconsinline);
} else {
if (typeof icons.default === "string") {
iconsinline = document.createElement("i");
iconsinline.className = "mdi mdi-" + icons.default;
newCell.appendChild(iconsinline);
}
}
}
}
// Name
newCell = newrow.insertCell(1);
newText = document.createTextNode(name);
newCell.appendChild(newText);
// Value
newCell = newrow.insertCell(2);
newCell.className = "align-right";
newText = document.createTextNode(value);
newCell.appendChild(newText);
// Unit
newCell = newrow.insertCell(3);
newCell.className = "align-left";
newText = document.createTextNode(unit);
newCell.appendChild(newText);
return newrow;
},
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);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "STATS_RESULT") {
this.result = payload;
var fade = 500;
this.updateDom(fade);
}
}
});