[awattar] change getNow to return an instant

Signed-off-by: Thomas Leber <thomas@tl-photography.at>
This commit is contained in:
Thomas Leber 2024-12-07 11:38:28 +01:00
parent a5a7e7c3c9
commit 2cc529b143
2 changed files with 13 additions and 11 deletions

View File

@ -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.getDuration;
import static org.openhab.binding.awattar.internal.AwattarUtil.getMillisToNextMinute; import static org.openhab.binding.awattar.internal.AwattarUtil.getMillisToNextMinute;
import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
@ -162,7 +163,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler {
long diff; long diff;
switch (channelId) { switch (channelId) {
case CHANNEL_ACTIVE: case CHANNEL_ACTIVE:
state = OnOffType.from(result.isActive(getNow(zoneId).toInstant())); state = OnOffType.from(result.isActive(getNow()));
break; break;
case CHANNEL_START: case CHANNEL_START:
state = new DateTimeType(Instant.ofEpochMilli(result.getStart())); state = new DateTimeType(Instant.ofEpochMilli(result.getStart()));
@ -171,7 +172,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler {
state = new DateTimeType(Instant.ofEpochMilli(result.getEnd())); state = new DateTimeType(Instant.ofEpochMilli(result.getEnd()));
break; break;
case CHANNEL_COUNTDOWN: case CHANNEL_COUNTDOWN:
diff = result.getStart() - getNow(zoneId).toInstant().toEpochMilli(); diff = result.getStart() - getNow().toEpochMilli();
if (diff >= 0) { if (diff >= 0) {
state = getDuration(diff); state = getDuration(diff);
} else { } else {
@ -179,8 +180,8 @@ public class AwattarBestPriceHandler extends BaseThingHandler {
} }
break; break;
case CHANNEL_REMAINING: case CHANNEL_REMAINING:
if (result.isActive(getNow(zoneId).toInstant())) { if (result.isActive(getNow())) {
diff = result.getEnd() - getNow(zoneId).toInstant().toEpochMilli(); diff = result.getEnd() - getNow().toEpochMilli();
state = getDuration(diff); state = getDuration(diff);
} else { } else {
state = QuantityType.valueOf(0, Units.MINUTE); state = QuantityType.valueOf(0, Units.MINUTE);
@ -226,7 +227,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler {
protected TimeRange getRange(int start, int duration, ZoneId zoneId) { protected TimeRange getRange(int start, int duration, ZoneId zoneId) {
ZonedDateTime startTime = getStartTime(start, zoneId); ZonedDateTime startTime = getStartTime(start, zoneId);
ZonedDateTime endTime = startTime.plusHours(duration); ZonedDateTime endTime = startTime.plusHours(duration);
ZonedDateTime now = getNow(zoneId); ZonedDateTime now = getNow().atZone(zoneId);
if (now.getHour() < start) { if (now.getHour() < start) {
// we are before the range, so we might be still within the last range // we are before the range, so we might be still within the last range
startTime = startTime.minusDays(1); startTime = startTime.minusDays(1);
@ -257,7 +258,7 @@ public class AwattarBestPriceHandler extends BaseThingHandler {
* @param zoneId the time zone * @param zoneId the time zone
* @return the current time * @return the current time
*/ */
protected ZonedDateTime getNow(ZoneId zoneId) { protected Instant getNow() {
return ZonedDateTime.now(zoneId); return Instant.now();
} }
} }

View File

@ -30,6 +30,7 @@ import static org.openhab.binding.awattar.internal.AwattarBindingConstants.CHANN
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.Comparator; import java.util.Comparator;
@ -206,8 +207,8 @@ public class AwattarBridgeHandlerTest extends JavaTest {
return ZonedDateTime.of(2024, 6, 15, 12, 0, 0, 0, zoneId); return ZonedDateTime.of(2024, 6, 15, 12, 0, 0, 0, zoneId);
} }
protected ZonedDateTime getNow(ZoneId zoneId) { protected Instant getNow() {
return ZonedDateTime.of(2024, 6, 15, 12, 0, 0, 0, zoneId); 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); return ZonedDateTime.of(2024, 6, 15, 0, 0, 0, 0, zoneId);
} }
protected ZonedDateTime getNow(ZoneId zoneId) { protected Instant getNow() {
return ZonedDateTime.of(2024, 6, 15, currentHour, currentMinute, 0, 0, zoneId); return ZonedDateTime.of(2024, 6, 15, currentHour, currentMinute, 0, 0, ZoneId.of("GMT+2")).toInstant();
} }
}; };