Skip to content

Commit

Permalink
fix(home-assistant): Do not make distributed sensors unavailable on s…
Browse files Browse the repository at this point in the history
…hutdown

Other instances may still update it, so we don't know if it's
unavailable.
  • Loading branch information
mKeRix committed Jan 25, 2020
1 parent 4b948bd commit 29ffddc
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/integrations/home-assistant/home-assistant.service.ts
Expand Up @@ -55,6 +55,9 @@ export class HomeAssistantService
this.config.mqttUrl,
this.config.mqttOptions
);
this.mqttClient.on('connect', () =>
this.logger.log(`Connected to ${this.config.mqttUrl}`)
);

const systemInfo = await system();
this.device = new Device(systemInfo.uuid);
Expand All @@ -65,10 +68,12 @@ export class HomeAssistantService

async onApplicationShutdown(signal?: string): Promise<void> {
this.entityConfigs.forEach(config => {
this.mqttClient.publish(
config.availabilityTopic,
config.payloadNotAvailable
);
if (config.device.identifiers !== 'room-assistant-distributed') {
this.mqttClient.publish(
config.availabilityTopic,
config.payloadNotAvailable
);
}
});
return this.mqttClient.end();
}
Expand All @@ -88,12 +93,7 @@ export class HomeAssistantService
return;
}

const customization = customizations.find(
value => value.for.prototype instanceof EntityConfig
);
if (customization !== undefined) {
Object.assign(config, customization.overrides);
}
config = this.applyCustomizations(config, customizations);

if (entity.distributed) {
config.device = new Device('room-assistant-distributed');
Expand Down Expand Up @@ -164,7 +164,21 @@ export class HomeAssistantService
}_${entityId}`;
}

private formatMessage(message: object): object {
protected applyCustomizations(
config: EntityConfig,
customizations: Array<EntityCustomization<any>>
): EntityConfig {
const customization = customizations.find(
value => value.for.prototype instanceof EntityConfig
);
if (customization !== undefined) {
Object.assign(config, customization.overrides);
}

return config;
}

protected formatMessage(message: object): object {
const filteredMessage = _.omit(message, PROPERTY_BLACKLIST);
return this.deepMap(filteredMessage, obj => {
return _.mapKeys(obj, (v, k) => {
Expand Down

0 comments on commit 29ffddc

Please sign in to comment.