Skip to content

Commit 9217e09

Browse files
committed
feat(current-state): able to override config entity id from payload
This might be a small breaking change if your current-state node if receiving a payload with entity_id set Closes #115
1 parent b8adf62 commit 9217e09

File tree

2 files changed

+42
-51
lines changed

2 files changed

+42
-51
lines changed

nodes/current-state/current-state.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
color: "#52C0F2",
55
inputs: 1,
66
outputs: 1,
7-
outputLabels: ["", "halt if"],
7+
outputLabels: ["", "if state"],
88
icon: "code.png",
99
paletteLabel: "current state",
1010
label: function() {
@@ -217,7 +217,7 @@ <h3>Configuration</h3>
217217
<dd>Match for entity_id field</dd>
218218

219219
<dt>If state <span class="property-type">string</span></dt>
220-
<dd>If the logic statement is true send message to second output</dd>
220+
<dd>If the logic statement is true send message to only the second output</dd>
221221

222222
<dt>State Type<span class="property-type">string</span></dt>
223223
<dd>Convert the state of the entity to the selected type. Boolean will be convert to true based on if the string is equal by default to (y|yes|true|on|home|open). Original value stored in msg.data.original_state</dd>

nodes/current-state/current-state.js

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ module.exports = function(RED) {
2121
input: {
2222
entity_id: {
2323
messageProp: 'payload.entity_id',
24-
configProp: 'entity_id', // Will be used if value not found on message,
24+
configProp: 'entity_id',
2525
validation: {
2626
haltOnFail: true,
27-
schema: Joi.string() // Validates on message if exists, Joi will also attempt coercion
27+
schema: Joi.string().label('entity_id')
2828
}
2929
}
3030
}
@@ -38,64 +38,45 @@ module.exports = function(RED) {
3838
/* eslint-disable camelcase */
3939
async onInput({ parsedMessage, message }) {
4040
const config = this.nodeConfig;
41-
const entityId = config.entity_id
42-
? config.entity_id
43-
: parsedMessage.entity_id.value;
44-
const logAndContinueEmpty = logMsg => {
45-
this.node.warn(logMsg);
46-
return { payload: {} };
47-
};
41+
const entityId = parsedMessage.entity_id.value;
4842

4943
if (config.server === null) {
5044
this.node.error('No valid server selected.');
51-
return null;
45+
return;
5246
}
5347

54-
if (!entityId)
55-
return logAndContinueEmpty(
56-
'entity ID not set, cannot get current state, sending empty payload'
57-
);
58-
59-
const currentState = this.utils.merge(
48+
const entity = this.utils.merge(
6049
{},
6150
await config.server.homeAssistant.getStates(entityId)
6251
);
63-
if (!currentState.entity_id)
64-
return logAndContinueEmpty(
65-
`entity could not be found in cache for entity_id: ${entityId}, sending empty payload`
52+
53+
if (!entity.entity_id) {
54+
this.node.error(
55+
`entity could not be found in cache for entity_id: ${entityId}`
6656
);
57+
return;
58+
}
6759

68-
currentState.timeSinceChangedMs =
69-
Date.now() - new Date(currentState.last_changed).getTime();
60+
entity.timeSinceChangedMs =
61+
Date.now() - new Date(entity.last_changed).getTime();
7062

7163
// Convert and save original state if needed
7264
if (config.state_type && config.state_type !== 'str') {
73-
currentState.original_state = currentState.state;
74-
currentState.state = this.getCastValue(
65+
entity.original_state = entity.state;
66+
entity.state = this.getCastValue(
7567
config.state_type,
76-
currentState.state
68+
entity.state
7769
);
7870
}
7971

8072
config.halt_if_compare = config.halt_if_compare || 'is';
8173
config.halt_if_type = config.halt_if_type || 'str';
8274

83-
const isHaltValid = await this.getComparatorResult(
84-
config.halt_if_compare,
85-
config.halt_if,
86-
currentState.state,
87-
config.halt_if_type,
88-
{
89-
message,
90-
entity: currentState
91-
}
92-
);
93-
const shouldHaltIfState = config.halt_if && isHaltValid;
94-
95-
// default switch to true if undefined (backward compatibility
96-
const override_topic = config.override_topic !== false;
97-
if (override_topic) message.topic = entityId;
75+
// default switch to true if undefined (backward compatibility)
76+
message.topic =
77+
config.override_topic !== false ? entityId : message.topic;
9878

79+
// Set Defaults
9980
if (config.state_location === undefined) {
10081
config.state_location = 'payload';
10182
config.override_payload =
@@ -107,29 +88,39 @@ module.exports = function(RED) {
10788
config.override_data !== false ? 'msg' : 'none';
10889
}
10990

91+
// Set 'State Location'
11092
this.setContextValue(
111-
currentState.state,
93+
entity.state,
11294
config.override_payload,
11395
config.state_location,
11496
message
11597
);
11698

99+
// Set 'Entity Location'
117100
this.setContextValue(
118-
currentState,
101+
entity,
119102
config.override_data,
120103
config.entity_location,
121104
message
122105
);
123106

124-
if (shouldHaltIfState) {
125-
const debugMsg = `Get current state: halting processing due to current state of ${entityId} matches "halt if state" option`;
126-
this.debug(debugMsg);
127-
this.debugToClient(debugMsg);
128-
this.setStatusFailed(currentState.state);
129-
this.node.send([null, message]);
107+
const isHaltValid = await this.getComparatorResult(
108+
config.halt_if_compare,
109+
config.halt_if,
110+
entity.state,
111+
config.halt_if_type,
112+
{
113+
message,
114+
entity
115+
}
116+
);
117+
118+
if (config.halt_if && isHaltValid) {
119+
this.setStatusFailed(entity.state);
120+
this.send([null, message]);
130121
} else {
131-
this.setStatusSuccess(currentState.state);
132-
this.node.send([message, null]);
122+
this.setStatusSuccess(entity.state);
123+
this.send([message, null]);
133124
}
134125
}
135126
}

0 commit comments

Comments
 (0)