Simplify DateTimeType handling for EVCC

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Jacob Laursen 2024-11-19 20:38:08 +01:00 committed by Ciprian Pascu
parent ca6854581b
commit 31d4469e5d
2 changed files with 17 additions and 15 deletions

View File

@ -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<String> vehicleFeatureHeating = new HashSet<String>();
private Set<String> loadpointFeatureHeating = new HashSet<String>();
Map<String, Triple<Boolean, Float, ZonedDateTime>> vehiclePlans = new HashMap<>();
Map<String, Triple<Boolean, Float, Instant>> 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<Boolean, Float, ZonedDateTime> planValues = vehiclePlans.get(vehicleName);
Triple<Boolean, Float, Instant> 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<Boolean, Float, ZonedDateTime> planValues = vehiclePlans.get(vehicleName);
Triple<Boolean, Float, Instant> 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<Boolean, Float, ZonedDateTime> planValues = vehiclePlans.get(vehicleName);
Triple<Boolean, Float, Instant> 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<Boolean, Float, ZonedDateTime> planValues = vehiclePlans.get(vehicleName);
Triple<Boolean, Float, Instant> 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<Boolean, Float, ZonedDateTime> planValues = vehiclePlans.get(vehicleName);
Triple<Boolean, Float, Instant> planValues = vehiclePlans.get(vehicleName);
if (isHeating) {
ChannelUID channel = new ChannelUID(uid, channelGroup, CHANNEL_HEATING_PLAN_ENABLED);

View File

@ -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,