Skip to content

Commit

Permalink
fix: Clear old Home Assistant discovery topics when exposes are chang…
Browse files Browse the repository at this point in the history
…ed (#20313)
  • Loading branch information
slugzero committed Dec 20, 2023
1 parent b67888f commit a5b959a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/extension/homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,7 @@ export default class HomeAssistant extends Extension {
(entity.options.hasOwnProperty('homeassistant') && !entity.options.homeassistant))) {
return;
}
const lastDiscovered = this.discovered[discoverKey];

this.discovered[discoverKey] = {topics: new Set(), mockProperties: new Set(), objectIDs: new Set()};
this.getConfigs(entity).forEach((config) => {
Expand Down Expand Up @@ -1543,6 +1544,11 @@ export default class HomeAssistant extends Extension {
config.mockProperties?.forEach((mockProperty) =>
this.discovered[discoverKey].mockProperties.add(mockProperty));
});
lastDiscovered?.topics?.forEach((topic) => {
if (!this.discovered[discoverKey].topics.has(topic)) {
this.mqtt.publish(topic, null, {retain: true, qos: 1}, this.discoveryTopic, false, false);
}
});
}

@bind private onMQTTMessage(data: eventdata.MQTTMessage): void {
Expand Down
15 changes: 14 additions & 1 deletion test/homeassistant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2482,4 +2482,17 @@ describe('HomeAssistant extension', () => {
expect.any(Function),
);
});
});

it('Should remove discovery entries for removed exposes when device options change', async () => {
MQTT.publish.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/device/options', stringify({"id": "0xf4ce368a38be56a1", "options": {"dimmer_1_enabled": "false", "dimmer_1_dimming_enabled": "false"}}));
await flushPromises();

expect(MQTT.publish).toHaveBeenCalledWith(
'homeassistant/light/0xf4ce368a38be56a1/light_l2/config',
null,
{ retain: true, qos: 1 },
expect.any(Function),
);
});
});

0 comments on commit a5b959a

Please sign in to comment.