Skip to content

Commit

Permalink
fix: Prevent duplicate Home Assistant scene entities (#20097)
Browse files Browse the repository at this point in the history
  • Loading branch information
mundschenk-at committed Dec 8, 2023
1 parent 59efc15 commit a17f650
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/extension/homeassistant.ts
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions test/homeassistant.test.js
Expand Up @@ -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',
Expand Down

0 comments on commit a17f650

Please sign in to comment.