From 80fb59525bb38fc6b62dbb1b251cff35326fd021 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sun, 14 Apr 2024 09:23:41 +0200 Subject: [PATCH] Improve error handling for action calculatePrice (#16651) Signed-off-by: Jacob Laursen --- bundles/org.openhab.binding.energidataservice/README.md | 9 +++++++-- .../internal/action/EnergiDataServiceActions.java | 8 ++++---- .../internal/action/EnergiDataServiceActionsTest.java | 9 +++++++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/bundles/org.openhab.binding.energidataservice/README.md b/bundles/org.openhab.binding.energidataservice/README.md index 05e5ab6ae54..7c7434c9f86 100644 --- a/bundles/org.openhab.binding.energidataservice/README.md +++ b/bundles/org.openhab.binding.energidataservice/README.md @@ -303,6 +303,7 @@ var Map result = actions.calculateCheapestPeriod(now.toInstant() **Result:** Price as `BigDecimal`. This action calculates the price for using given amount of power in the period from `start` till `end`. +Returns `null` if the calculation cannot be performed due to missing price data within the requested period. Example: @@ -402,7 +403,9 @@ var priceMap = actions.getPrices("SpotPrice,GridTariff"); logInfo("Current spot price + grid tariff excl. VAT", priceMap.get(hourStart).toString) var price = actions.calculatePrice(Instant.now, now.plusHours(1).toInstant, 150 | W) -logInfo("Total price for using 150 W for the next hour", price.toString) +if (price != null) { + logInfo("Total price for using 150 W for the next hour", price.toString) +} val ArrayList durationPhases = new ArrayList() durationPhases.add(Duration.ofMinutes(37)) @@ -467,7 +470,9 @@ utils.javaMapToJsMap(edsActions.getPrices("SpotPrice,GridTariff")).forEach((valu console.log("Current spot price + grid tariff excl. VAT: " + priceMap.get(hourStart.toString())); var price = edsActions.calculatePrice(time.Instant.now(), time.Instant.now().plusSeconds(3600), Quantity("150 W")); -console.log("Total price for using 150 W for the next hour: " + price.toString()); +if (price !== null) { + console.log("Total price for using 150 W for the next hour: " + price.toString()); +} var durationPhases = []; durationPhases.push(time.Duration.ofMinutes(37)); diff --git a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActions.java b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActions.java index a881dbe733c..3af139c3115 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActions.java +++ b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActions.java @@ -139,7 +139,7 @@ public class EnergiDataServiceActions implements ThingActions { } @RuleAction(label = "@text/action.calculate-price.label", description = "@text/action.calculate-price.description") - public @ActionOutput(name = "price", type = "java.math.BigDecimal") BigDecimal calculatePrice( + public @ActionOutput(name = "price", type = "java.math.BigDecimal") @Nullable BigDecimal calculatePrice( @ActionInput(name = "start", type = "java.time.Instant") Instant start, @ActionInput(name = "end", type = "java.time.Instant") Instant end, @ActionInput(name = "power", type = "QuantityType") QuantityType power) { @@ -149,7 +149,7 @@ public class EnergiDataServiceActions implements ThingActions { return priceCalculator.calculatePrice(start, end, power); } catch (MissingPriceException e) { logger.warn("{}", e.getMessage()); - return BigDecimal.ZERO; + return null; } } @@ -314,10 +314,10 @@ public class EnergiDataServiceActions implements ThingActions { * @param power Constant power consumption * @return Map of prices */ - public static BigDecimal calculatePrice(@Nullable ThingActions actions, @Nullable Instant start, + public static @Nullable BigDecimal calculatePrice(@Nullable ThingActions actions, @Nullable Instant start, @Nullable Instant end, @Nullable QuantityType power) { if (start == null || end == null || power == null) { - return BigDecimal.ZERO; + return null; } if (actions instanceof EnergiDataServiceActions serviceActions) { return serviceActions.calculatePrice(start, end, power); diff --git a/bundles/org.openhab.binding.energidataservice/src/test/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActionsTest.java b/bundles/org.openhab.binding.energidataservice/src/test/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActionsTest.java index 80780dfa224..5cd246c2e81 100644 --- a/bundles/org.openhab.binding.energidataservice/src/test/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActionsTest.java +++ b/bundles/org.openhab.binding.energidataservice/src/test/java/org/openhab/binding/energidataservice/internal/action/EnergiDataServiceActionsTest.java @@ -33,6 +33,7 @@ import java.util.stream.Collectors; import javax.measure.quantity.Power; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -239,6 +240,7 @@ public class EnergiDataServiceActionsTest { void calculatePriceSimple() throws IOException { mockCommonDatasets(actions); + @Nullable BigDecimal actual = actions.calculatePrice(Instant.parse("2023-02-04T15:30:00Z"), Instant.parse("2023-02-04T16:30:00Z"), new QuantityType<>(150, Units.WATT)); assertThat(actual, is(equalTo(new BigDecimal("0.311447631975000000")))); // 0.3114476319750 @@ -257,6 +259,7 @@ public class EnergiDataServiceActionsTest { void calculatePriceFullHours() throws IOException { mockCommonDatasets(actions); + @Nullable BigDecimal actual = actions.calculatePrice(Instant.parse("2023-02-04T15:00:00Z"), Instant.parse("2023-02-04T17:00:00Z"), new QuantityType<>(1, Units.KILOVAR)); assertThat(actual, is(equalTo(new BigDecimal("4.152635093000000000")))); // 4.152635093 @@ -266,18 +269,20 @@ public class EnergiDataServiceActionsTest { void calculatePriceOutOfRangeStart() throws IOException { mockCommonDatasets(actions); + @Nullable BigDecimal actual = actions.calculatePrice(Instant.parse("2023-02-03T23:59:00Z"), Instant.parse("2023-02-04T12:30:00Z"), new QuantityType<>(1000, Units.WATT)); - assertThat(actual, is(equalTo(BigDecimal.ZERO))); + assertThat(actual, is(nullValue())); } @Test void calculatePriceOutOfRangeEnd() throws IOException { mockCommonDatasets(actions); + @Nullable BigDecimal actual = actions.calculatePrice(Instant.parse("2023-02-05T22:00:00Z"), Instant.parse("2023-02-05T23:01:00Z"), new QuantityType<>(1000, Units.WATT)); - assertThat(actual, is(equalTo(BigDecimal.ZERO))); + assertThat(actual, is(nullValue())); } /**