Skip to content

Commit

Permalink
feat: Access to msg, flow and global context
Browse files Browse the repository at this point in the history
Able to access flow and global context from the state-change, poll-state
nodes. Able to access msg, flow and global context from current-state,
wait-until and get-entities nodes.
  • Loading branch information
zachowj committed Mar 9, 2019
1 parent 1d2bde8 commit e1ce911
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 17 deletions.
29 changes: 24 additions & 5 deletions lib/base-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,21 @@ class BaseNode {
}
}

getContextValue(location, property, message) {
if (message && location === 'msg') {
return this.RED.util.getMessageProperty(message, property);
}

const contextKey = this.RED.util.parseContextStore(property);
return this.context()[location].get(contextKey.key, contextKey.store);
}

async getComparatorResult(
comparatorType,
comparatorValue,
actualValue,
comparatorValueDatatype
comparatorValueDatatype,
message
) {
if (
comparatorType === 'includes' ||
Expand All @@ -293,10 +303,19 @@ class BaseNode {
comparatorValue = comparatorValue === 'true';
}

const cValue = this.getCastValue(
comparatorValueDatatype,
comparatorValue
);
let cValue;
if (['msg', 'flow', 'global'].includes(comparatorValueDatatype)) {
cValue = this.getContextValue(
comparatorValueDatatype,
comparatorValue,
message
);
} else {
cValue = this.getCastValue(
comparatorValueDatatype,
comparatorValue
);
}

switch (comparatorType) {
case 'is':
Expand Down
7 changes: 5 additions & 2 deletions nodes/_static/haltif.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const $ = window.jQuery;
const defaultTypes = ['str', 'num', 'bool', 're'];

const resizeHaltIf = function(input) {
const $clearHaltIf = $('#clearHaltIf');
Expand All @@ -17,7 +16,11 @@ const resizeHaltIf = function(input) {

window.resizeHaltIf = resizeHaltIf;

window.setupHaltIf = function(input, compare) {
window.setupHaltIf = function(
input,
compare,
defaultTypes = ['str', 'num', 'bool', 're', 'flow', 'global']
) {
const $input = $(input);
const $compare = $(compare);
const $help = $('#halt_if_help');
Expand Down
10 changes: 9 additions & 1 deletion nodes/current-state/current-state.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,15 @@
}

$.getScript("/homeassistant/static/haltif.js", function() {
setupHaltIf("#node-input-halt_if", "#node-input-halt_if_compare");
setupHaltIf("#node-input-halt_if", "#node-input-halt_if_compare", [
"str",
"num",
"bool",
"re",
"msg",
"flow",
"global"
]);
});
},
oneditsave: function() {
Expand Down
3 changes: 2 additions & 1 deletion nodes/current-state/current-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ module.exports = function(RED) {
this.nodeConfig.halt_if_compare,
this.nodeConfig.halt_if,
currentState.state,
this.nodeConfig.halt_if_type
this.nodeConfig.halt_if_type,
message
);
const shouldHaltIfState = this.nodeConfig.halt_if && isHaltValid;

Expand Down
14 changes: 11 additions & 3 deletions nodes/get-entities/get-entities.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@
{ value: "starts_with", text: "starts with" },
{ value: "in_group", text: "in group" }
];
const defaultTypes = ["str", "num", "bool", "re"];
const defaultTypes = [
"str",
"num",
"bool",
"re",
"msg",
"flow",
"global"
];
const $logic = $("#logic");

if (!NODE.server) {
Expand Down Expand Up @@ -132,13 +140,13 @@
case "lte":
case "gt":
case "gte":
types = ["num"];
types = ["num", "msg", "flow", "global"];
break;
case "includes":
case "does_not_include":
case "starts_with":
case "in_group":
types = ["str"];
types = ["str", "msg", "flow", "global"];
break;
}
$value.typedInput("types", types);
Expand Down
3 changes: 2 additions & 1 deletion nodes/get-entities/get-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ module.exports = function(RED) {
rule.logic,
rule.value,
value,
rule.valueType
rule.valueType,
message
);
if (value === undefined || !result) {
return false;
Expand Down
14 changes: 11 additions & 3 deletions nodes/wait-until/wait-until.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,15 @@
$("#node-input-checkCurrentState").prop("checked", false);
}

const defaultTypes = ["str", "num", "bool", "re"];
const defaultTypes = [
"str",
"num",
"bool",
"re",
"msg",
"flow",
"global"
];
$("#node-input-value")
.typedInput({
default: "str",
Expand All @@ -117,11 +125,11 @@
case "lte":
case "gt":
case "gte":
types = ["num"];
types = ["num", "msg", "flow", "global"];
break;
case "includes":
case "does_not_include":
types = ["str"];
types = ["str", "msg", "flow", "global"];
break;
}
$("#node-input-value").typedInput("types", types);
Expand Down
3 changes: 2 additions & 1 deletion nodes/wait-until/wait-until.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ module.exports = function(RED) {
config.comparator,
config.value,
this.utils.selectn(config.property, event.new_state),
config.valueType
config.valueType,
this.savedMessage
);

if (!result) {
Expand Down

0 comments on commit e1ce911

Please sign in to comment.