Skip to content

Commit

Permalink
Merge pull request #68 from AYapejian/dev-master
Browse files Browse the repository at this point in the history
v0.3.2
  • Loading branch information
AYapejian authored May 5, 2018
2 parents d62a097 + 045cf84 commit c06d98b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 26 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# V0.3.1
# v0.3.2
* Feat: History node now supports end date and entityid filtering
* Fix: Nodered configuration value userDir is fetched correctly now

# v0.3.1
* Fix/Feat: Current State Node: Preserve original message with options to override topic and payload (Thanks @declankenny)
* Fix: Added try/catch in onHaEventsStateChanged (Thanks @oskargronqvist)
* Docs: Added quotes to 'Call Service' placeholder text (Thanks @brubaked)
Expand Down
11 changes: 2 additions & 9 deletions docker/home-assistant/root-fs/config/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,18 @@ logger:
default: !env_var HA_LOGGER_DEFAULT

recorder:
purge_interval: 2
purge_keep_days: 1

logbook:
frontend:
config:
history:
map:
sun:

weblink:
entities:
- name: Node Red
url: !env_var NODE_RED_URL

panel_iframe:
node_red:
title: Node Red
url: !env_var NODE_RED_URL
icon: mdi:grid-on
icon: mdi:sitemap


alarm_control_panel:
Expand Down
2 changes: 1 addition & 1 deletion lib/base-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class BaseNode {
try {
if (DB) return DB;

let dbLocation = this.RED.settings.get('userDir');
let dbLocation = this.RED.settings.userDir;
if (!dbLocation) throw new Error('Could not find userDir to use for database store');
dbLocation += '/node-red-contrib-home-assistant.json';

Expand Down
61 changes: 55 additions & 6 deletions nodes/api_get-history/api_get-history.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
category: 'home_assistant',
color: '#52C0F2',
defaults: {
name: { value: '' },
name: { value: '' },
server: { value: '', type: 'server', required: true },
startdate: { value: '' }
startdate: { value: '' },
enddate: { value: '' },
entityid: { value: '' },
entityidtype: { value: '' }
},
inputs: 1,
outputs: 1,
Expand All @@ -22,6 +25,9 @@
oneditprepare: function() {
const NODE = this;
const $server = $('#node-input-server');
NODE.entityidtype = NODE.entityidtype || 'is';
$('#node-input-entityidtype').val(NODE.entityidtype);

const utils = {
setDefaultServerSelection: function () {
let defaultServer;
Expand All @@ -40,7 +46,7 @@
</script>


<script type="text/x-red" data-template-name="api-get-history">
<script type="text/html" data-template-name="api-get-history">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
Expand All @@ -57,16 +63,33 @@
<input type="text" id="node-input-startdate" autofocus="autofocus" />
</div>

<div class="form-row">
<label for="node-input-enddate"><i class="fa fa-date"></i> End Date</label>
<input type="text" id="node-input-enddate" />
</div>

<!-- Comparator Selection -->
<div class="form-row">
<label for="node-input-entityid"><i class="fa fa-date"></i> Entity ID</label>

<select type="text" id="node-input-entityidtype" style="width: 80px;">
<option value="is">Is</option>
<option value="includes">Includes</option>
</select>

<input type="text" id="node-input-entityid" style="width: 52%;"/>
</div>

<div class="form-tips">
<ul>
<li>
<code>startdate</code> must be in ISO format, example: "2018-01-27T13:00:00+00:00"
dates must be in ISO format, example: "2018-01-27T13:00:00+00:00"
</li>
</ul>
</div>
</script>

<script type="text/x-red" data-help-name="api-get-history">
<script type="text/html" data-help-name="api-get-history">
<p>Fetches history from Home Assistant (all history for the past day by default)</p>

<h3>Inputs</h3>
Expand All @@ -75,6 +98,16 @@ <h3>Inputs</h3>
startdate <span class="property-type">string | date</span>
</dt>
<dd>Date to start fetching history from. Will override the nodes configuration if passed in</dd>

<dt class="optional">
enddate <span class="property-type">string | date</span>
</dt>
<dd>Date to fetch history to. Will override the nodes configuration if passed in</dd>

<dt class="optional">
entityid <span class="property-type">string</span>
</dt>
<dd>Exact entity_id to fetch history for, must be an exact match as is passed directly to the home-assistant api</dd>
</dl>

<h3>Outputs</h3>
Expand All @@ -83,10 +116,26 @@ <h3>Outputs</h3>
payload <span class="property-type">array</span>
</dt>
<dd>The history returned by home-assistant, which is an array of arrays where each array entry contains history objects for one particular entity</dd>

<dt>
startdate <span class="property-type">string</span>
</dt>
<dd>ISO date string used to fetch history</dd>

<dt>
enddate <span class="property-type">string</span>
</dt>
<dd>ISO date string used to fetch history</dd>

<dt>
entityid <span class="property-type">string</span>
</dt>
<dd>entityid string used during fetch history call</dd>

<dt>
entityidtype <span class="property-type">string</span>
</dt>
<dd>'is' or 'includes' depending on match type. NOTE: 'includes' fetches all history for time period then filters according to value, this will be less performant than exact ('is') matching</dd>
</dl>

<h3>Details</h3>
Expand All @@ -95,6 +144,6 @@ <h3>Details</h3>
<h3>References</h3>
<ul>
<li><a href="https://home-assistant.io/components/history">HA History Component</a></li>
<li><a href="https://home-assistant.io/developers/rest_api">HA API: /api/history/period</a></li>
<li><a href="https://developers.home-assistant.io/docs/en/external_api_rest.html#get-api-history-period-lt-timestamp">HA API: /api/history/period</a></li>
</ul>
</script>
40 changes: 32 additions & 8 deletions nodes/api_get-history/api_get-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,36 @@ module.exports = function(RED) {
const nodeOptions = {
debug: true,
config: {
startdate: {},
name: {},
server: { isNode: true }
name: {},
server: { isNode: true },
startdate: {},
enddate: {},
entityid: {},
entityidtype: {}
},
input: {
startdate: {
messageProp: 'startdate',
configProp: 'startdate',
configProp: 'startdate',
default: () => {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
return yesterday.toISOString();
},
validation: { haltOnFail: true, schema: Joi.date().optional().allow('') }
},
enddate: {
messageProp: 'enddate',
configProp: 'enddate',
validation: { haltOnFail: true, schema: Joi.date().optional().allow('') }
},
entityid: {
messageProp: 'entityid',
configProp: 'entityid'
},
entityidtype: {
messageProp: 'entityidtype',
configProp: 'entityidtype'
}
}
};
Expand All @@ -29,13 +45,21 @@ module.exports = function(RED) {
}

onInput({ parsedMessage, message }) {
let { startdate } = parsedMessage;
startdate = startdate.value;
let { startdate, enddate, entityid, entityidtype } = parsedMessage;
startdate = startdate.value;
enddate = enddate.value;
entityid = entityid.value;

let apiRequest = (entityidtype.value === 'includes' && entityid)
? this.nodeConfig.server.api.getHistory(startdate, null, enddate, { include: new RegExp(entityid) })
: this.nodeConfig.server.api.getHistory(startdate, entityid, enddate);

return this.nodeConfig.server.api.getHistory(startdate)
return apiRequest
.then(res => {
message.startdate = startdate;
message.payload = res;
message.enddate = enddate || null;
message.entityid = entityid || null;
message.payload = res;
this.send(message);
})
.catch(err => {
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.1",
"version": "0.3.2",
"homepage": "https://github.com/AYapejian/node-red-contrib-home-assistant",
"bugs": {
"url": "https://github.com/AYapejian/node-red-contrib-home-assistant/issues"
Expand Down

0 comments on commit c06d98b

Please sign in to comment.