[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 <cody@cutrer.us>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Cody Cutrer 2024-10-18 07:24:50 -06:00 committed by Ciprian Pascu
parent 6f283a47d4
commit 69c519e06d
3 changed files with 19 additions and 0 deletions

View 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;

View 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.
*

View 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