Skip to content

Commit

Permalink
feat(current-state): able to override config entity id from payload
Browse files Browse the repository at this point in the history
This might be a small breaking change if your current-state node if
receiving a payload with entity_id set

Closes #115
  • Loading branch information
zachowj committed May 1, 2019
1 parent b8adf62 commit 9217e09
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 51 deletions.
4 changes: 2 additions & 2 deletions nodes/current-state/current-state.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
color: "#52C0F2",
inputs: 1,
outputs: 1,
outputLabels: ["", "halt if"],
outputLabels: ["", "if state"],
icon: "code.png",
paletteLabel: "current state",
label: function() {
Expand Down Expand Up @@ -217,7 +217,7 @@ <h3>Configuration</h3>
<dd>Match for entity_id field</dd>

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

<dt>State Type<span class="property-type">string</span></dt>
<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>
Expand Down
89 changes: 40 additions & 49 deletions nodes/current-state/current-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ module.exports = function(RED) {
input: {
entity_id: {
messageProp: 'payload.entity_id',
configProp: 'entity_id', // Will be used if value not found on message,
configProp: 'entity_id',
validation: {
haltOnFail: true,
schema: Joi.string() // Validates on message if exists, Joi will also attempt coercion
schema: Joi.string().label('entity_id')
}
}
}
Expand All @@ -38,64 +38,45 @@ module.exports = function(RED) {
/* eslint-disable camelcase */
async onInput({ parsedMessage, message }) {
const config = this.nodeConfig;
const entityId = config.entity_id
? config.entity_id
: parsedMessage.entity_id.value;
const logAndContinueEmpty = logMsg => {
this.node.warn(logMsg);
return { payload: {} };
};
const entityId = parsedMessage.entity_id.value;

if (config.server === null) {
this.node.error('No valid server selected.');
return null;
return;
}

if (!entityId)
return logAndContinueEmpty(
'entity ID not set, cannot get current state, sending empty payload'
);

const currentState = this.utils.merge(
const entity = this.utils.merge(
{},
await config.server.homeAssistant.getStates(entityId)
);
if (!currentState.entity_id)
return logAndContinueEmpty(
`entity could not be found in cache for entity_id: ${entityId}, sending empty payload`

if (!entity.entity_id) {
this.node.error(
`entity could not be found in cache for entity_id: ${entityId}`
);
return;
}

currentState.timeSinceChangedMs =
Date.now() - new Date(currentState.last_changed).getTime();
entity.timeSinceChangedMs =
Date.now() - new Date(entity.last_changed).getTime();

// Convert and save original state if needed
if (config.state_type && config.state_type !== 'str') {
currentState.original_state = currentState.state;
currentState.state = this.getCastValue(
entity.original_state = entity.state;
entity.state = this.getCastValue(
config.state_type,
currentState.state
entity.state
);
}

config.halt_if_compare = config.halt_if_compare || 'is';
config.halt_if_type = config.halt_if_type || 'str';

const isHaltValid = await this.getComparatorResult(
config.halt_if_compare,
config.halt_if,
currentState.state,
config.halt_if_type,
{
message,
entity: currentState
}
);
const shouldHaltIfState = config.halt_if && isHaltValid;

// default switch to true if undefined (backward compatibility
const override_topic = config.override_topic !== false;
if (override_topic) message.topic = entityId;
// default switch to true if undefined (backward compatibility)
message.topic =
config.override_topic !== false ? entityId : message.topic;

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

// Set 'State Location'
this.setContextValue(
currentState.state,
entity.state,
config.override_payload,
config.state_location,
message
);

// Set 'Entity Location'
this.setContextValue(
currentState,
entity,
config.override_data,
config.entity_location,
message
);

if (shouldHaltIfState) {
const debugMsg = `Get current state: halting processing due to current state of ${entityId} matches "halt if state" option`;
this.debug(debugMsg);
this.debugToClient(debugMsg);
this.setStatusFailed(currentState.state);
this.node.send([null, message]);
const isHaltValid = await this.getComparatorResult(
config.halt_if_compare,
config.halt_if,
entity.state,
config.halt_if_type,
{
message,
entity
}
);

if (config.halt_if && isHaltValid) {
this.setStatusFailed(entity.state);
this.send([null, message]);
} else {
this.setStatusSuccess(currentState.state);
this.node.send([message, null]);
this.setStatusSuccess(entity.state);
this.send([message, null]);
}
}
}
Expand Down

0 comments on commit 9217e09

Please sign in to comment.