fix: Prevent duplicate Home Assistant scene entities (#20097)

This commit is contained in:
Der Mundschenk & Compagnie
2023-12-08 19:34:37 +01:00
committed by GitHub
parent 59efc15f3a
commit a17f650ccb
2 changed files with 18 additions and 4 deletions
+15 -4
View File
@@ -1612,19 +1612,30 @@ export default class HomeAssistant extends Extension {
this.discover(data.device);
}
@bind onScenesChanged(): void {
@bind async onScenesChanged(): Promise<void> {
// Re-trigger MQTT discovery of all devices and groups, similar to bridge.ts
for (const entity of [...this.zigbee.devices(), ...this.zigbee.groups()]) {
// First, clear existing scene discovery topics
const entities = [...this.zigbee.devices(), ...this.zigbee.groups()];
// First, clear existing scene discovery topics
entities.forEach((entity) => {
logger.debug(`Clearing Home Assistant scene discovery topics for '${entity.name}'`);
this.discovered[this.getDiscoverKey(entity)]?.topics.forEach((topic) => {
if (topic.startsWith('scene')) {
this.mqtt.publish(topic, null, {retain: true, qos: 1}, this.discoveryTopic, false, false);
}
});
});
// Make sure Home Assistant deletes the old entity first otherwise another one (_2) is created
// https://github.com/Koenkk/zigbee2mqtt/issues/12610
logger.debug(`Finished clearing scene discovery topics, waiting for Home Assistant.`);
await utils.sleep(2);
// Re-discover all entities (including their new scenes).
logger.debug(`Re-discovering entities with their scenes.`);
entities.forEach((entity) => {
this.discover(entity, true);
}
});
}
private getDevicePayload(entity: Device | Group | Bridge): KeyValue {
+3
View File
@@ -2171,6 +2171,9 @@ describe('HomeAssistant extension', () => {
expect.any(Function),
);
jest.runOnlyPendingTimers();
await flushPromises();
const payload = {
'name': 'Chill scene',
'command_topic': 'zigbee2mqtt/bulb_color_2/set',