From 69c519e06d926837361dc7a952659b01cda3ab58 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Fri, 18 Oct 2024 07:24:50 -0600 Subject: [PATCH] [mqtt.homeassistant] avoid improperly delivered triggers (#17584) if multiple DeviceTrigger components share a topic, and each has a payload value configured, only messages matching that payload should be delivered to the corresponding channel Signed-off-by: Cody Cutrer Signed-off-by: Ciprian Pascu --- .../org/openhab/binding/mqtt/generic/ChannelState.java | 7 +++++++ .../internal/component/AbstractComponentTests.java | 9 +++++++++ .../internal/component/DeviceTriggerTests.java | 3 +++ 3 files changed, 19 insertions(+) diff --git a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/ChannelState.java b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/ChannelState.java index 681e5073e9a..7304ae57c7c 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/ChannelState.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/ChannelState.java @@ -173,6 +173,13 @@ public class ChannelState implements MqttMessageSubscriber { // Is trigger?: Special handling if (config.trigger) { + try { + cachedValue.parseMessage(new StringType(strValue)); + } catch (IllegalArgumentException e) { + // invalid value for this trigger; ignore + receivedOrTimeout(); + return; + } channelStateUpdateListener.triggerChannel(channelUID, strValue); receivedOrTimeout(); return; diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponentTests.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponentTests.java index bbb278d530f..4b090b6385d 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponentTests.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponentTests.java @@ -237,6 +237,15 @@ public abstract class AbstractComponentTests extends AbstractHomeAssistantTests verify(thingHandler).triggerChannel(eq(component.getChannel(channelId).getChannel().getUID()), eq(trigger)); } + /** + * Assert a channel does not triggers= + */ + protected void assertNotTriggered(AbstractComponent<@NonNull ? extends AbstractChannelConfiguration> component, + String channelId, String trigger) { + verify(thingHandler, never()).triggerChannel(eq(component.getChannel(channelId).getChannel().getUID()), + eq(trigger)); + } + /** * Assert that given payload was published exact-once on given topic. * diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/DeviceTriggerTests.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/DeviceTriggerTests.java index ad5f338cc9d..baa476704eb 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/DeviceTriggerTests.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/DeviceTriggerTests.java @@ -62,6 +62,9 @@ public class DeviceTriggerTests extends AbstractComponentTests { spyOnChannelUpdates(component, "action"); publishMessage("zigbee2mqtt/Charge Now Button/action", "on"); assertTriggered(component, "action", "on"); + + publishMessage("zigbee2mqtt/Charge Now Button/action", "off"); + assertNotTriggered(component, "action", "off"); } @Override