Skip to content

Commit

Permalink
fix(home-assistant): clear retained old style status messages
Browse files Browse the repository at this point in the history
Pre 2.18.x the status messages were retained. This isn't done anymore,
but after upgrading the old status messages are still retained on the
cluster. This just clears the old msgs to prevent any weird behavior in
the future, providing a more seamless upgrade path.
  • Loading branch information
mKeRix committed May 30, 2021
1 parent 6781619 commit d33d6e3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/integrations/home-assistant/home-assistant.service.spec.ts
Expand Up @@ -558,6 +558,19 @@ describe('HomeAssistantService', () => {
}
);

it('should clear old retained status messages from pre-2.18.x', async () => {
await service.onModuleInit();
service.handleNewEntity(new Sensor('test', 'Test'));

expect(mockMqttClient.publish).toHaveBeenCalledWith(
'room-assistant/sensor/test-instance-test/status',
'',
{
retain: true,
}
);
});

it('should send an instance status message as heartbeat', async () => {
await service.onModuleInit();
mockMqttClient.publish.mockClear();
Expand Down
15 changes: 14 additions & 1 deletion src/integrations/home-assistant/home-assistant.service.ts
Expand Up @@ -171,7 +171,11 @@ export class HomeAssistantService
this.entityConfigs.set(combinedId, config);

this.sendDiscoveryMessage(config);
this.sendEntityStatus(config);

if (config.availability.length > 0) {
this.clearRetained(config.availability[0].topic); // for upgrades from < 2.18.x
this.sendEntityStatus(config);
}
}

/**
Expand Down Expand Up @@ -434,6 +438,15 @@ export class HomeAssistantService
});
}

/**
* Clear any retained message from the given MQTT topic.
*
* @param topic - Topic to clear ("delete")
*/
private async clearRetained(topic: string): Promise<void> {
await this.mqttClient.publish(topic, '', { retain: true });
}

/**
* Publish entity status to its availability topic with MQTT.
*
Expand Down

0 comments on commit d33d6e3

Please sign in to comment.