Skip to content

Commit

Permalink
fix: check for WS client before listening for events
Browse files Browse the repository at this point in the history
Closes #158
  • Loading branch information
zachowj committed Oct 19, 2019
1 parent 2a84866 commit 8d72dbc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
45 changes: 23 additions & 22 deletions lib/base-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,28 +186,29 @@ class BaseNode {
}

setConnectionStatus(state, additionalText) {
let connectionStatus;
switch (state) {
case this.websocketClient.CONNECTING:
connectionStatus = {
shape: 'ring',
fill: 'yellow',
text: 'connecting'
};
break;
case this.websocketClient.CONNECTED:
connectionStatus = {
shape: 'dot',
fill: 'green',
text: 'connected'
};
break;
default:
connectionStatus = {
shape: 'ring',
fill: 'red',
text: 'disconnected'
};
let connectionStatus = {
shape: 'ring',
fill: 'red',
text: 'disconnected'
};

if (this.websocketClient) {
switch (state) {
case this.websocketClient.CONNECTING:
connectionStatus = {
shape: 'ring',
fill: 'yellow',
text: 'connecting'
};
break;
case this.websocketClient.CONNECTED:
connectionStatus = {
shape: 'dot',
fill: 'green',
text: 'connected'
};
break;
}
}
if (
Object.prototype.hasOwnProperty.call(this, 'isenabled') &&
Expand Down
6 changes: 4 additions & 2 deletions lib/events-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ class EventsNode extends BaseNode {
}

addEventClientListener({ event, handler }) {
this.listeners[event] = handler;
this.websocketClient.addListener(event, handler);
if (this.websocketClient) {
this.listeners[event] = handler;
this.websocketClient.addListener(event, handler);
}
}

removeEventClientListeners() {
Expand Down
9 changes: 6 additions & 3 deletions nodes/events-all/events-all.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = function(RED) {
class ServerEventsNode extends EventsNode {
constructor(nodeDefinition) {
super(nodeDefinition, RED, nodeOptions);

this.addEventClientListener({
event: 'ha_events:' + (this.nodeConfig.event_type || 'all'),
handler: this.onHaEventsAll.bind(this)
Expand All @@ -29,9 +30,11 @@ module.exports = function(RED) {
}

// Registering only needed event types
this.nodeConfig.server.homeAssistant.eventsList[this.id] =
this.nodeConfig.event_type || '__ALL__';
this.updateEventList();
if (this.utils.selectn('nodeConfig.server.homeAssistant', this)) {
this.nodeConfig.server.homeAssistant.eventsList[this.id] =
this.nodeConfig.event_type || '__ALL__';
this.updateEventList();
}
}

onHaEventsAll(evt) {
Expand Down

0 comments on commit 8d72dbc

Please sign in to comment.