From dab00add8033bc9681652f9816eab15d5c090c84 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Wed, 13 Nov 2024 07:41:17 +0100 Subject: [PATCH] Fix last hour not being considered for best price (#17731) Resolves #17316 Signed-off-by: Jacob Laursen Signed-off-by: Ciprian Pascu --- .../handler/AwattarBestPriceHandler.java | 2 +- .../internal/handler/AwattarBridgeHandler.java | 16 ++++++++++++++-- .../awattar/internal/handler/TimeRange.java | 1 + .../handler/AwattarBridgeHandlerTest.java | 10 +++++++++- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBestPriceHandler.java b/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBestPriceHandler.java index e2fb8585c2c..39540ae5de4 100644 --- a/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBestPriceHandler.java +++ b/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBestPriceHandler.java @@ -142,7 +142,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler { } AwattarBestPriceConfiguration config = getConfigAs(AwattarBestPriceConfiguration.class); TimeRange timerange = getRange(config.rangeStart, config.rangeDuration, bridgeHandler.getTimeZone()); - if (!(bridgeHandler.containsPriceFor(timerange.start()) && bridgeHandler.containsPriceFor(timerange.end()))) { + if (!(bridgeHandler.containsPriceFor(timerange))) { updateState(channelUID, state); return; } diff --git a/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandler.java b/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandler.java index 3eb99114018..e11fcdfce81 100644 --- a/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandler.java +++ b/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandler.java @@ -239,8 +239,20 @@ public class AwattarBridgeHandler extends BaseBridgeHandler { public boolean containsPriceFor(long timestamp) { SortedSet localPrices = getPrices(); - return localPrices != null && localPrices.first().timerange().start() <= timestamp - && localPrices.last().timerange().end() > timestamp; + if (localPrices == null) { + return false; + } + return new TimeRange(localPrices.first().timerange().start(), localPrices.last().timerange().end()) + .contains(timestamp); + } + + public boolean containsPriceFor(TimeRange timeRange) { + SortedSet localPrices = getPrices(); + if (localPrices == null) { + return false; + } + return new TimeRange(localPrices.first().timerange().start(), localPrices.last().timerange().end()) + .contains(timeRange); } @Override diff --git a/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/TimeRange.java b/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/TimeRange.java index 7e3bf8815c1..2ff339f8097 100644 --- a/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/TimeRange.java +++ b/bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/TimeRange.java @@ -48,6 +48,7 @@ public record TimeRange(long start, long end) implements Comparable { * @param o the object to be compared * @return the result of {@link Long#compare(long, long)} for the {@link #start} timestamps */ + @Override public int compareTo(TimeRange o) { return Long.compare(start, o.start); } diff --git a/bundles/org.openhab.binding.awattar/src/test/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandlerTest.java b/bundles/org.openhab.binding.awattar/src/test/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandlerTest.java index 819174b2aef..37dde69ac64 100644 --- a/bundles/org.openhab.binding.awattar/src/test/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandlerTest.java +++ b/bundles/org.openhab.binding.awattar/src/test/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandlerTest.java @@ -150,9 +150,17 @@ public class AwattarBridgeHandlerTest extends JavaTest { } @Test - void testContainsPrizeFor() { + void testContainsPriceForTimestamp() { + assertThat(bridgeHandler.containsPriceFor(new TimeRange(1618503200000L, 1718316000000L)), is(false)); + assertThat(bridgeHandler.containsPriceFor(new TimeRange(1618503200000L, 1718503200000L)), is(false)); + assertThat(bridgeHandler.containsPriceFor(new TimeRange(1718503200000L, 1718575200000L)), is(true)); + } + + @Test + void testContainsPriceForRange() { assertThat(bridgeHandler.containsPriceFor(1618503200000L), is(false)); assertThat(bridgeHandler.containsPriceFor(1718503200000L), is(true)); + assertThat(bridgeHandler.containsPriceFor(1718575200000L), is(false)); assertThat(bridgeHandler.containsPriceFor(1818503200000L), is(false)); }