From e84874ab90508cbd97283c9e10c91a3bd6f7d649 Mon Sep 17 00:00:00 2001 From: Arne Seime Date: Wed, 25 Dec 2024 12:54:31 +0100 Subject: [PATCH 1/3] Set thing action scope from @ThingActionScope if set --- .../jrule/actions/JRuleActionClassGenerator.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openhab/automation/jrule/actions/JRuleActionClassGenerator.java b/src/main/java/org/openhab/automation/jrule/actions/JRuleActionClassGenerator.java index 6cc07e97..acd2baca 100644 --- a/src/main/java/org/openhab/automation/jrule/actions/JRuleActionClassGenerator.java +++ b/src/main/java/org/openhab/automation/jrule/actions/JRuleActionClassGenerator.java @@ -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; @@ -123,7 +124,16 @@ public boolean generateActionsSource(Collection things) { private Map createActionsModel(Thing thing) { Map freemarkerModel = new HashMap<>(); freemarkerModel.put("id", thing.getUID().toString()); - freemarkerModel.put("scope", thing.getUID().getBindingId()); + if (thing.getHandler() != null) { + Class thingActionsClass = thing.getHandler().getServices().stream() + .filter(ThingActions.class::isAssignableFrom).findFirst() + .orElseThrow(() -> new IllegalStateException("should not occur here")); + if (thingActionsClass.getAnnotation(ThingActionsScope.class) != null) { + freemarkerModel.put("scope", thingActionsClass.getAnnotation(ThingActionsScope.class).name()); + } + } else { + freemarkerModel.put("scope", thing.getUID().getBindingId()); + } freemarkerModel.put("name", StringUtils.uncapitalize(getActionFriendlyName(thing.getUID().toString()))); freemarkerModel.put("package", jRuleConfig.getGeneratedActionPackage()); freemarkerModel.put("class", From f86c7c411b41f275e146142bffaa277dddd86fa3 Mon Sep 17 00:00:00 2001 From: Arne Seime Date: Wed, 25 Dec 2024 13:21:10 +0100 Subject: [PATCH 2/3] Only do 1 pr at a time and run tests inbetween ;) --- .../automation/jrule/actions/JRuleActionClassGenerator.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/openhab/automation/jrule/actions/JRuleActionClassGenerator.java b/src/main/java/org/openhab/automation/jrule/actions/JRuleActionClassGenerator.java index acd2baca..39492bcc 100644 --- a/src/main/java/org/openhab/automation/jrule/actions/JRuleActionClassGenerator.java +++ b/src/main/java/org/openhab/automation/jrule/actions/JRuleActionClassGenerator.java @@ -124,6 +124,7 @@ public boolean generateActionsSource(Collection things) { private Map createActionsModel(Thing thing) { Map freemarkerModel = new HashMap<>(); freemarkerModel.put("id", thing.getUID().toString()); + freemarkerModel.put("scope", thing.getUID().getBindingId()); if (thing.getHandler() != null) { Class thingActionsClass = thing.getHandler().getServices().stream() .filter(ThingActions.class::isAssignableFrom).findFirst() @@ -131,8 +132,6 @@ private Map createActionsModel(Thing thing) { if (thingActionsClass.getAnnotation(ThingActionsScope.class) != null) { freemarkerModel.put("scope", thingActionsClass.getAnnotation(ThingActionsScope.class).name()); } - } else { - freemarkerModel.put("scope", thing.getUID().getBindingId()); } freemarkerModel.put("name", StringUtils.uncapitalize(getActionFriendlyName(thing.getUID().toString()))); freemarkerModel.put("package", jRuleConfig.getGeneratedActionPackage()); From dd7ce756a733f31da017198847cf44fbd0efd18b Mon Sep 17 00:00:00 2001 From: Arne Seime Date: Mon, 6 Jan 2025 18:20:14 +0100 Subject: [PATCH 3/3] Improve code --- .../jrule/actions/JRuleActionClassGenerator.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openhab/automation/jrule/actions/JRuleActionClassGenerator.java b/src/main/java/org/openhab/automation/jrule/actions/JRuleActionClassGenerator.java index 39492bcc..3be4ecb9 100644 --- a/src/main/java/org/openhab/automation/jrule/actions/JRuleActionClassGenerator.java +++ b/src/main/java/org/openhab/automation/jrule/actions/JRuleActionClassGenerator.java @@ -121,18 +121,24 @@ public boolean generateActionsSource(Collection things) { return false; } - private Map createActionsModel(Thing thing) { - Map freemarkerModel = new HashMap<>(); - freemarkerModel.put("id", thing.getUID().toString()); - freemarkerModel.put("scope", thing.getUID().getBindingId()); + String getActionsScope(Thing thing) { if (thing.getHandler() != null) { + // Check if the ThingHandlerService has a ThingActionsScope annotation Class thingActionsClass = thing.getHandler().getServices().stream() .filter(ThingActions.class::isAssignableFrom).findFirst() .orElseThrow(() -> new IllegalStateException("should not occur here")); if (thingActionsClass.getAnnotation(ThingActionsScope.class) != null) { - freemarkerModel.put("scope", thingActionsClass.getAnnotation(ThingActionsScope.class).name()); + return thingActionsClass.getAnnotation(ThingActionsScope.class).name(); } } + // Else default to the binding id + return thing.getUID().getBindingId(); + } + + private Map createActionsModel(Thing thing) { + Map freemarkerModel = new HashMap<>(); + freemarkerModel.put("id", thing.getUID().toString()); + freemarkerModel.put("scope", getActionsScope(thing)); freemarkerModel.put("name", StringUtils.uncapitalize(getActionFriendlyName(thing.getUID().toString()))); freemarkerModel.put("package", jRuleConfig.getGeneratedActionPackage()); freemarkerModel.put("class",