From 68527c03f544fea1450a723c8975d18b1f533949 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Tue, 19 Nov 2024 20:38:08 +0100 Subject: [PATCH] Simplify DateTimeType handling for EVCC Signed-off-by: Jacob Laursen --- .../binding/evcc/internal/EvccHandler.java | 25 +++++++++++-------- .../binding/evcc/internal/api/EvccAPI.java | 7 +++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/EvccHandler.java b/bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/EvccHandler.java index 50f0c76e531..918459a2dca 100644 --- a/bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/EvccHandler.java +++ b/bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/EvccHandler.java @@ -14,8 +14,10 @@ package org.openhab.binding.evcc.internal; import static org.openhab.binding.evcc.internal.EvccBindingConstants.*; +import java.time.Instant; import java.time.ZonedDateTime; import java.time.format.DateTimeParseException; +import java.time.temporal.ChronoUnit; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -79,7 +81,7 @@ public class EvccHandler extends BaseThingHandler { private Set vehicleFeatureHeating = new HashSet(); private Set loadpointFeatureHeating = new HashSet(); - Map> vehiclePlans = new HashMap<>(); + Map> vehiclePlans = new HashMap<>(); public EvccHandler(Thing thing, TimeZoneProvider timeZoneProvider) { super(thing); @@ -284,7 +286,7 @@ public class EvccHandler extends BaseThingHandler { } } case CHANNEL_VEHICLE_PLAN_ENABLED, CHANNEL_HEATING_PLAN_ENABLED -> { - Triple planValues = vehiclePlans.get(vehicleName); + Triple planValues = vehiclePlans.get(vehicleName); if (command == OnOffType.ON) { evccAPI.setVehiclePlan(vehicleName, planValues.getMiddle().intValue(), planValues.getRight()); @@ -299,7 +301,7 @@ public class EvccHandler extends BaseThingHandler { } } case CHANNEL_VEHICLE_PLAN_SOC -> { - Triple planValues = vehiclePlans.get(vehicleName); + Triple planValues = vehiclePlans.get(vehicleName); if (command instanceof QuantityType qt) { vehiclePlans.put(vehicleName, new Triple<>(planValues.getLeft(), qt.toUnit(Units.PERCENT).floatValue(), planValues.getRight())); @@ -318,7 +320,7 @@ public class EvccHandler extends BaseThingHandler { } } case CHANNEL_HEATING_PLAN_TEMPERATURE -> { - Triple planValues = vehiclePlans.get(vehicleName); + Triple planValues = vehiclePlans.get(vehicleName); if (command instanceof QuantityType qt) { vehiclePlans.put(vehicleName, new Triple<>(planValues.getLeft(), qt.toUnit(SIUnits.CELSIUS).floatValue(), planValues.getRight())); @@ -337,14 +339,14 @@ public class EvccHandler extends BaseThingHandler { } } case CHANNEL_VEHICLE_PLAN_TIME, CHANNEL_HEATING_PLAN_TIME -> { - Triple planValues = vehiclePlans.get(vehicleName); + Triple planValues = vehiclePlans.get(vehicleName); if (command instanceof DateTimeType dtt) { - vehiclePlans.put(vehicleName, new Triple<>(planValues.getLeft(), planValues.getMiddle(), - dtt.getZonedDateTime())); + vehiclePlans.put(vehicleName, + new Triple<>(planValues.getLeft(), planValues.getMiddle(), dtt.getInstant())); if (planValues.getLeft()) { try { evccAPI.setVehiclePlan(vehicleName, planValues.getMiddle().intValue(), - dtt.getZonedDateTime()); + dtt.getInstant()); } catch (DateTimeParseException e) { logger.debug("Failed to set vehicle plan time: ", e); } @@ -967,15 +969,16 @@ public class EvccHandler extends BaseThingHandler { plan = vehicle.getPlan(); } if (plan == null && vehiclePlans.get(vehicleName) == null) { - vehiclePlans.put(vehicleName, new Triple<>(false, 100f, ZonedDateTime.now().plusHours(12))); + vehiclePlans.put(vehicleName, new Triple<>(false, 100f, Instant.now().plus(12, ChronoUnit.HOURS))); } else if (plan != null) { - vehiclePlans.put(vehicleName, new Triple<>(true, plan.getSoC(), ZonedDateTime.parse(plan.getTime()))); + vehiclePlans.put(vehicleName, + new Triple<>(true, plan.getSoC(), ZonedDateTime.parse(plan.getTime()).toInstant())); } updateVehiclePlanChannel(uid, vehicleName, channelGroup, isHeating); } private void updateVehiclePlanChannel(ThingUID uid, String vehicleName, String channelGroup, boolean isHeating) { - Triple planValues = vehiclePlans.get(vehicleName); + Triple planValues = vehiclePlans.get(vehicleName); if (isHeating) { ChannelUID channel = new ChannelUID(uid, channelGroup, CHANNEL_HEATING_PLAN_ENABLED); diff --git a/bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/api/EvccAPI.java b/bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/api/EvccAPI.java index d5d80b076ac..6928822bf2a 100644 --- a/bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/api/EvccAPI.java +++ b/bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/api/EvccAPI.java @@ -16,7 +16,7 @@ import static org.openhab.binding.evcc.internal.EvccBindingConstants.EVCC_REST_A import static org.openhab.binding.evcc.internal.EvccBindingConstants.LONG_CONNECTION_TIMEOUT_MILLISEC; import java.io.IOException; -import java.time.ZoneId; +import java.time.Instant; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; @@ -147,9 +147,8 @@ public class EvccAPI { return httpRequest(this.host + EVCC_REST_API + "vehicles/" + vehicleName + "/limitsoc/" + limitSoC, "POST"); } - public String setVehiclePlan(String vehicleName, int planSoC, ZonedDateTime planTime) throws EvccApiException { - ZoneId zoneId = timeZoneProvider.getTimeZone(); - ZonedDateTime adjustedTime = planTime.withZoneSameInstant(zoneId); + public String setVehiclePlan(String vehicleName, int planSoC, Instant planTime) throws EvccApiException { + ZonedDateTime adjustedTime = planTime.atZone(timeZoneProvider.getTimeZone()); String formattedTime = adjustedTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); return httpRequest( this.host + EVCC_REST_API + "vehicles/" + vehicleName + "/plan/soc/" + planSoC + "/" + formattedTime,