[automation] Allow using * as wildcard in thing status triggers (#2832)

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
J-N-K 2022-04-25 14:16:44 +02:00 committed by GitHub
parent 26596ecc25
commit 86960f243f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -17,6 +17,7 @@ import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@ -65,6 +66,7 @@ public class ThingStatusTriggerHandler extends BaseTriggerModuleHandler implemen
private final BundleContext bundleContext;
private final ServiceRegistration<?> eventSubscriberRegistration;
private final Pattern eventTopicFilter;
public ThingStatusTriggerHandler(Trigger module, BundleContext bundleContext) {
super(module);
@ -77,6 +79,9 @@ public class ThingStatusTriggerHandler extends BaseTriggerModuleHandler implemen
this.types = Set.of(ThingStatusInfoChangedEvent.TYPE);
}
this.bundleContext = bundleContext;
this.eventTopicFilter = Pattern.compile("^openhab/things/" + thingUID.replace("*", ".*?") + "/.*$");
Dictionary<String, Object> properties = new Hashtable<>();
properties.put("event.topics", "openhab/things/" + thingUID + "/*");
eventSubscriberRegistration = this.bundleContext.registerService(EventSubscriber.class.getName(), this,
@ -143,6 +148,6 @@ public class ThingStatusTriggerHandler extends BaseTriggerModuleHandler implemen
@Override
public boolean apply(Event event) {
logger.trace("->FILTER: {}: {}", event.getTopic(), thingUID);
return event.getTopic().contains("openhab/things/" + thingUID + "/");
return eventTopicFilter.matcher(event.getTopic()).matches();
}
}

View File

@ -10,7 +10,7 @@
"type": "TEXT",
"context": "thing",
"label": "Thing",
"description": "The UID of the thing.",
"description": "The UID of the thing. You can use '*' as wildcard to match multiple things.",
"required": true
},
{