Skip to content

Commit

Permalink
Added ability to filter event type within the 'events: all' node
Browse files Browse the repository at this point in the history
  • Loading branch information
zachowj committed Sep 20, 2018
1 parent dfe1347 commit 8a2d87c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
42 changes: 29 additions & 13 deletions nodes/events-all/events-all.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<script type="text/javascript">
RED.nodes.registerType('server-events', {
category: 'home_assistant',
color: '#038FC7',
color: '#038FC7',
defaults: {
name: { value: '' },
server: { value: '', type: 'server', required: true }
name: { value: '' },
server: { value: '', type: 'server', required: true },
event_type: { value: '', required: false },
},
inputs: 0,
outputs: 1,
icon: "arrow-right-bold.png",
paletteLabel: 'events: all',
label: function() { return this.name || `events_all: ${this.eventtypefilter}` },
label: function() { return this.name || `events: ${this.event_type || 'all'}` },
oneditprepare: function() {
const NODE = this;
const $server = $('#node-input-server');
Expand Down Expand Up @@ -42,17 +43,32 @@
<label for="node-input-server"><i class="fa fa-dot-circle-o"></i> Server</label>
<input type="text" id="node-input-server" />
</div>

<div class="form-row">
<label for="node-input-event_type"><i class="fa fa-tag"></i> Event Type</label>
<input type="text" id="node-input-event_type" placeholder="leave empty for all events" />
</div>
</script>

<script type="text/x-red" data-help-name="server-events">
<p>Outputs events received from the home assistant server.</p>

<h3>Outputs</h3>
<p>The outputted message object contains the following data, underscored values are straight from home assistant</p>
<table style="width: 100%;" border="1" cellpadding="10">
<tbody>
<tr> <td>topic</td> <td>event_type</td> </tr>
<tr> <td>payload</td> <td>original event</td> </tr>
<tr> <td>event_type</td> <td>event_type</td> </tr>
</tbody>
</table>
<dl class="message-properties">
<dt>topic <span class="property-type">string</span></dt>
<dd>event_type</dd>
<dt>event_type <span class="property-type">string</span></dt>
<dd>event_type</dd>
<dt>payload <span class="property-type">object</span></dt>
<dd>original event object</dd>
</dl>

<h3>Configuration</h3>

<dl class="message-properties">
<dt>Name <span class="property-type">string</span></dt>
<dd>the name of the node</dd>
<dt>Event Type <span class="property-type">string</span></dt>
<dd>filter by event type or leave blank for all events</dd>
</dl>

</script>
15 changes: 12 additions & 3 deletions nodes/events-all/events-all.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
const EventsNode = require('../../lib/events-node');

module.exports = function(RED) {
const nodeOptions = {
config: {
event_type: {}
}
};

class ServerEventsNode extends EventsNode {
constructor(nodeDefinition) {
super(nodeDefinition, RED);
this.addEventClientListener({ event: 'ha_events:all', handler: this.onHaEventsAll.bind(this) });
super(nodeDefinition, RED, nodeOptions);
this.addEventClientListener({
event: 'ha_events:' + (this.nodeConfig.event_type || 'all'),
handler: this.onHaEventsAll.bind(this) }
);
}

onHaEventsAll(evt) {
this.send({ event_type: evt.event_type, topic: evt.event_type, payload: evt });
this.status({fill: 'green', shape: 'dot', text: `${evt.event_type}`});
this.status({ fill: 'green', shape: 'dot', text: `${evt.event_type} at: ${this.getPrettyDate()}` });
}
}

Expand Down

0 comments on commit 8a2d87c

Please sign in to comment.