Skip to content
This repository has been archived by the owner on Nov 20, 2019. It is now read-only.

Commit

Permalink
Merge pull request #33 from Spartan-II-117/dev-master
Browse files Browse the repository at this point in the history
Breaking Change: Poll state now acts like current-state
minor fixes
  • Loading branch information
Spartan-II-117 authored Aug 26, 2018
2 parents 19ef45e + 50c0f42 commit 086c8c6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.3.6
* Feat: Added <= and => comparison for Constraints and Outputs. Renamed greater_than and less_than to > and < but kept backwards compatibility
* Fix: Fixed label for outputs that was getting undefined tacked on to the front of the string.
* Fix: poll-state node now matches events-state output
* Fix: entity_id is no longer required on current-state

# v0.3.2
* Feat: History node now supports end date and entityid filtering
* Fix: Nodered configuration value userDir is fetched correctly now
Expand Down
2 changes: 1 addition & 1 deletion nodes/api_current-state/api_current-state.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
halt_if: { value: '' },
override_topic: { value: true },
override_payload: { value: true },
entity_id: { value: '', required: true },
entity_id: { value: '' },
},
oneditprepare: function () {
const $entityIdField = $('#entity_id');
Expand Down
17 changes: 8 additions & 9 deletions nodes/poll-state/poll-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,24 @@ module.exports = function(RED) {

async onTimer() {
try {
const state = await this.getState(this.nodeConfig.entity_id);
if (!state) {
const pollState = await this.getState(this.nodeConfig.entity_id);
if (!pollState) {
this.warn(`could not find state with entity_id "${this.nodeConfig.entity_id}"`);
this.status({fill:"red",shape:"ring",text:`no state found for ${this.nodeConfig.entity_id}`});
return;
}

const dateChanged = this.calculateTimeSinceChanged(state);
const dateChanged = this.calculateTimeSinceChanged(pollState);
if (dateChanged) {
const timeSinceChanged = ta.ago(dateChanged);
const timeSinceChangedMs = Date.now() - dateChanged.getTime();
pollState.timeSinceChanged = ta.ago(dateChanged);
pollState.timeSinceChangedMs = Date.now() - dateChanged.getTime();
this.send({
topic: this.nodeConfig.entity_id,
payload: { timeSinceChanged, timeSinceChangedMs, dateChanged, data: state }
payload: pollState.state,
data: pollState
});
var prettyDate = new Date().toLocaleDateString("en-US",{month: 'short', day: 'numeric', hour12: false, hour: 'numeric', minute: 'numeric'});
this.status({fill:"green",shape:"dot",text:`${state} at: ${prettyDate}`});
this.status({fill:"green",shape:"dot",text:`${pollState.state} at: ${prettyDate}`});
} else {
this.warn(`could not calculate time since changed for entity_id "${this.nodeConfig.entity_id}"`);
}
Expand All @@ -81,8 +82,6 @@ module.exports = function(RED) {
state = await this.nodeConfig.server.homeAssistant.getStates(this.nodeConfig.entity_id, true);
}
return state;
var prettyDate = new Date().toLocaleDateString("en-US",{month: 'short', day: 'numeric', hour12: false, hour: 'numeric', minute: 'numeric'});
this.status({fill:"green",shape:"dot",text:`${state} at: ${prettyDate}`});
}
}
RED.nodes.registerType('poll-state', TimeSinceStateNode);
Expand Down
16 changes: 10 additions & 6 deletions nodes/trigger-state/trigger-state.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ <h3>Add Constraints</h3>
<option value="is">Is</option>
<option value="is_not">Is Not</option>

<option value="greater_than">Greater Than</option>
<option value="less_than">Less Than</option>
<option value=">">&gt;</option>
<option value=">=">&gt;=</option>
<option value="<">&lt;</option>
<option value="<=">&lt;=</option>

<option value="includes">Includes</option>
<option value="does_not_include">Does Not Include</option>
Expand Down Expand Up @@ -112,8 +114,10 @@ <h3>Add Outputs</h3>
<option value="is"> Is </option>
<option value="is_not"> Is Not </option>

<option value="greater_than"> Greater Than </option>
<option value="less_than"> Less Than </option>
<option value=">">&gt;</option>
<option value=">=">&gt;=</option>
<option value="<">&lt;</option>
<option value="<=">&lt;=</option>

<option value="includes">Includes</option>
<option value="does_not_include">Does Not Include</option>
Expand Down Expand Up @@ -166,9 +170,9 @@ <h3>Add Outputs</h3>
const co = this.customoutputs[index - NUM_DEFAULT_OUTPUTS];
let label;
if (co.comparatorPropertyType === 'always') {
label += 'always sent'
label = 'always sent'
} else {
label += `sent when ${co.comparatorPropertyType.replace('_', ' ')} ${co.comparatorType.replace('_', '')} ${co.comparatorValue}`;
label = `${co.comparatorPropertyType.replace('_', ' ')} ${co.comparatorType.replace('_', '')} ${co.comparatorValue}`;
}
return label;
},
Expand Down
20 changes: 17 additions & 3 deletions nodes/trigger-state/trigger-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,18 @@ module.exports = function(RED) {

try {
const constraintComparatorResults = await this.getConstraintComparatorResults(this.nodeConfig.constraints, eventMessage);
let outputs = this.getDefaultMessageOutputs(constraintComparatorResults, eventMessage);
const state = eventMessage.event.new_state.state;
const prettyDate = new Date().toLocaleDateString("en-US",{month: 'short', day: 'numeric', hour12: false, hour: 'numeric', minute: 'numeric'});
let outputs = this.getDefaultMessageOutputs(constraintComparatorResults, eventMessage);
let status = { fill: "green", shape: "dot", text: `${state} at: ${prettyDate}` };

// If a constraint comparator failed we're done, also if no custom outputs to look at
if (constraintComparatorResults.failed.length || !this.nodeConfig.customoutputs.length) {
if (constraintComparatorResults.failed.length) {
status = { fill: "red", shape: "ring", text: `${state} at: ${prettyDate}` };
}
this.debugToClient('done processing sending messages: ', outputs);
this.status(status);
return this.send(outputs);
}

Expand All @@ -72,6 +79,7 @@ module.exports = function(RED) {

outputs = outputs.concat(customOutputMessages);
this.debugToClient('done processing sending messages: ', outputs);
this.status(status);
this.send(outputs);
} catch (e) {
this.error(e);
Expand Down Expand Up @@ -181,10 +189,16 @@ module.exports = function(RED) {
case 'does_not_include':
const isIncluded = cValue.includes(actualValue);
return (comparatorType === 'includes') ? isIncluded : !isIncluded;
case 'greater_than':
case 'greater_than': // here for backwards compatibility
case '>':
return actualValue > cValue;
case 'less_than':
case '>=':
return actualValue >= cValue;
case 'less_than': // here for backwards compatibility
case '<':
return actualValue < cValue;
case '<=':
return actualValue <= cValue;
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "node-red-contrib-home-assistant",
"description": "node-red nodes to visually construct home automation with home assistant",
"version": "0.3.5",
"version": "0.3.6",
"homepage": "https://github.com/Spartan-II-117/node-red-contrib-home-assistant",

"bugs": {
Expand Down

0 comments on commit 086c8c6

Please sign in to comment.