Skip to content

Commit

Permalink
fix(home-assistant): no availability msgs for distributed child devices
Browse files Browse the repository at this point in the history
Distributed entities that had an override on their device config
incorrectly send offline messages whenever an instance was shutting
down. This extends the logic to also correctly cover these child devices
of the distributed hub.
  • Loading branch information
mKeRix committed Feb 10, 2020
1 parent 4b77868 commit 1b82c55
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/integrations/home-assistant/home-assistant.service.spec.ts
Expand Up @@ -18,6 +18,7 @@ import { SensorConfig } from './sensor-config';
import { Entity } from '../../entities/entity';
import { Sensor } from '../../entities/sensor';
import { BinarySensor } from '../../entities/binary-sensor';
import { DISTRIBUTED_DEVICE_ID } from './home-assistant.const';

jest.mock('async-mqtt', () => {
return {
Expand Down Expand Up @@ -107,10 +108,30 @@ describe('HomeAssistantService', () => {

it('should not send offline messages for distributed entities', async () => {
await service.onModuleInit();
service.handleNewEntity(new Sensor('distributed', 'Dist', true));
service.handleNewEntity(new Sensor('test-sensor', 'Dist', true));
mockMqttClient.publish.mockClear();

await service.onApplicationShutdown();

expect(mockMqttClient.publish).not.toHaveBeenCalled();
});

it('should not send offline messages for entities belong to a child device of the distributed hub', async () => {
await service.onModuleInit();
service.handleNewEntity(new Sensor('test-sensor', 'Dist', true), [
{
for: SensorConfig,
overrides: {
device: {
identifiers: 'test-device-id',
viaDevice: DISTRIBUTED_DEVICE_ID
}
}
}
]);
mockMqttClient.publish.mockClear();

await service.onApplicationShutdown();

expect(mockMqttClient.publish).not.toHaveBeenCalled();
});
Expand Down
5 changes: 4 additions & 1 deletion src/integrations/home-assistant/home-assistant.service.ts
Expand Up @@ -76,7 +76,10 @@ export class HomeAssistantService
*/
async onApplicationShutdown(): Promise<void> {
this.entityConfigs.forEach(config => {
if (config.device?.identifiers !== DISTRIBUTED_DEVICE_ID) {
if (
config.device?.identifiers !== DISTRIBUTED_DEVICE_ID &&
config.device?.viaDevice !== DISTRIBUTED_DEVICE_ID
) {
this.mqttClient.publish(
config.availabilityTopic,
config.payloadNotAvailable,
Expand Down

0 comments on commit 1b82c55

Please sign in to comment.