Simplify DateTimeType handling for JuiceNet

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-11-09 23:17:08 +01:00 committed by lsiepel
parent 6e2eb70950
commit c17b5f9ea8
2 changed files with 8 additions and 20 deletions

View File

@ -20,7 +20,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.juicenet.internal.handler.JuiceNetBridgeHandler;
import org.openhab.binding.juicenet.internal.handler.JuiceNetDeviceHandler;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing;
@ -44,13 +43,10 @@ public class JuiceNetHandlerFactory extends BaseThingHandlerFactory {
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(BRIDGE_THING_TYPE, DEVICE_THING_TYPE);
private final HttpClientFactory httpClientFactory;
private final TimeZoneProvider timeZoneProvider;
@Activate
public JuiceNetHandlerFactory(@Reference HttpClientFactory httpClientFactory,
@Reference TimeZoneProvider timeZoneProvider) {
public JuiceNetHandlerFactory(@Reference HttpClientFactory httpClientFactory) {
this.httpClientFactory = httpClientFactory;
this.timeZoneProvider = timeZoneProvider;
}
@Override
@ -65,7 +61,7 @@ public class JuiceNetHandlerFactory extends BaseThingHandlerFactory {
if (thingTypeUID.equals(BRIDGE_THING_TYPE)) {
return new JuiceNetBridgeHandler((Bridge) thing, httpClientFactory.getCommonHttpClient());
} else if (thingTypeUID.equals(DEVICE_THING_TYPE)) {
return new JuiceNetDeviceHandler(thing, timeZoneProvider);
return new JuiceNetDeviceHandler(thing);
}
return null;

View File

@ -15,6 +15,7 @@ package org.openhab.binding.juicenet.internal.handler;
import static org.openhab.binding.juicenet.internal.JuiceNetBindingConstants.*;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.util.Objects;
@ -29,7 +30,6 @@ import org.openhab.binding.juicenet.internal.api.dto.JuiceNetApiDeviceStatus;
import org.openhab.binding.juicenet.internal.api.dto.JuiceNetApiInfo;
import org.openhab.binding.juicenet.internal.api.dto.JuiceNetApiTouSchedule;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
@ -61,8 +61,6 @@ public class JuiceNetDeviceHandler extends BaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(JuiceNetDeviceHandler.class);
private final TimeZoneProvider timeZoneProvider;
// properties
private String name = "";
@ -75,10 +73,8 @@ public class JuiceNetDeviceHandler extends BaseThingHandler {
JuiceNetApiTouSchedule deviceTouSchedule = new JuiceNetApiTouSchedule();
JuiceNetApiCar deviceCar = new JuiceNetApiCar();
public JuiceNetDeviceHandler(Thing thing, TimeZoneProvider timeZoneProvider) {
public JuiceNetDeviceHandler(Thing thing) {
super(thing);
this.timeZoneProvider = timeZoneProvider;
}
public void setNameAndToken(String name, String token) {
@ -219,7 +215,7 @@ public class JuiceNetDeviceHandler extends BaseThingHandler {
return;
}
ZonedDateTime datetime = ((DateTimeType) command).getZonedDateTime();
ZonedDateTime datetime = ((DateTimeType) command).getZonedDateTime(ZoneId.systemDefault());
Long targetTime = datetime.toEpochSecond() + datetime.get(ChronoField.OFFSET_SECONDS);
logger.debug("DateTime: {} - {}", datetime.toString(), targetTime);
@ -314,10 +310,6 @@ public class JuiceNetDeviceHandler extends BaseThingHandler {
}
}
private ZonedDateTime toZonedDateTime(long localEpochSeconds) {
return Instant.ofEpochSecond(localEpochSeconds).atZone(timeZoneProvider.getTimeZone());
}
private void refreshStatusChannels() {
updateState(CHANNEL_STATE, new StringType(deviceStatus.state));
@ -332,9 +324,9 @@ public class JuiceNetDeviceHandler extends BaseThingHandler {
updateState(CHANNEL_MESSAGE, new StringType(deviceStatus.message));
updateState(CHANNEL_OVERRIDE, OnOffType.from(deviceStatus.showOverride));
updateState(CHANNEL_CHARGING_TIME_LEFT, new QuantityType<>(deviceStatus.chargingTimeLeft, Units.SECOND));
updateState(CHANNEL_PLUG_UNPLUG_TIME, new DateTimeType(toZonedDateTime(deviceStatus.plugUnplugTime)));
updateState(CHANNEL_TARGET_TIME, new DateTimeType(toZonedDateTime(deviceStatus.targetTime)));
updateState(CHANNEL_UNIT_TIME, new DateTimeType(toZonedDateTime(deviceStatus.utcTime)));
updateState(CHANNEL_PLUG_UNPLUG_TIME, new DateTimeType(Instant.ofEpochSecond(deviceStatus.plugUnplugTime)));
updateState(CHANNEL_TARGET_TIME, new DateTimeType(Instant.ofEpochSecond(deviceStatus.targetTime)));
updateState(CHANNEL_UNIT_TIME, new DateTimeType(Instant.ofEpochSecond(deviceStatus.utcTime)));
updateState(CHANNEL_TEMPERATURE, new QuantityType<>(deviceStatus.temperature, SIUnits.CELSIUS));
updateState(CHANNEL_CURRENT_LIMIT, new QuantityType<>(deviceStatus.charging.ampsLimit, Units.AMPERE));
updateState(CHANNEL_CURRENT, new QuantityType<>(deviceStatus.charging.ampsCurrent, Units.AMPERE));