Skip to content

Commit 772026e

Browse files
authored
Fix last hour not being considered for best price (#17731)
Resolves #17316 Signed-off-by: Jacob Laursen <[email protected]>
1 parent 10048bc commit 772026e

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBestPriceHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void refreshChannel(ChannelUID channelUID) {
142142
}
143143
AwattarBestPriceConfiguration config = getConfigAs(AwattarBestPriceConfiguration.class);
144144
TimeRange timerange = getRange(config.rangeStart, config.rangeDuration, bridgeHandler.getTimeZone());
145-
if (!(bridgeHandler.containsPriceFor(timerange.start()) && bridgeHandler.containsPriceFor(timerange.end()))) {
145+
if (!(bridgeHandler.containsPriceFor(timerange))) {
146146
updateState(channelUID, state);
147147
return;
148148
}

bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandler.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,20 @@ public synchronized SortedSet<AwattarPrice> getPrices() {
239239

240240
public boolean containsPriceFor(long timestamp) {
241241
SortedSet<AwattarPrice> localPrices = getPrices();
242-
return localPrices != null && localPrices.first().timerange().start() <= timestamp
243-
&& localPrices.last().timerange().end() > timestamp;
242+
if (localPrices == null) {
243+
return false;
244+
}
245+
return new TimeRange(localPrices.first().timerange().start(), localPrices.last().timerange().end())
246+
.contains(timestamp);
247+
}
248+
249+
public boolean containsPriceFor(TimeRange timeRange) {
250+
SortedSet<AwattarPrice> localPrices = getPrices();
251+
if (localPrices == null) {
252+
return false;
253+
}
254+
return new TimeRange(localPrices.first().timerange().start(), localPrices.last().timerange().end())
255+
.contains(timeRange);
244256
}
245257

246258
@Override

bundles/org.openhab.binding.awattar/src/main/java/org/openhab/binding/awattar/internal/handler/TimeRange.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public boolean contains(TimeRange other) {
4848
* @param o the object to be compared
4949
* @return the result of {@link Long#compare(long, long)} for the {@link #start} timestamps
5050
*/
51+
@Override
5152
public int compareTo(TimeRange o) {
5253
return Long.compare(start, o.start);
5354
}

bundles/org.openhab.binding.awattar/src/test/java/org/openhab/binding/awattar/internal/handler/AwattarBridgeHandlerTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,17 @@ void testGetPriceForFail() {
150150
}
151151

152152
@Test
153-
void testContainsPrizeFor() {
153+
void testContainsPriceForTimestamp() {
154+
assertThat(bridgeHandler.containsPriceFor(new TimeRange(1618503200000L, 1718316000000L)), is(false));
155+
assertThat(bridgeHandler.containsPriceFor(new TimeRange(1618503200000L, 1718503200000L)), is(false));
156+
assertThat(bridgeHandler.containsPriceFor(new TimeRange(1718503200000L, 1718575200000L)), is(true));
157+
}
158+
159+
@Test
160+
void testContainsPriceForRange() {
154161
assertThat(bridgeHandler.containsPriceFor(1618503200000L), is(false));
155162
assertThat(bridgeHandler.containsPriceFor(1718503200000L), is(true));
163+
assertThat(bridgeHandler.containsPriceFor(1718575200000L), is(false));
156164
assertThat(bridgeHandler.containsPriceFor(1818503200000L), is(false));
157165
}
158166

0 commit comments

Comments
 (0)