UNPKG

node-red-contrib-hubitat

Version:
206 lines (195 loc) 8.58 kB
<script src="hubitat/js/common.js"></script> <script type="text/javascript"> /* global cleanHubitatSelectMenu, resetHubitatSelectMenu, cleanHubitatDevices, listHubitatDevices, loadCredentials, paramsFromServer */ /* eslint no-underscore-dangle: ["error", { "allow": ["_config"] }] */ function listHubitatDeviceCommands(server, deviceId, command) { const selectMenu = resetHubitatSelectMenu('node-input-command', 'Choose command...'); $.getJSON(`hubitat/devices/${deviceId}/commands`, paramsFromServer(server), (res) => { const uniqueCommands = res.reduce((accumulator, value) => { if (!(value.command in accumulator) || (value.type.length && value.type[0] !== 'n/a')) { accumulator[value.command] = value; } return accumulator; }, {}); Object.values(uniqueCommands).forEach((item) => { selectMenu.append($('<option>', { value: item.command, text: item.command, args_placeholder: item.type.join(','), })); }); if (command in uniqueCommands) { selectMenu.val(command).trigger('change'); } }); } function migrateFromVersionsPriorTo1_10_0(node) { // eslint-disable-line camelcase if (!node._config) { // new node return false; } if (node._config.ignoreOverrides === undefined && !node.changed) { return true; } return false; } RED.nodes.registerType('hubitat command', { category: 'hubitat', color: '#ACE043', defaults: { deviceLabel: { value: '' }, name: { value: '' }, server: { type: 'hubitat config', required: true }, deviceId: { value: '' }, command: { value: '' }, commandArgs: { value: '' }, ignoreOverrides: { value: true }, }, inputs: 1, outputs: 1, align: 'right', icon: 'command.svg', label() { return this.name || this.deviceLabel || 'command'; }, paletteLabel: 'command', oneditprepare() { $('document').ready(() => { $('#node-input-deviceLabel').hide(); $('#node-input-server').change(() => { const serverId = $('#node-input-server option:selected').val(); const server = RED.nodes.node(serverId); if (server) { loadCredentials(server).then(() => { listHubitatDevices(server, this.deviceId); }); } else { cleanHubitatDevices(); } }); $('#node-input-deviceId').change(() => { const deviceLabel = $('#node-input-deviceId option:selected').text(); const deviceId = $('#node-input-deviceId option:selected').val(); if (deviceId) { $('#node-input-name').attr('placeholder', deviceLabel); if (!this.name) { $('#node-input-deviceLabel').val(deviceLabel); } } const serverId = $('#node-input-server option:selected').val(); const server = RED.nodes.node(serverId); if (server && deviceId) { loadCredentials(server).then(() => { listHubitatDeviceCommands(server, deviceId, this.command); }); } else { cleanHubitatSelectMenu('node-input-command', 'Choose command...'); $('#node-input-deviceLabel').val(''); $('#node-input-name').attr('placeholder', 'command'); } }); $('#node-input-name').change(() => { const name = $('#node-input-name').val(); const deviceId = $('#node-input-deviceId option:selected').val(); if ((!name) && (deviceId)) { const deviceLabel = $('#node-input-deviceId option:selected').text(); $('#node-input-deviceLabel').val(deviceLabel); } }); $('#node-input-command').change(() => { let placeholder = $('#node-input-command option:selected').attr('args_placeholder'); const commandArgsInput = $('#node-input-commandArgs'); if (placeholder === 'n/a') { commandArgsInput.val(''); commandArgsInput.prop('disabled', true); } else { commandArgsInput.prop('disabled', false); } if (placeholder === 'COLOR_MAP') { placeholder = '{"hue":88,"saturation":50,"level":90} or {"hex":"15fe21"}'; } commandArgsInput.attr('placeholder', placeholder); }); const ignoreOverrides = $('#node-input-ignoreOverrides'); if (migrateFromVersionsPriorTo1_10_0(this)) { ignoreOverrides.prop('checked', false); } }); }, }); </script> <script type="text/html" data-template-name="hubitat command"> <div class="form-row"> <label for="node-input-server"><i class="fa fa-globe"></i> Server</label> <input type="text" id="node-input-server"> </div> <div class="form-row"> <label for="node-input-deviceId"><i class="fa fa-lightbulb-o"></i> Device</label> <select id="node-input-deviceId"></select> </div> <div class="form-row"> <label for="node-input-command"><i class="fa fa-hashtag"></i> Command</label> <select id="node-input-command"></select> </div> <div class="form-row"> <label for="node-input-commandArgs"><i class="fa fa-bars"></i> Arguments</label> <input type="text" id="node-input-commandArgs"> </div> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <input type="text" id="node-input-name" placeholder="Name"> </div> <div class="form-row"> <label for="node-input-ignoreOverrides" style="width: auto"><i class="fa fa-ban"></i> Ignore Input Overrides </label> <input type="checkbox" id="node-input-ignoreOverrides" style="display: inline-block; width: auto; vertical-align: top;" checked> </div> <!-- to be handled by NR framework--> <div class="form-row"> <label for="node-input-deviceLabel"></label> <input type="text" id="node-input-deviceLabel"> </div> </script> <script type="text/html" data-help-name="hubitat command"> <p>A node that send command to Hubitat.</p> <h3>Inputs</h3> <dl class="message-properties"> <dt class="optional">msg.deviceId <span class="property-type">string</span></dt> <dd>Allow to overwrite the device.</dd> </dl> <dl class="message-properties"> <dt class="optional">msg.command <span class="property-type">string</span></dt> <dd>Allow to overwrite the command sent.</dd> </dl> <dl class="message-properties"> <dt class="optional">msg.arguments <span class="property-type">string</span></dt> <dd>Allow to overwrite the command arguments sent. You must remove this property if you send a command that doesn't require an argument</dd> </dl> <h3>Output</h3> <dl class="message-properties"> <dt class="optional">msg.responseStatus <span class="property-type">integer</span></dt> <dd>The HTTP response return code from the server. Usually 200 if the command is executed and 500 if something went wrong on the server. Other codes may be returned according to Hubitat implementation. </dd> </dl> <dl class="message-properties"> <dt class="optional">msg.response <span class="property-type">string</span></dt> <dd>The raw response of the server. Usually the device object in json format if <code>msg.responseStatus</code> is 200. Otherwise, a raw string of the error</dd> </dl> <dl class="message-properties"> <dt class="optional">msg.requestCommand <span class="property-type">string</span></dt> <dd>The command requested by the node</dd> </dl> <dl class="message-properties"> <dt class="optional">msg.requestArguments <span class="property-type">string</span></dt> <dd>The arguments requested by the node</dd> </dl> <h3>Details</h3> <p><b>Device</b> dropdown is populated when the server is reachable.</p> <p><b>Command</b> dropdown is populated when the <b>Device</b> is selected. If not specified, input message must define <code>command</code> property</p> <p><b>Arguments</b> field is disabled or placeholder is set according to the chosen <b>Command</b>. If not specified, input message must define <code>arguments</code> property. To send many arguments, use the following syntax: <code>{"hue":88,"saturation":50,"level":90}</code>. </p> <p><b>Ignore Input Overrides</b> ignore input message properties (i.e. <code>msg.deviceId</code>, <code>msg.command</code> and <code>msg.arguments</code>).</p> </script>