Skip to content

Commit

Permalink
[mqtt.homeassistant] avoid improperly delivered triggers (openhab#17584)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ccutrer authored and matchews committed Oct 18, 2024
1 parent d56fdee commit a1d6440
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ public void processMessage(String topic, byte[] payload) {

// 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ protected void assertTriggered(AbstractComponent<@NonNull ? extends AbstractChan
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public void test() throws InterruptedException {
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
Expand Down

0 comments on commit a1d6440

Please sign in to comment.