From ad189bd4f14899e0399368f3700c10f04b62d0a3 Mon Sep 17 00:00:00 2001 From: Kai Kreuzer Date: Wed, 16 Dec 2020 15:11:46 +0100 Subject: [PATCH] [automation] Make quantity types available in scripts (#1940) Fixes #1763 Signed-off-by: Kai Kreuzer --- bundles/org.openhab.core.model.rule/bnd.bnd | 1 + bundles/org.openhab.core.model.script/bnd.bnd | 3 ++- .../scoping/ScriptImportSectionNamespaceScopeProvider.java | 2 ++ .../core/model/script/engine/ScriptEngineOSGiTest.java | 4 +++- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.core.model.rule/bnd.bnd b/bundles/org.openhab.core.model.rule/bnd.bnd index 3e380d6c90..7d16b1ed4a 100644 --- a/bundles/org.openhab.core.model.rule/bnd.bnd +++ b/bundles/org.openhab.core.model.rule/bnd.bnd @@ -33,6 +33,7 @@ Import-Package: \ org.openhab.core.model.script.engine.action,\ com.google.common.base;version="14",\ javax.measure,\ + javax.measure.quantity,\ org.eclipse.jdt.annotation;resolution:=optional,\ org.eclipse.xtext.xbase.lib,\ org.osgi.*,\ diff --git a/bundles/org.openhab.core.model.script/bnd.bnd b/bundles/org.openhab.core.model.script/bnd.bnd index 1cb054537c..87e93c80a8 100644 --- a/bundles/org.openhab.core.model.script/bnd.bnd +++ b/bundles/org.openhab.core.model.script/bnd.bnd @@ -43,7 +43,8 @@ Import-Package: \ org.openhab.core.io.net.http,\ org.openhab.core.model.core,\ com.google.common.*;version="14",\ - javax.measure.*,\ + javax.measure,\ + javax.measure.quantity,\ org.apache.*,\ org.eclipse.jdt.annotation;resolution:=optional,\ org.eclipse.jetty.http.*,\ diff --git a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ScriptImportSectionNamespaceScopeProvider.java b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ScriptImportSectionNamespaceScopeProvider.java index efcb55e345..96314044c5 100644 --- a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ScriptImportSectionNamespaceScopeProvider.java +++ b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ScriptImportSectionNamespaceScopeProvider.java @@ -35,6 +35,7 @@ public class ScriptImportSectionNamespaceScopeProvider extends XImportSectionNam public static final QualifiedName MODEL_SCRIPT_ACTIONS_PACKAGE = QualifiedName.create("org", "openhab", "core", "model", "script", "actions"); public static final QualifiedName TIME_PACKAGE = QualifiedName.create("java", "time"); + public static final QualifiedName QUANTITY_PACKAGE = QualifiedName.create("javax", "measure", "quantity"); @Override protected List getImplicitImports(boolean ignoreCase) { @@ -46,6 +47,7 @@ public class ScriptImportSectionNamespaceScopeProvider extends XImportSectionNam implicitImports.add(doCreateImportNormalizer(CORE_PERSISTENCE_PACKAGE, true, false)); implicitImports.add(doCreateImportNormalizer(MODEL_SCRIPT_ACTIONS_PACKAGE, true, false)); implicitImports.add(doCreateImportNormalizer(TIME_PACKAGE, true, false)); + implicitImports.add(doCreateImportNormalizer(QUANTITY_PACKAGE, true, false)); return implicitImports; } diff --git a/itests/org.openhab.core.model.script.tests/src/main/java/org/openhab/core/model/script/engine/ScriptEngineOSGiTest.java b/itests/org.openhab.core.model.script.tests/src/main/java/org/openhab/core/model/script/engine/ScriptEngineOSGiTest.java index 9035ba0e0c..21f5d676e1 100644 --- a/itests/org.openhab.core.model.script.tests/src/main/java/org/openhab/core/model/script/engine/ScriptEngineOSGiTest.java +++ b/itests/org.openhab.core.model.script.tests/src/main/java/org/openhab/core/model/script/engine/ScriptEngineOSGiTest.java @@ -262,7 +262,9 @@ public class ScriptEngineOSGiTest extends JavaOSGiTest { Item numberItem = itemRegistry.get(NUMBER_ITEM_LENGTH); ((NumberItem) numberItem).setState(new QuantityType<>("1 m")); - assertThat((QuantityType) runScript("val length = NumberC.state as QuantityType; return length / 2|cm;"), + assertThat( + (QuantityType) runScript( + "val length = NumberC.state as QuantityType; return length / 2|cm;"), is(QuantityType.valueOf("50"))); }