]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mqtt.homeassistant] avoid improperly delivered triggers (#17584)
authorCody Cutrer <cody@cutrer.us>
Fri, 18 Oct 2024 13:24:50 +0000 (07:24 -0600)
committerGitHub <noreply@github.com>
Fri, 18 Oct 2024 13:24:50 +0000 (15:24 +0200)
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 <cody@cutrer.us>
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/ChannelState.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/AbstractComponentTests.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/DeviceTriggerTests.java

index 681e5073e9af0d86cbb521b0d405af7c7a123004..7304ae57c7c26fbe0ba75b7250f48a3b94d3aae7 100644 (file)
@@ -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;
index bbb278d530fbfd3fcc0fda6358a3dd96913882bb..4b090b6385d9b7d8cba7863c0b66942e673f5fc8 100644 (file)
@@ -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.
      *
index ad5f338cc9da31d91122f0eafb3c09bae754d73a..baa476704eb949e050db8efd207c064cd7b136df 100644 (file)
@@ -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