mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-08-29 07:08:56 +00:00
fix: Prevent duplicate Home Assistant scene entities (#20097)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user