Skip to content

Commit

Permalink
Set thing action scope from @ThingActionScope if set (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
seime authored Feb 20, 2025
1 parent bf09d9e commit 6c6f72a
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openhab.core.automation.annotation.RuleAction;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.thing.binding.ThingActionsScope;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -120,10 +121,24 @@ public boolean generateActionsSource(Collection<Thing> things) {
return false;
}

String getActionsScope(Thing thing) {
if (thing.getHandler() != null) {
// Check if the ThingHandlerService has a ThingActionsScope annotation
Class<? extends ThingHandlerService> thingActionsClass = thing.getHandler().getServices().stream()
.filter(ThingActions.class::isAssignableFrom).findFirst()
.orElseThrow(() -> new IllegalStateException("should not occur here"));
if (thingActionsClass.getAnnotation(ThingActionsScope.class) != null) {
return thingActionsClass.getAnnotation(ThingActionsScope.class).name();
}
}
// Else default to the binding id
return thing.getUID().getBindingId();
}

private Map<String, Object> createActionsModel(Thing thing) {
Map<String, Object> freemarkerModel = new HashMap<>();
freemarkerModel.put("id", thing.getUID().toString());
freemarkerModel.put("scope", thing.getUID().getBindingId());
freemarkerModel.put("scope", getActionsScope(thing));
freemarkerModel.put("name", StringUtils.uncapitalize(getActionFriendlyName(thing.getUID().toString())));
freemarkerModel.put("package", jRuleConfig.getGeneratedActionPackage());
freemarkerModel.put("class",
Expand Down

0 comments on commit 6c6f72a

Please sign in to comment.