UNPKG

node-red-contrib-mi-sensors

Version:

A set of nodes to control some of the popular Xiaomi sensors which are connected to the Xiaomi Gateway, and the Gateway itself.

125 lines (116 loc) 4.95 kB
<script type="text/javascript"> RED.nodes.registerType('xiaomi-all', { category: 'xiaomi', color: '#3FADB5', defaults: { gateway: {value:"", type:"xiaomi-configurator"}, name: {value: ""}, onlyModels: {value: []}, excludedSids: { value: []} }, inputs: 1, outputs: 1, outputLabels: ["All devices"], paletteLabel: "all", icon: "mi-all.png", label: function () { return this.name || "xiaomi-all"; }, oneditprepare: function() { var node = this; function getOnlyModelsValue(input) { var cleanOnlyModels = []; input.forEach((value) => { cleanOnlyModels = cleanOnlyModels.concat(value.split(',')); }); return cleanOnlyModels; } function changeGateway(gateway, onlyModels, excludedSids) { var configNodeID = gateway || $('#node-input-gateway').val(); if (configNodeID) { var configNode = RED.nodes.node(configNodeID); if(configNode) { onlyModels = getOnlyModelsValue(onlyModels || $('#node-input-onlyModels').val() || []); excludedSids = excludedSids || $('#node-input-excludedSids').val() || []; $('#node-input-excludedSids').empty(); for (key in configNode.deviceList) { var device = configNode.deviceList[key]; if (onlyModels.length == 0 || onlyModels.indexOf(device.model) >= 0) { var option = $('<option value="' + device.sid + '">' + device.desc + '</option>'); if(excludedSids && excludedSids.indexOf(device.sid) >= 0) { option.prop('selected', true); } $('#node-input-excludedSids').append(option); } } } } } changeGateway(this.gateway, this.onlyModels, this.excludedSids); $("#node-input-gateway, #node-input-onlyModels").change(function () { changeGateway(); }); }, oneditsave: function() { if(!$('#node-input-onlyModels').val()) { this.onlyModels = []; } if(!$('#node-input-excludedSids').val()) { this.excludedSids = []; } } }); </script> <script type="text/x-red" data-template-name="xiaomi-all"> <div class="form-row"> <label for="node-input-gateway"><i class="icon-tag"></i> Gateway</label> <input type="text" id="node-input-gateway" placeholder="xiaomi gateway"> </div> <div class="form-row"> <label for="node-input-name"><i class="icon-tag"></i> Name</label> <input type="text" id="node-input-name" placeholder="Name"> </div> <hr /> <h5>Filters</h5> <div class="form-row"> <label for="node-input-onlyModels"><i class="icon-tag"></i> Only</label> <select multiple id="node-input-onlyModels"> <option value="sensor_ht,weather.v1">Temperature/humidty</option> <option value="motion">Motion</option> <option value="switch,sensor_switch.aq2">Switches</option> <option value="magnet,sensor_magnet.aq2">Contacts</option> <option value="plug">Plugs</option> </select> </div> <div class="form-row"> <label for="node-input-excludedSids"><i class="icon-tag"></i> Exclude</label> <select multiple id="node-input-excludedSids" size=10></select> </div> </script> <script type="text/x-red" data-help-name="xiaomi-all"> <p>All devices registred in the gateway, except gateway itself.</p> <h3>Inputs</h3> <dl class="message-properties"> <dt>payload <span class="property-type">object</span> </dt> <dd>When use as an incoming filter node, <code>sid</code> and <code>model</code> are mandatory.</dd> </dl> <h3>Outputs</h3> <ol class="node-ports"> <li>Devices output <dl class="message-properties"> <dt>payload <span class="property-type">array</span></dt> <dd>Array of devices.</dd> </dl> </li> </ol> <h4>Details</h4> <p>Sample payload:</p> <p><pre>[ {sid: "128d0901db1fa8", desc: "Door sensor" model: "magnet"}, {sid: "151d0401ab2491", desc: "Heat sensor", model: "sensor_ht"}, {sid: "658d030171427c", desc: "Button", model: "switch"} ]</pre> </p> </script>