forked from leinich/MMM-homeassistant-sensors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-homeassistant-sensors.js
187 lines (183 loc) · 5.17 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
'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"];
},
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 val = this.getValue(data, sensor);
var name = this.getName(data, sensor);
var unit = this.getUnit(data, sensor);
if (val) {
tableElement.appendChild(this.addValue(name, val, unit, icons));
}
}
} 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++) {
if (data[i].entity_id == value) {
return data[i].state;
}
}
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) {
var newrow,
newText,
newCell;
newrow = 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();
});
}
// 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);
}
},
});