Skip to content

Commit

Permalink
Add support for more types as output of thing actions (#4435)
Browse files Browse the repository at this point in the history
Byte
Short
Long
BigDecimal
QuantityType
LocalDate
LocalTime
LocalDateTime
ZonedDateTime
Instant
Duration

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored Nov 4, 2024
1 parent 9646607 commit bc67311
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit bc67311

Please sign in to comment.