forked from wonderslug/MMM-HomeAssistantDisplay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMM-HomeAssistantDisplay.js
More file actions
executable file
·183 lines (169 loc) · 5.06 KB
/
MMM-HomeAssistantDisplay.js
File metadata and controls
executable file
·183 lines (169 loc) · 5.06 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
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
/* Magic Mirror
* Module: MMM-HomeAssistantDisplay
*
* By Brian Towles <brian@towles.com>
* MIT Licensed.
*/
Module.register("MMM-HomeAssistantDisplay", {
result: {},
defaults: {
title: "Home Assistant",
host: "hassio.local",
port: "8123",
useTLS: false,
ignoreCert: true,
token: "",
debuglogging: false,
useModuleTrigger: false, // Corrected typo: useModuleTigger -> useModuleTrigger
useModuleTigger: false, // For backwards compatibility
moduleTriggerTemplate: "",
moduleTriggerEntities: false,
animationSpeed: 3000,
sections: [],
class: ""
},
requiresVersion: "2.1.0", // Required version of MagicMirror
start: function () {
var self = this;
//Flag for check if module is loaded
this.loaded = false;
this.displayModule = false;
this.config.identifier = this.identifier;
this.sendSocketNotification("CONNECT", this.config);
// Setup the watched entity for the module display
if ((this.config.useModuleTrigger || this.config.useModuleTigger) && this.config.moduleTriggerEntities) {
for (const entity in this.config.moduleTriggerEntities) {
this.sendSocketNotification("SET_WATCHED_ENTITY", {
identifier: this.identifier,
entity: this.config.moduleTriggerEntities[entity]
});
}
} else {
this.displayModule = true;
}
if (this.config.sections) {
for (const sectioid in this.config.sections) {
const section = this.config.sections[sectioid];
for (const entity in section.triggerEntities) {
this.sendSocketNotification("SET_WATCHED_ENTITY", {
identifier: this.identifier,
entity: section.triggerEntities[entity]
});
}
// Set up a timer to trigger re-rendering outside of any entity state update
if (section.refreshTimer) {
setInterval(() => {
this.renderTemplates("timeout");
this.updateDom();
}, section.refreshTimer * 1000);
}
}
}
this.renderTemplates("foo");
self.updateDom(self.config.animationSpeed);
},
isEmpty: function (obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
return false;
}
}
return true;
},
getDom: function () {
var self = this;
var wrapper = document.createElement("div");
if (this.config.class) {
wrapper.className += this.config.class;
}
if (!this.displayModule) {
this.hide();
return wrapper;
}
this.show();
for (const section in this.config.sections) {
if (this.config.sections[section].render) {
var sectionWrapper = document.createElement("div");
sectionWrapper.className += " section_" + section;
if (this.config.sections[section].class) {
sectionWrapper.className += " " + this.config.sections[section].class;
}
sectionWrapper.innerHTML = this.config.sections[section].render;
wrapper.appendChild(sectionWrapper);
}
}
return wrapper;
},
getHeader: function () {
// If the module is configured to use a trigger to control its visibility,
// and the trigger currently indicates the module should be hidden,
// then do not return a title, so no header is rendered.
if ((this.config.useModuleTrigger || this.config.useModuleTigger) && !this.displayModule) {
return null;
}
// Otherwise, return the configured title.
return this.config.title;
},
getScripts: function () {
return [];
},
getStyles: function () {
return ["modules/MMM-HomeAssistantDisplay/node_modules/@mdi/font/css/materialdesignicons.min.css"];
},
// Load translations files
getTranslations: function () {
//FIXME: This can be load a one file javascript definition
return {
en: "translations/en.json",
es: "translations/es.json"
};
},
updateState: function (state) {
if (this.entities.hasOwnProperty(state.entity_id)) {
this.entities[state.entity_id].updateState(state);
}
},
renderTemplates: function (causingEntity) {
if ((this.config.useModuleTrigger || this.config.useModuleTigger) && this.config.moduleTriggerTemplate) {
this.sendSocketNotification("RENDER_MODULE_DISPLAY_TEMPLATE", {
identifier: this.identifier,
template: this.config.moduleTriggerTemplate
});
}
for (const sectionId in this.config.sections) {
this.sendSocketNotification("RENDER_SECTION_DISPLAY_TEMPLATE", {
identifier: this.identifier,
section: sectionId,
template: this.config.sections[sectionId].displayTemplate
});
}
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notification, payload) {
if (payload.identifier === this.identifier) {
switch (notification) {
case "MODULE_DISPLAY_RENDERED":
if (payload.render.toLowerCase() === "true" || payload.render.toLowerCase() === "on") {
this.displayModule = true;
} else {
this.displayModule = false;
}
this.updateDom();
break;
case "CHANGED_STATE":
this.renderTemplates(payload.cause);
this.updateDom();
break;
case "SECTION_DISPLAY_RENDERED":
this.config.sections[payload.section].render = payload.render;
this.updateDom();
break;
case "HASSWS_DISCONNECTED":
this.sendSocketNotification("RECONNECT_WS", this.config);
break;
default:
break;
}
}
}
});