Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[evcc] Fix charge plan time channel not using correct timezone #17640

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Laith-Budairi - out of curiosity, does the API support UTC time, e.g. 2024-10-24T05:30:00.636Z? In this case, the fix could be as simple as:

String formattedTime = planTime.toInstant().toString();

And there would be no need to use TimeZoneProvider.

return httpRequest(
this.host + EVCC_REST_API + "vehicles/" + vehicleName + "/plan/soc/" + planSoC + "/" + formattedTime,
"POST");
}

public String removeVehiclePlan(String vehicleName) throws EvccApiException {
Expand Down