Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
314 changes: 169 additions & 145 deletions MMM-Hue.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,150 +9,174 @@

Module.register("MMM-Hue", {

// Default module config.
defaults: {
bridgeip: "",
userid: "",
colour: false,
refreshTime: 60 * 10000,
lightsorgroups: "groups",
showOnlyOn: false,
showLabel: true,
hideSpecificGroups: false,
hideGroupsWithString: 'hgrp'

},
// Define required scripts.
getScripts: function () {
return [this.file("js/jquery.js")];
},
getStyles: function () {

return ["font-awesome.css", "MMM-Hue.css"];
},
// Define required translations.
getTranslations: function() {
return {
'en': 'translations/en.json',
'id': 'translations/id.json'
};
},
// Define start sequence.
start: function () {
//These will be moved to config in a later release
this.lightsorgroups = this.config.lightsorgroups;
this.updateInterval = this.config.refreshTime; // updates every 10 minutes
this.animationSpeed = 2 * 1000;
this.initialLoadDelay = 0;
//end
var result = false;
this.url = "http://" + this.config.bridgeip + "/api/" + this.config.userid + "/" + this.lightsorgroups;
this.getData();
setInterval(() => {
this.getData();
}, this.updateInterval);
},

// Override dom generator.
getDom: function () {
var wrapper = document.createElement("div");
//alert("http://" + this.config.bridgeip + "/api/" + this.config.userid + "/" + this.config.lightsorgroups);

if (this.result) {

var table = document.createElement("table");
table.classList.add("small", "table", "align-left");

if (this.config.showLabel)
table.appendChild(this.createLabelRow());

var lamps = Object.keys(this.result);

for (var i = 0; i < lamps.length; i++) {
var groupName = this.result[lamps[i]].name;
console.debug(groupName, groupName.includes('hgrp'));

if (this.config.showOnlyOn) {
if (this.config.hideSpecificGroups && !groupName.includes(this.config.hideGroupsWithString)) {
if (this.result[lamps[i]].state.all_on || this.result[lamps[i]].state.any_on) {
domAction(this.result,lamps[i],this.config);
}
} else if (!this.config.hideSpecificGroups) {
domAction(this.result,lamps[i],this.config);
}
} else {
if (this.config.hideSpecificGroups && !groupName.includes(this.config.hideGroupsWithString)) {
domAction(this.result,lamps[i],this.config);
} else if (!this.config.hideSpecificGroups) {
domAction(this.result,lamps[i],this.config);
}
}


}

function domAction(result, lamp, config) {
var row = document.createElement("tr");
var room = document.createElement("td");
console.debug(result[lamp]);
room.innerHTML = result[lamp].name;
row.appendChild(room);
var lightsallLabel = document.createElement("td");
lightsallLabel.classList.add("centered");

var lightstatus = document.createElement("i");
lightstatus.classList.add("fa", result[lamp].state.all_on ? "fa-lightbulb-o" : (result[lamp].state.any_on ? "fa-adjust" : "fa-times"));
if (config.colour) {

if (result[lamp].state.all_on) {
lightstatus.classList.add("lights-all-on")
}
else {
if (result[lamp].state.any_on) {
lightstatus.classList.add("lights-partial-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;
// Default module config.
defaults: {
bridgeip: "",
userid: "",
colour: false,
refreshTime: 60 * 10000,
lightsorgroups: "groups",
showOnlyOn: false,
showLabel: true,
hideSpecificGroups: false,
hideGroupsWithString: 'hgrp'

},
// Define required scripts.
getScripts: function () {
return [this.file("js/jquery.js")];
},
getStyles: function () {

return ["font-awesome.css", "MMM-Hue.css"];
},
// Define required translations.
getTranslations: function() {
return {
'en': 'translations/en.json',
'id': 'translations/id.json'
};
},

getCommands: function(commander) {
commander.add({
command: 'hue',
description: this.translate("TXT_HUE_DESC"),
callback: 'cmd_hue'
});
},

cmd_hue: function(command, handler) {
var text = "*" + this.translate("TXT_HUE") + "*\n";
if (this.result) {
var lamps = Object.keys(this.result);
for (var i = 0; i < lamps.length; i++) {
text += "*" + this.result[lamps[i]].name + "*: ";
text += "`" + (this.result[lamps[i]].state.all_on ? this.translate("TXT_HUE_ON") : (this.result[lamps[i]].state.any_on ? this.translate("TXT_HUE_ON_PARTIAL") : this.translate("TXT_HUE_OFF"))) + "`\n";
}
}
,
getData: function () {
$.getJSON(this.url, (data) => {
this.result = data;
this.updateDom();
});
},
else {
text = this.translate("NO_DATA") + "\n";
}
handler.reply("TEXT", text, {parse_mode:'Markdown'});
},

// Define start sequence.
start: function () {
//These will be moved to config in a later release
this.lightsorgroups = this.config.lightsorgroups;
this.updateInterval = this.config.refreshTime; // updates every 10 minutes
this.animationSpeed = 2 * 1000;
this.initialLoadDelay = 0;
//end
var result = false;
this.url = "http://" + this.config.bridgeip + "/api/" + this.config.userid + "/" + this.lightsorgroups;
this.getData();
setInterval(() => {
this.getData();
}, this.updateInterval);
},

// Override dom generator.
getDom: function () {
var wrapper = document.createElement("div");
//alert("http://" + this.config.bridgeip + "/api/" + this.config.userid + "/" + this.config.lightsorgroups);

if (this.result) {

var table = document.createElement("table");
table.classList.add("small", "table", "align-left");

if (this.config.showLabel)
table.appendChild(this.createLabelRow());

var lamps = Object.keys(this.result);

for (var i = 0; i < lamps.length; i++) {
var groupName = this.result[lamps[i]].name;
console.debug(groupName, groupName.includes('hgrp'));

if (this.config.showOnlyOn) {
if (this.config.hideSpecificGroups && !groupName.includes(this.config.hideGroupsWithString)) {
if (this.result[lamps[i]].state.all_on || this.result[lamps[i]].state.any_on) {
domAction(this.result,lamps[i],this.config);
}
} else if (!this.config.hideSpecificGroups) {
domAction(this.result,lamps[i],this.config);
}
} else {
if (this.config.hideSpecificGroups && !groupName.includes(this.config.hideGroupsWithString)) {
domAction(this.result,lamps[i],this.config);
} else if (!this.config.hideSpecificGroups) {
domAction(this.result,lamps[i],this.config);
}
}


}

function domAction(result, lamp, config) {
var row = document.createElement("tr");
var room = document.createElement("td");
console.debug(result[lamp]);
room.innerHTML = result[lamp].name;
row.appendChild(room);
var lightsallLabel = document.createElement("td");
lightsallLabel.classList.add("centered");

var lightstatus = document.createElement("i");
lightstatus.classList.add("fa", result[lamp].state.all_on ? "fa-lightbulb-o" : (result[lamp].state.any_on ? "fa-adjust" : "fa-times"));
if (config.colour) {

if (result[lamp].state.all_on) {
lightstatus.classList.add("lights-all-on")
}
else {
if (result[lamp].state.any_on) {
lightstatus.classList.add("lights-partial-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 () {
$.getJSON(this.url, (data) => {
this.result = data;
this.updateDom();
});
},
});
7 changes: 6 additions & 1 deletion translations/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"LIGHTS_ON": "Lights On",
"NO_DATA": "No Data returned"
"NO_DATA": "No Data returned",
"TXT_HUE": "PHILIPS HUE STATUS",
"TXT_HUE_DESC": "display all room status of installed philips hue",
"TXT_HUE_ON": "ON",
"TXT_HUE_ON_PARTIAL": "ON (partially)",
"TXT_HUE_OFF": "OFF"
}
7 changes: 6 additions & 1 deletion translations/id.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"LIGHTS_ON": "Nyala",
"NO_DATA": "Tidak ada data"
"NO_DATA": "Tidak ada data",
"TXT_HUE": "STATUS PHILIPS HUE",
"TXT_HUE_DESC": "menampilkan semua status (ruangan/lampu) philips hue terpasang",
"TXT_HUE_ON": "NYALA",
"TXT_HUE_ON_PARTIAL": "NYALA (sebagian)",
"TXT_HUE_OFF": "PADAM"
}