Skip to content

Commit

Permalink
[evcc] Charge Plan Time is not converted to correct Timezone (openhab…
Browse files Browse the repository at this point in the history
…#17620)

Signed-off-by: Laith Budairi <laith.budairi@exalt.ps>
  • Loading branch information
LaithBudairi committed Oct 27, 2024
1 parent a6bb537 commit 34f5b27
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -66,6 +67,7 @@
@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;

Expand All @@ -79,8 +81,9 @@ public class EvccHandler extends BaseThingHandler {

Map<String, Triple<Boolean, Float, ZonedDateTime>> vehiclePlans = new HashMap<>();

public EvccHandler(Thing thing) {
public EvccHandler(Thing thing, TimeZoneProvider timeZoneProvider) {
super(thing);
this.timeZoneProvider = timeZoneProvider;
}

@Override
Expand Down Expand Up @@ -376,7 +379,7 @@ public void initialize() {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

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
Expand All @@ -37,6 +40,13 @@ public class EvccHandlerFactory extends BaseThingHandlerFactory {

private static final Set<ThingTypeUID> 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);
Expand All @@ -47,7 +57,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();

if (THING_TYPE_DEVICE.equals(thingTypeUID)) {
return new EvccHandler(thing);
return new EvccHandler(thing, timeZoneProvider);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
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;
Expand All @@ -40,10 +42,12 @@
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;
}

/**
Expand Down Expand Up @@ -144,8 +148,12 @@ public String setVehicleLimitSoC(String vehicleName, int limitSoC) throws EvccAp
}

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 {
Expand Down

0 comments on commit 34f5b27

Please sign in to comment.