mirror of
https://github.com/Koenkk/zigbee2mqtt.git
synced 2026-07-02 01:51:38 +00:00
Clear outdated Home Assistant device_automation discoveries. #4452
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user