Skip to content

Commit

Permalink
fix: Handle resubscribing after a disconnect
Browse files Browse the repository at this point in the history
Don't have the HA WS lib auto resubscribe on a reconnect
so that we can wait until the NR integration is loaded before sending
entity discovery information.

Fixes: #250
  • Loading branch information
zachowj committed Aug 1, 2020
1 parent 1c187a5 commit 93d396c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
24 changes: 9 additions & 15 deletions lib/events-ha-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ const Joi = require('@hapi/joi');
const merge = require('lodash.merge');

const EventsNode = require('./events-node');
const {
INTEGRATION_LOADED,
INTEGRATION_UNLOADED,
INTEGRATION_NOT_LOADED,
} = require('./const');
const { INTEGRATION_UNLOADED, INTEGRATION_NOT_LOADED } = require('./const');

const DEFAULT_NODE_OPTIONS = {
debug: false,
Expand Down Expand Up @@ -67,17 +63,14 @@ class EventsHaNode extends EventsNode {
this.removeSubscription();
}

onHaEventsOpen() {
this.subscription = null;
}

async onHaIntegration(type) {
super.onHaIntegration(type);

switch (type) {
case INTEGRATION_LOADED:
if (this.registered) {
// Update the state of exposed nodes because when the HA client
// resubscribes it registers the entity with the original state
setTimeout(() => this.updateHomeAssistant(), 300);
}
break;
case INTEGRATION_UNLOADED:
case INTEGRATION_NOT_LOADED:
this.isEnabled = true;
Expand All @@ -103,7 +96,7 @@ class EventsHaNode extends EventsNode {
}

async registerEntity() {
if (this.subscription || super.registerEntity() === false) {
if (super.registerEntity() === false) {
return;
}

Expand All @@ -126,7 +119,8 @@ class EventsHaNode extends EventsNode {
this.debug(`Registering with HA`);
this.subscription = await this.websocketClient.client.subscribeMessage(
this.onHaEventMessage.bind(this),
payload
payload,
{ resubscribe: false }
);

this.setStatusSuccess('Registered');
Expand Down Expand Up @@ -275,8 +269,8 @@ class EventsHaNode extends EventsNode {
if (this.subscription) {
this.debug('Unregistering from HA');
this.subscription().catch(() => {});
this.subscription = null;
}
this.subscription = null;
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/events-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class EventsNode extends BaseNode {
}

onHaEventsClose() {
this.registered = false;
this.updateConnectionStatus();
}

Expand Down
3 changes: 2 additions & 1 deletion nodes/webhook/webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ module.exports = function (RED) {
webhook_id: this.nodeConfig.webhookId,
name: this.id,
server_id: this.nodeConfig.server.id,
}
},
{ resubscribe: false }
);
}
this.setStatusSuccess('Registered');
Expand Down

0 comments on commit 93d396c

Please sign in to comment.