-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-Yeelight.js
135 lines (115 loc) · 4.16 KB
/
MMM-Yeelight.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
/**
* @file MMM-Yeelight.js
*
* @author slametps
* @license MIT
*
* @see https://github.com/slametps/MMM-Yeelight
*/
Module.register("MMM-Yeelight", {
// Default module config.
defaults: {
colour: false,
updateInterval: 600 * 1000,
showOnlyOn: false,
showLabel: true,
timeout: 3000,
animationSpeed: 2.5 * 1000, // Speed of the update animation. (milliseconds)
devicesInfo: []
},
getStyles: function () {
return ["font-awesome.css", "MMM-Yeelight.css"];
},
// Define required translations.
getTranslations: function() {
return {
'en': 'translations/en.json',
'id': 'translations/id.json'
};
},
// Define start sequence.
start: function () {
var self = this;
var devices = [];
this.getData();
setInterval(() => {
this.getData();
}, self.config.updateInterval);
},
// Override dom generator.
getDom: function () {
var wrapper = document.createElement("div");
var self = this;
if (self.devices && self.devices.length > 0) {
var table = document.createElement("table");
table.classList.add("small", "table", "align-left");
if (this.config.showLabel)
table.appendChild(this.createLabelRow());
for (var i = 0; i < self.devices.length; i++) {
if (self.config.showOnlyOn) {
if (self.devices[i].on_off == 1) {
domAction(self.devices[i], self.config);
}
}
else {
domAction(self.devices[i], self.config);
}
}
function domAction(device, config) {
var row = document.createElement("tr");
var room = document.createElement("td");
console.debug(device.alias);
room.innerHTML = device.alias;
row.appendChild(room);
var lightsallLabel = document.createElement("td");
lightsallLabel.classList.add("centered");
var lightstatus = document.createElement("i");
lightstatus.classList.add("fa", device.on_off ? (device.type === "bulb" ? "fa-lightbulb-o" : "fa-plug") : "fa-times");
if (config.colour) {
lightstatus.classList.add("lights-all-on");
}
lightsallLabel.appendChild(lightstatus);
row.appendChild(lightsallLabel);
table.appendChild(row);
}
wrapper.appendChild(table);
} else {
wrapper.innerHTML = this.translate("NO_DATA");
wrapper.className = "dimmed light small";
}
return wrapper;
},
createLabelRow: function () {
var labelRow = document.createElement("tr");
var roomiconlabel = document.createElement("th");
var typeIcon = document.createElement("room");
typeIcon.classList.add("fa", "fa-home");
roomiconlabel.appendChild(typeIcon);
labelRow.appendChild(roomiconlabel);
var lightsonlabel = document.createElement("th");
lightsonlabel.classList.add("centered");
var typeIcon = document.createElement("lightson");
//typeIcon.classList.add("fa", "fa-lightbulb-o");
typeIcon.innerHTML = this.translate("LIGHTS_ON");
lightsonlabel.appendChild(typeIcon);
labelRow.appendChild(lightsonlabel);
var lightsonlabel = document.createElement("th");
lightsonlabel.classList.add("centered");
return labelRow;
},
getData: function () {
this.notificationReceived('YEELIGHT_NETWORK_SEARCH');
},
notificationReceived: function(notification, payload, sender) {
console.log(this.name + " received " + notification + " notification");
this.sendSocketNotification(notification, {config: this.config, payload: payload});
},
socketNotificationReceived: function(notification, payload) {
var self = this;
console.log(this.name + " received " + notification + " socket notification");
if (notification == 'YEELIGHT_NETWORK_SEARCH_RESULT') {
this.devices = payload.devices;
this.updateDom(self.config.animationSpeed);
}
}
});