Simplify DateTimeType handling for VolvoOnCall

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-11-26 23:00:30 +01:00 committed by lsiepel
parent df8f92e847
commit 9c19aa70e9
3 changed files with 6 additions and 8 deletions

View File

@ -15,7 +15,6 @@ package org.openhab.binding.volvooncall.internal.dto;
import static org.openhab.binding.volvooncall.internal.VolvoOnCallBindingConstants.UNDEFINED; import static org.openhab.binding.volvooncall.internal.VolvoOnCallBindingConstants.UNDEFINED;
import java.time.Duration; import java.time.Duration;
import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.temporal.Temporal; import java.time.temporal.Temporal;
import java.util.Optional; import java.util.Optional;
@ -47,8 +46,7 @@ public class TripDetail {
private @NonNullByDefault({}) PositionData endPosition; private @NonNullByDefault({}) PositionData endPosition;
private State ZonedDateTimeToState(@Nullable ZonedDateTime datetime) { private State ZonedDateTimeToState(@Nullable ZonedDateTime datetime) {
return datetime != null ? new DateTimeType(datetime.withZoneSameInstant(ZoneId.systemDefault())) return datetime != null ? new DateTimeType(datetime) : UnDefType.NULL;
: UnDefType.NULL;
} }
private State getPositionAsState(PositionData details) { private State getPositionAsState(PositionData details) {

View File

@ -17,7 +17,8 @@ import static org.openhab.core.library.unit.MetricPrefix.KILO;
import static org.openhab.core.library.unit.SIUnits.*; import static org.openhab.core.library.unit.SIUnits.*;
import static org.openhab.core.library.unit.Units.*; import static org.openhab.core.library.unit.Units.*;
import java.time.ZonedDateTime; import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -426,7 +427,8 @@ public class VehicleHandler extends BaseThingHandler {
: UnDefType.UNDEF; : UnDefType.UNDEF;
case CHARGING_END: case CHARGING_END:
return hvBattery.timeToHVBatteryFullyCharged != UNDEFINED && hvBattery.timeToHVBatteryFullyCharged > 0 return hvBattery.timeToHVBatteryFullyCharged != UNDEFINED && hvBattery.timeToHVBatteryFullyCharged > 0
? new DateTimeType(ZonedDateTime.now().plusMinutes(hvBattery.timeToHVBatteryFullyCharged)) ? new DateTimeType(
Instant.now().plus(hvBattery.timeToHVBatteryFullyCharged, ChronoUnit.MINUTES))
: UnDefType.UNDEF; : UnDefType.UNDEF;
} }
return UnDefType.NULL; return UnDefType.NULL;

View File

@ -12,7 +12,6 @@
*/ */
package org.openhab.binding.volvooncall.internal.wrapper; package org.openhab.binding.volvooncall.internal.wrapper;
import java.time.ZoneId;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@ -86,7 +85,6 @@ public class VehiclePositionWrapper {
public State getTimestamp() { public State getTimestamp() {
return Objects.requireNonNull(Optional.ofNullable(this.position).flatMap(pos -> pos.getTimestamp()) return Objects.requireNonNull(Optional.ofNullable(this.position).flatMap(pos -> pos.getTimestamp())
.map(dt -> (State) new DateTimeType(dt.withZoneSameInstant(ZoneId.systemDefault()))) .map(dt -> (State) new DateTimeType(dt)).orElse(UnDefType.NULL));
.orElse(UnDefType.NULL));
} }
} }