node-red-contrib-huemagic
Version:
Philips Hue node to control bridges, lights, groups, scenes, rules, taps, switches, buttons, motion sensors, temperature sensors and Lux sensors using Node-RED.
219 lines (201 loc) • 8.97 kB
HTML
<script type="text/x-red" data-template-name="hue-light">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="hue-light.config.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]hue-light.config.input-name" style="width: calc(100% - 105px)">
</div>
<div class="form-row">
<label for="node-input-bridge"><i class="fa fa-server"></i> Bridge</label>
<input type="text" id="node-input-bridge" style="width: calc(100% - 105px)">
</div>
<div class="form-row">
<label for="node-input-lightid"><i class="fa fa-lightbulb-o"></i> <span data-i18n="hue-light.config.light"></span></label>
<div style="display: inline-flex; width: calc(100% - 105px)">
<div id="input-select-toggle" style="flex-grow: 1;">
<input type="text" id="node-input-lightid" placeholder="00000000-0000-0000-0000-000000000000" style="width: 100%"/>
</div>
<button id="node-config-input-scan-lights" type="button" class="red-ui-button" style="margin-left: 10px;">
<i class="fa fa-search"></i>
</button>
</div>
</div>
<div class="form-row" style="margin-top: 30px">
<div style="display: inline-flex; width: calc(100% - 105px)">
<input type="checkbox" id="node-input-colornamer" style="flex: 15px;">
<span data-i18n="hue-light.config.colornamer-info" style="width: 100%; margin-left: 10px;"></span>
</div>
</div>
<div class="form-row">
<div style="display: inline-flex; width: calc(100% - 105px)">
<input type="checkbox" id="node-input-skipevents" style="flex: 15px;">
<span data-i18n="hue-light.config.skipevents-node" style="width: 100%; margin-left: 10px;"></span>
</div>
</div>
<div class="form-row">
<div style="display: inline-flex; width: calc(100% - 105px)">
<input type="checkbox" id="node-input-initevents" style="flex: 15px;">
<span data-i18n="hue-light.config.sendinitevents-node" style="width: 100%; margin-left: 10px;"></span>
</div>
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType('hue-light',{
category: 'HueMagic',
color: '#47c0e8',
defaults: {
name: { value:"" },
bridge: { type: "hue-bridge", required: true },
lightid: { value:"", required: false },
colornamer: { value: true },
skipevents: { value: false },
initevents: { value: false }
},
align: 'left',
icon: "hue-light.png",
inputs: 1,
outputs: 1,
label: function() {
return this.name || this._("hue-light.node.title");
},
paletteLabel: function() {
return this._("hue-light.node.title");
},
inputLabels: function() {
return this._("hue-light.node.input");
},
outputLabels: function() {
return this._("hue-light.node.output");
},
oneditprepare: function()
{
const scope = this;
let options = [];
function manualInput()
{
// GET CURRENT SELECTED VALUE
var current = $('#node-input-lightid').val();
// REMOVE SELECT FIELD
$('#input-select-toggle').empty();
// CREATE NEW INPUT FIELD
$('#input-select-toggle').append('<input type="text" id="node-input-lightid" placeholder="00000000-0000-0000-0000-000000000000" style="width: 100%" value="'+current+'" />');
// CHANGE BUTTON ICON
var button = $("#node-config-input-scan-lights");
var buttonIcon = button.find("i");
buttonIcon.removeClass("fa-pencil");
buttonIcon.addClass("fa-search");
}
function sortResourcesBy(prop, resources)
{
return resources.sort((a, b) => {
if ( a[prop] < b[prop] ){
return -1;
}
if ( a[prop] > b[prop] ){
return 1;
}
return 0;
});
}
function searchAndSelect()
{
// GET CURRENT BRIDGE CONFIGURATION
var bridgeConfig = RED.nodes.node($('#node-input-bridge option:selected').val());
if(!bridgeConfig) { return false; }
// GET CURRENT SELECTED VALUE
var current = $('#node-input-lightid').val();
// TRIGGER SEARCHING NOTIFICATION
var notification = RED.notify(scope._("hue-light.config.searching"), { type: "compact", modal: true, fixed: true });
// GET THE LIGHTS
$.get('hue/resources', { bridge: bridgeConfig.bridge, key: bridgeConfig.key, type: "light" })
.done( function(data) {
var allResources = JSON.parse(data);
allResources = sortResourcesBy('name', allResources);
if(allResources.length <= 0)
{
notification.close();
RED.notify(scope._("hue-light.config.none-found"), { type: "error" });
return false;
}
// SET OPTIONS
allResources.forEach(function(resource)
{
if(resource.model)
{
options[resource.id] = { value: resource.id, label: resource.name + " ("+resource.model+")" };
}
else
{
options[resource.id] = { value: resource.id, label: resource.name };
}
});
// SELECT CURRENT VALUE
$("#node-input-lightid").typedInput({
types: [
{
value: current,
options: Object.values(options)
}
]
});
// CHANGE BUTTON ICON
var button = $("#node-config-input-scan-lights");
var buttonIcon = button.find("i");
buttonIcon.removeClass("fa-search");
buttonIcon.addClass("fa-pencil");
// CLOSE SEARCH NOTIFICATION
notification.close();
})
.fail(function()
{
notification.close();
RED.notify(scope._("hue-light.config.unknown-error"), "error");
});
}
// CHANGED LIGHT ID? -> REPLACE NAME (IF POSSIBLE)
$(document).on('change', '#node-input-lightid', function(e)
{
let currentSelectedOptionID = $(e.currentTarget).val();
let currentSelectedOptionValue = (currentSelectedOptionID.length > 0 && options[currentSelectedOptionID]) ? options[currentSelectedOptionID].label : false;
if(currentSelectedOptionValue !== false)
{
$('#node-input-name').val(currentSelectedOptionValue.split(" (")[0]);
}
});
// TOGGLE SELECT/INPUT FIELD
$('#node-config-input-scan-lights').click(function()
{
if($('#input-select-toggle').find(".red-ui-typedInput-container").length > 0)
{
manualInput();
}
else
{
searchAndSelect();
}
});
},
button: {
enabled: function() {
return (this.lightid && this.lightid.length > 1)
},
visible: function() {
return (this.lightid && this.lightid.length > 1)
},
onclick: function()
{
const node = this;
if(node.bridge)
{
$.ajax({
url: "inject/" + node.id,
type: "POST",
data: JSON.stringify({ __user_inject_props__: "status"}),
contentType: "application/json; charset=utf-8",
success: function (resp) {
RED.notify(node.name + ": " + node._("hue-light.node.statusmsg"), { type: "success", id: "status", timeout: 2000 });
}
});
}
}
}
});
</script>