]> git.basschouten.com Git - openhab-addons.git/commitdiff
[jpa] Fix return value for UoM items (#16977)
authorJ-N-K <github@klug.nrw>
Mon, 1 Jul 2024 21:24:04 +0000 (23:24 +0200)
committerGitHub <noreply@github.com>
Mon, 1 Jul 2024 21:24:04 +0000 (23:24 +0200)
Signed-off-by: Jan N. Klug <github@klug.nrw>
bundles/org.openhab.persistence.jpa/src/main/java/org/openhab/persistence/jpa/internal/JpaHistoricItem.java

index a39bc676f77f062e53e1bc2f2cce5ca8713a3480..eaee4bb8caadb7f23c6288c592c3c925d5cf247b 100644 (file)
@@ -19,6 +19,8 @@ import java.time.ZonedDateTime;
 import java.util.List;
 import java.util.stream.Collectors;
 
+import javax.measure.Unit;
+
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.core.items.Item;
 import org.openhab.core.library.items.ContactItem;
@@ -34,6 +36,7 @@ import org.openhab.core.library.types.OnOffType;
 import org.openhab.core.library.types.OpenClosedType;
 import org.openhab.core.library.types.PercentType;
 import org.openhab.core.library.types.PointType;
+import org.openhab.core.library.types.QuantityType;
 import org.openhab.core.library.types.StringListType;
 import org.openhab.core.library.types.StringType;
 import org.openhab.core.persistence.HistoricItem;
@@ -100,8 +103,10 @@ public class JpaHistoricItem implements HistoricItem {
      */
     public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
         State state;
-        if (item instanceof NumberItem) {
-            state = new DecimalType(Double.valueOf(pItem.getValue()));
+        if (item instanceof NumberItem numberItem) {
+            Unit<?> unit = numberItem.getUnit();
+            double value = Double.parseDouble(pItem.getValue());
+            state = (unit == null) ? new DecimalType(value) : new QuantityType<>(value, unit);
         } else if (item instanceof DimmerItem) {
             state = new PercentType(Integer.parseInt(pItem.getValue()));
         } else if (item instanceof SwitchItem) {