Skip to content

Commit

Permalink
feat(trigger-state): More options for custom outputs
Browse files Browse the repository at this point in the history
msg.payload can now be customized instead of the whole message.
There is now the option to select the output format.
Templates can be used within the custom output. The default context for
the template is event data (msg.data)
  • Loading branch information
zachowj committed May 20, 2019
1 parent c8b2e09 commit 3df9c18
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 35 deletions.
65 changes: 42 additions & 23 deletions nodes/trigger-state/trigger-state.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@

messageType: $("#output-message-type"),
messageValue: $("#output-message-value"),
messageValueType: $("#output-message-value-type"),

comparatorPropertyType: $("#output-comparator-property-type"),
comparatorPropertyValue: $("#output-comparator-property-value"),
Expand All @@ -87,6 +88,8 @@
.slice(2)
};

let availableProperties = [];
let availablePropertiesPrefixes = [];
haServer.init(node, "#node-input-server");
haServer.autocomplete("entities", entities => {
node.availableEntities = entities;
Expand All @@ -100,28 +103,26 @@
});
});
haServer.autocomplete("properties", properties => {
node.availableProperties = properties;
availableProperties = properties;
// prefix properties with new_state and old_state
node.availablePropertiesPrefixes = []
availablePropertiesPrefixes = []
.concat(
properties.map(e => `new_state.${e}`),
properties.map(e => `old_state.${e}`)
)
.sort();
$("#constraint-property-value").autocomplete({
source: node.availablePropertiesPrefixes,
minLength: 0
});
$("#output-comparator-property-value").autocomplete({
source: node.availableProperties,
$(
"#constraint-property-value,#output-comparator-property-value"
).autocomplete({
source: availablePropertiesPrefixes,
minLength: 0
});
$("#constraint-target-type").on("change", function() {
$("#constraint-property-value").autocomplete("option", {
source:
$(this).val() === "this_entity"
? node.availablePropertiesPrefixes
: node.availableProperties
? availablePropertiesPrefixes
: availableProperties
});
});
});
Expand Down Expand Up @@ -263,6 +264,7 @@

messageType: $customoutputs.messageType.val(),
messageValue: $customoutputs.messageValue.val(),
messageValueType: $customoutputs.messageValueType.val(),

comparatorPropertyType: $customoutputs.comparatorPropertyType.val(),
comparatorPropertyValue: $customoutputs.comparatorPropertyValue.val(),
Expand Down Expand Up @@ -306,7 +308,14 @@
""
)} ${d.comparatorValue}`;

const html = `Send <strong>${messageText}</strong>, if <strong>${sendWhenText}</strong>`;
const where =
d.messageType === "default"
? "Send"
: d.messageType === "custom"
? "Msg"
: "Payload";

const html = `${where} <strong>${messageText}</strong>, if <strong>${sendWhenText}</strong>`;

$row.html(html);
},
Expand Down Expand Up @@ -352,9 +361,9 @@
},
onMessageTypeChange: function(e) {
const val = e.target.value;
val === "default"
? $customoutputs.messageValue.attr("disabled", "disabled")
: $customoutputs.messageValue.removeAttr("disabled");
$customoutputs.messageValue.typedInput(
val === "default" ? "hide" : "show"
);
},
comparatorPropertyTypeChange: function(e) {
const val = e.target.value;
Expand Down Expand Up @@ -392,6 +401,14 @@
"change",
outputsHandler.onMessageTypeChange
);

$customoutputs.messageValue.typedInput({
default: "json",
types: ["str", "num", "bool", "json"],
typeField: "#output-message-value-type"
});
$customoutputs.messageType.trigger("change");

$customoutputs.comparatorPropertyType.on(
"change",
outputsHandler.comparatorPropertyTypeChange
Expand Down Expand Up @@ -555,21 +572,23 @@ <h3>Add Outputs</h3>
<div class="form-row">
<!-- Type -->
<select type="text" id="output-message-type" style="width: 140px;">
<option value="default"> Default Msg </option>
<option value="custom"> Custom Msg </option>
<option value="default">Default Msg</option>
<option value="custom">Custom Msg</option>
<option value="payload">Custom Payload</option>
</select>

<input type="text" id="output-message-value" style="width: 62%" disabled/>
<input type="text" id="output-message-value" style="width: 62%"/>
<input type="hidden" id="output-message-value-type" />
</div>


<!-- Output Comparator Selection -->
<div class="form-row">
<select type="text" id="output-comparator-property-type" style="width: 140px">
<option value="always"> Send Always </option>
<option value="current_state"> If State </option>
<option value="previous_state"> If Prev State </option>
<option value="property"> If Property </option>
<option value="always">Send Always</option>
<option value="current_state">If State</option>
<option value="previous_state">If Prev State</option>
<option value="property">If Property</option>
</select>

<input type="text" id="output-comparator-property-value" style="width: 62%" disabled />
Expand All @@ -578,8 +597,8 @@ <h3>Add Outputs</h3>
<div class="form-row">
<!-- Type -->
<select type="text" id="output-comparator-type" style="width: 140px;" disabled>
<option value="is"> Is </option>
<option value="is_not"> Is Not </option>
<option value="is">Is</option>
<option value="is_not">Is Not</option>

<option value=">">&gt;</option>
<option value=">=">&gt;=</option>
Expand Down
51 changes: 39 additions & 12 deletions nodes/trigger-state/trigger-state.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable camelcase */
const EventsNode = require('../../lib/events-node');
const { reduce } = require('p-iteration');
const RenderTemplate = require('../../lib/mustache-context');

module.exports = function(RED) {
const nodeOptions = {
Expand Down Expand Up @@ -43,7 +44,6 @@ module.exports = function(RED) {
});

this.NUM_DEFAULT_MESSAGES = 2;
this.messageTimers = {};

this.loadPersistedData();

Expand Down Expand Up @@ -365,19 +365,46 @@ module.exports = function(RED) {
return null;
}

if (output.messageType === 'default') {
return {
topic: eventMessage.entity_id,
payload: eventMessage.event.new_state.state,
data: eventMessage
};
}
let payload = eventMessage.event.new_state.state;
if (
output.messageType === 'custom' ||
output.messageType === 'payload'
) {
// Render Template Variables
payload = RenderTemplate(
output.messageValue,
eventMessage.event,
this.node.context(),
this.utils.toCamelCase(this.nodeConfig.server.name)
);

try {
return JSON.parse(output.messageValue);
} catch (e) {
return output.messageValue;
switch (output.messageValueType) {
case 'num':
payload = Number(payload);
break;
case 'bool':
payload = payload === 'true';
break;
case 'str':
break;
case 'json':
default:
try {
payload = JSON.parse(payload);
} catch (e) {}
break;
}

if (output.messageType === 'custom') {
return payload;
}
}

return {
topic: eventMessage.entity_id,
payload,
data: eventMessage
};
}

async loadPersistedData() {
Expand Down

0 comments on commit 3df9c18

Please sign in to comment.