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 <lg.hc@free.fr>
This commit is contained in:
lolodomo 2024-11-04 10:40:31 +01:00 committed by GitHub
parent 9646607e47
commit bc6731157c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -15,6 +15,13 @@ package org.openhab.core.automation.internal.module.handler;
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.ActionType;
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 class AnnotationActionHandler extends BaseActionModuleHandler {
// 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());
}