diff --git a/lib/extension/homeassistant.js b/lib/extension/homeassistant.js index 709291fc..5a1daa00 100644 --- a/lib/extension/homeassistant.js +++ b/lib/extension/homeassistant.js @@ -2438,12 +2438,14 @@ class HomeAssistant extends Extension { onMQTTMessage(topic, message) { const discoveryMatch = topic.match(discoveryRegex); + const isDeviceAutomation = discoveryMatch && discoveryMatch[1] === 'device_automation'; if (discoveryMatch) { // Clear outdated discovery configs. try { message = JSON.parse(message); - if (!message || !message.availability_topic || - !message.availability_topic.startsWith(settings.get().mqtt.base_topic + '/')) { + const property = isDeviceAutomation ? 'topic' : 'availability_topic'; + if (!message || !message[property] || + !message[property].startsWith(settings.get().mqtt.base_topic + '/')) { // Base topic is different, probably different Zigbee2MQTT instance. return; } @@ -2455,7 +2457,7 @@ class HomeAssistant extends Extension { const resolvedEntity = this.zigbee.resolveEntity(ieeeAddr); let clear = !resolvedEntity || !resolvedEntity.definition; - if (!clear) { + if (!clear && !isDeviceAutomation) { const type = discoveryMatch[1]; const objectID = discoveryMatch[3]; clear = !this.getConfigs(resolvedEntity).find((c) => c.type === type && c.object_id === objectID); diff --git a/test/homeassistant.test.js b/test/homeassistant.test.js index 1b370275..59c2d871 100644 --- a/test/homeassistant.test.js +++ b/test/homeassistant.test.js @@ -1231,5 +1231,18 @@ describe('HomeAssistant extension', () => { await MQTT.events.message('homeassistant/sensor/0x123/temperature/config', '1}3'); await flushPromises(); expect(MQTT.publish).toHaveBeenCalledTimes(0); + + // Existing device, device automation -> don't clear + MQTT.publish.mockClear(); + await MQTT.events.message('homeassistant/device_automation/0x000b57fffec6a5b2/action_button_3_single/config', stringify({topic: 'zigbee2mqtt/0x000b57fffec6a5b2/availability'})); + await flushPromises(); + expect(MQTT.publish).toHaveBeenCalledTimes(0); + + // Not-existing device, device automation -> don't clear + MQTT.publish.mockClear(); + await MQTT.events.message('homeassistant/device_automation/0x000b57fffec6a5b2_not_existing/action_button_3_single/config', stringify({topic: 'zigbee2mqtt/0x000b57fffec6a5b2_not_existing/availability'})); + await flushPromises(); + expect(MQTT.publish).toHaveBeenCalledTimes(1); + expect(MQTT.publish).toHaveBeenCalledWith('homeassistant/device_automation/0x000b57fffec6a5b2_not_existing/action_button_3_single/config', null, {qos: 0, retain: true}, expect.any(Function)); }); });