From 2cc529b1431dbb496e7b3d992e270e6f36f85aaf Mon Sep 17 00:00:00 2001 From: Thomas Leber Date: Sat, 7 Dec 2024 11:38:28 +0100 Subject: [PATCH] [awattar] change getNow to return an instant Signed-off-by: Thomas Leber --- .../internal/handler/AwattarBestPriceHandler.java | 15 ++++++++------- .../handler/AwattarBridgeHandlerTest.java | 9 +++++---- 2 files changed, 13 insertions(+), 11 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 c0ca4ca935c..24225233034 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 @@ -23,6 +23,7 @@ import static org.openhab.binding.awattar.internal.AwattarUtil.getCalendarForHou import static org.openhab.binding.awattar.internal.AwattarUtil.getDuration; import static org.openhab.binding.awattar.internal.AwattarUtil.getMillisToNextMinute; +import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.ArrayList; @@ -162,7 +163,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler { long diff; switch (channelId) { case CHANNEL_ACTIVE: - state = OnOffType.from(result.isActive(getNow(zoneId).toInstant())); + state = OnOffType.from(result.isActive(getNow())); break; case CHANNEL_START: state = new DateTimeType(Instant.ofEpochMilli(result.getStart())); @@ -171,7 +172,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler { state = new DateTimeType(Instant.ofEpochMilli(result.getEnd())); break; case CHANNEL_COUNTDOWN: - diff = result.getStart() - getNow(zoneId).toInstant().toEpochMilli(); + diff = result.getStart() - getNow().toEpochMilli(); if (diff >= 0) { state = getDuration(diff); } else { @@ -179,8 +180,8 @@ public class AwattarBestPriceHandler extends BaseThingHandler { } break; case CHANNEL_REMAINING: - if (result.isActive(getNow(zoneId).toInstant())) { - diff = result.getEnd() - getNow(zoneId).toInstant().toEpochMilli(); + if (result.isActive(getNow())) { + diff = result.getEnd() - getNow().toEpochMilli(); state = getDuration(diff); } else { state = QuantityType.valueOf(0, Units.MINUTE); @@ -226,7 +227,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler { protected TimeRange getRange(int start, int duration, ZoneId zoneId) { ZonedDateTime startTime = getStartTime(start, zoneId); ZonedDateTime endTime = startTime.plusHours(duration); - ZonedDateTime now = getNow(zoneId); + ZonedDateTime now = getNow().atZone(zoneId); if (now.getHour() < start) { // we are before the range, so we might be still within the last range startTime = startTime.minusDays(1); @@ -257,7 +258,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler { * @param zoneId the time zone * @return the current time */ - protected ZonedDateTime getNow(ZoneId zoneId) { - return ZonedDateTime.now(zoneId); + protected Instant getNow() { + return Instant.now(); } } 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 70f5fbb0246..6d6d949cf01 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 @@ -30,6 +30,7 @@ import static org.openhab.binding.awattar.internal.AwattarBindingConstants.CHANN import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; +import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Comparator; @@ -206,8 +207,8 @@ public class AwattarBridgeHandlerTest extends JavaTest { return ZonedDateTime.of(2024, 6, 15, 12, 0, 0, 0, zoneId); } - protected ZonedDateTime getNow(ZoneId zoneId) { - return ZonedDateTime.of(2024, 6, 15, 12, 0, 0, 0, zoneId); + protected Instant getNow() { + return ZonedDateTime.of(2024, 6, 15, 12, 0, 0, 0, ZoneId.of("GMT+2")).toInstant(); } }; @@ -262,8 +263,8 @@ public class AwattarBridgeHandlerTest extends JavaTest { return ZonedDateTime.of(2024, 6, 15, 0, 0, 0, 0, zoneId); } - protected ZonedDateTime getNow(ZoneId zoneId) { - return ZonedDateTime.of(2024, 6, 15, currentHour, currentMinute, 0, 0, zoneId); + protected Instant getNow() { + return ZonedDateTime.of(2024, 6, 15, currentHour, currentMinute, 0, 0, ZoneId.of("GMT+2")).toInstant(); } };