From bc6731157c180486ae3bc057d8a159c2ad866102 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Mon, 4 Nov 2024 10:40:31 +0100 Subject: [PATCH] Add support for more types as output of thing actions (#4435) Byte Short Long BigDecimal QuantityType LocalDate LocalTime LocalDateTime ZonedDateTime Instant Duration Signed-off-by: Laurent Garnier --- .../handler/AnnotationActionHandler.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/AnnotationActionHandler.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/AnnotationActionHandler.java index dd49381baf8..14a4d114015 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/AnnotationActionHandler.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/AnnotationActionHandler.java @@ -15,6 +15,13 @@ import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.math.BigDecimal; +import java.time.Duration; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -30,6 +37,7 @@ import org.openhab.core.automation.type.Input; import org.openhab.core.automation.type.Output; import org.openhab.core.automation.util.ActionInputsHelper; +import org.openhab.core.library.types.QuantityType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -138,14 +146,26 @@ public AnnotationActionHandler(Action module, ActionType mt, Method method, Obje // we allow simple data types as return values and put them under the context key "result". } else if (result instanceof Boolean booleanValue) { output.put(MODULE_RESULT, booleanValue); - } else if (result instanceof String) { - output.put(MODULE_RESULT, result); - } else if (result instanceof Integer) { - output.put(MODULE_RESULT, result); + } else if (result instanceof String stringValue) { + output.put(MODULE_RESULT, stringValue); + } else if (result instanceof Byte byteValue) { + output.put(MODULE_RESULT, byteValue); + } else if (result instanceof Short shortValue) { + output.put(MODULE_RESULT, shortValue); + } else if (result instanceof Integer integerValue) { + output.put(MODULE_RESULT, integerValue); + } else if (result instanceof Long longValue) { + output.put(MODULE_RESULT, longValue); } else if (result instanceof Double doubleValue) { output.put(MODULE_RESULT, doubleValue); } else if (result instanceof Float floatValue) { output.put(MODULE_RESULT, floatValue); + } else if (result instanceof BigDecimal bigDecimalValue) { + output.put(MODULE_RESULT, bigDecimalValue.doubleValue()); + } else if (result instanceof QuantityType || result instanceof LocalDate || result instanceof LocalTime + || result instanceof LocalDateTime || result instanceof ZonedDateTime || result instanceof Instant + || result instanceof Duration) { + output.put(MODULE_RESULT, result.toString()); } else { logger.warn("Non compatible return type '{}' on action method.", result.getClass()); }