From f13c5c5072bbc51ba856390c4c0410c24e6d791f Mon Sep 17 00:00:00 2001 From: Laith-Budairi <48382142+Laith-Budairi@users.noreply.github.com> Date: Mon, 28 Oct 2024 11:21:03 +0200 Subject: [PATCH] [evcc] Charge Plan Time is not converted to correct Timezone (#17620) (#17640) Signed-off-by: Laith Budairi Signed-off-by: Ciprian Pascu --- .../openhab/binding/evcc/internal/EvccHandler.java | 7 +++++-- .../binding/evcc/internal/EvccHandlerFactory.java | 12 +++++++++++- .../openhab/binding/evcc/internal/api/EvccAPI.java | 14 +++++++++++--- 3 files changed, 27 insertions(+), 6 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 a7fa9e55487..8c910403e2a 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 @@ -33,6 +33,7 @@ import org.openhab.binding.evcc.internal.api.dto.PV; import org.openhab.binding.evcc.internal.api.dto.Plan; import org.openhab.binding.evcc.internal.api.dto.Result; import org.openhab.binding.evcc.internal.api.dto.Vehicle; +import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.library.CoreItemFactory; import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DecimalType; @@ -66,6 +67,7 @@ import org.slf4j.LoggerFactory; @NonNullByDefault public class EvccHandler extends BaseThingHandler { private final Logger logger = LoggerFactory.getLogger(EvccHandler.class); + private final TimeZoneProvider timeZoneProvider; private @Nullable EvccAPI evccAPI; private @Nullable ScheduledFuture statePollingJob; @@ -79,8 +81,9 @@ public class EvccHandler extends BaseThingHandler { Map> vehiclePlans = new HashMap<>(); - public EvccHandler(Thing thing) { + public EvccHandler(Thing thing, TimeZoneProvider timeZoneProvider) { super(thing); + this.timeZoneProvider = timeZoneProvider; } @Override @@ -376,7 +379,7 @@ public class EvccHandler extends BaseThingHandler { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, "@text/offline.configuration-error.no-host"); } else { - this.evccAPI = new EvccAPI(url); + this.evccAPI = new EvccAPI(url, timeZoneProvider); logger.debug("Setting up refresh job ..."); statePollingJob = scheduler.scheduleWithFixedDelay(this::refresh, 0, config.refreshInterval, TimeUnit.SECONDS); diff --git a/bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/EvccHandlerFactory.java b/bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/EvccHandlerFactory.java index 1212f869fd4..a8df39e884f 100644 --- a/bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/EvccHandlerFactory.java +++ b/bundles/org.openhab.binding.evcc/src/main/java/org/openhab/binding/evcc/internal/EvccHandlerFactory.java @@ -18,12 +18,15 @@ import java.util.Set; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.binding.BaseThingHandlerFactory; import org.openhab.core.thing.binding.ThingHandler; import org.openhab.core.thing.binding.ThingHandlerFactory; +import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; /** * The {@link EvccHandlerFactory} is responsible for creating things and thing @@ -37,6 +40,13 @@ public class EvccHandlerFactory extends BaseThingHandlerFactory { private static final Set SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_DEVICE); + private final TimeZoneProvider timeZoneProvider; + + @Activate + public EvccHandlerFactory(final @Reference TimeZoneProvider timeZoneProvider) { + this.timeZoneProvider = timeZoneProvider; + } + @Override public boolean supportsThingType(ThingTypeUID thingTypeUID) { return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID); @@ -47,7 +57,7 @@ public class EvccHandlerFactory extends BaseThingHandlerFactory { ThingTypeUID thingTypeUID = thing.getThingTypeUID(); if (THING_TYPE_DEVICE.equals(thingTypeUID)) { - return new EvccHandler(thing); + return new EvccHandler(thing, timeZoneProvider); } return null; 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 afacdfe0be6..d5d80b076ac 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,12 +16,14 @@ 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.ZonedDateTime; import java.time.format.DateTimeFormatter; import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.binding.evcc.internal.api.dto.Result; import org.openhab.binding.evcc.internal.api.dto.Status; +import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.io.net.http.HttpUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,10 +42,12 @@ import com.google.gson.JsonSyntaxException; public class EvccAPI { private final Logger logger = LoggerFactory.getLogger(EvccAPI.class); private final Gson gson = new Gson(); + private final TimeZoneProvider timeZoneProvider; private String host; - public EvccAPI(String host) { + public EvccAPI(String host, TimeZoneProvider timeZoneProvider) { this.host = (host.endsWith("/") ? host.substring(0, host.length() - 1) : host); + this.timeZoneProvider = timeZoneProvider; } /** @@ -144,8 +148,12 @@ public class EvccAPI { } public String setVehiclePlan(String vehicleName, int planSoC, ZonedDateTime planTime) throws EvccApiException { - return httpRequest(this.host + EVCC_REST_API + "vehicles/" + vehicleName + "/plan/soc/" + planSoC + "/" - + planTime.toLocalDateTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + "Z", "POST"); + ZoneId zoneId = timeZoneProvider.getTimeZone(); + ZonedDateTime adjustedTime = planTime.withZoneSameInstant(zoneId); + String formattedTime = adjustedTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME); + return httpRequest( + this.host + EVCC_REST_API + "vehicles/" + vehicleName + "/plan/soc/" + planSoC + "/" + formattedTime, + "POST"); } public String removeVehiclePlan(String vehicleName) throws EvccApiException {