From f0ba39f1217e11eb937ba6fe212ec229a5c7b935 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Mon, 25 Nov 2024 22:21:31 +0100 Subject: [PATCH] Simplify DateTimeType handling for FreeboxOS Signed-off-by: Jacob Laursen --- .../freeboxos/internal/handler/ApiConsumerHandler.java | 3 ++- .../freeboxos/internal/handler/FreeplugHandler.java | 4 ++-- .../freeboxos/internal/handler/KeyfobHandler.java | 10 +++------- .../binding/freeboxos/internal/handler/PirHandler.java | 5 +---- 4 files changed, 8 insertions(+), 14 deletions(-) diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/ApiConsumerHandler.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/ApiConsumerHandler.java index e3a4034249f..928e56d7707 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/ApiConsumerHandler.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/ApiConsumerHandler.java @@ -13,6 +13,7 @@ package org.openhab.binding.freeboxos.internal.handler; import java.math.BigDecimal; +import java.time.Instant; import java.time.ZonedDateTime; import java.util.HashMap; import java.util.Hashtable; @@ -288,7 +289,7 @@ public abstract class ApiConsumerHandler extends BaseThingHandler implements Api } } - protected void updateChannelDateTimeState(String channelId, @Nullable ZonedDateTime timestamp) { + protected void updateChannelDateTimeState(String channelId, @Nullable Instant timestamp) { updateIfActive(channelId, timestamp == null ? UnDefType.NULL : new DateTimeType(timestamp)); } diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeplugHandler.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeplugHandler.java index 6efc5485be6..cc8a59d6fed 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeplugHandler.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/FreeplugHandler.java @@ -14,7 +14,7 @@ package org.openhab.binding.freeboxos.internal.handler; import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*; -import java.time.ZonedDateTime; +import java.time.Instant; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -84,7 +84,7 @@ public class FreeplugHandler extends ApiConsumerHandler { "internalPoll is not possible because MAC address is undefined for the thing " + thing.getUID()); } getManager(FreeplugManager.class).getPlug(mac).ifPresent(plug -> { - updateChannelDateTimeState(LAST_SEEN, ZonedDateTime.now().minusSeconds(plug.inactive())); + updateChannelDateTimeState(LAST_SEEN, Instant.now().minusSeconds(plug.inactive())); updateChannelString(LINE_STATUS, plug.ethPortStatus()); updateChannelOnOff(REACHABLE, plug.hasNetwork()); diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/KeyfobHandler.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/KeyfobHandler.java index 524330a22da..7105dbbf9ce 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/KeyfobHandler.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/KeyfobHandler.java @@ -15,8 +15,6 @@ package org.openhab.binding.freeboxos.internal.handler; import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*; import java.time.Instant; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; import java.util.Objects; import java.util.Optional; @@ -50,11 +48,9 @@ public class KeyfobHandler extends HomeNodeHandler { @Override protected State getChannelState(String channelId, EndpointState state, Optional endPoint) { if (channelId.startsWith(KEYFOB_PUSHED)) { - return Objects.requireNonNull(endPoint.map(ep -> ep - .getLastChange().map( - change -> (State) (KEYFOB_PUSHED.equals(channelId) ? new DecimalType(change.value()) - : new DateTimeType(ZonedDateTime - .ofInstant(Instant.ofEpochSecond(change.timestamp()), ZoneOffset.UTC)))) + return Objects.requireNonNull(endPoint.map(ep -> ep.getLastChange() + .map(change -> (State) (KEYFOB_PUSHED.equals(channelId) ? new DecimalType(change.value()) + : new DateTimeType(Instant.ofEpochSecond(change.timestamp())))) .orElse(UnDefType.UNDEF)).orElse(UnDefType.UNDEF)); } String value = state.value(); diff --git a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/PirHandler.java b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/PirHandler.java index 191189e4f94..461dde03de9 100644 --- a/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/PirHandler.java +++ b/bundles/org.openhab.binding.freeboxos/src/main/java/org/openhab/binding/freeboxos/internal/handler/PirHandler.java @@ -15,8 +15,6 @@ package org.openhab.binding.freeboxos.internal.handler; import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*; import java.time.Instant; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; import java.util.Objects; import java.util.Optional; @@ -48,8 +46,7 @@ public class PirHandler extends HomeNodeHandler { protected State getChannelState(String channelId, EndpointState state, Optional endPoint) { if (PIR_TAMPER_UPDATE.equals(channelId) || PIR_TRIGGER_UPDATE.equals(channelId)) { return Objects.requireNonNull(endPoint.map(ep -> ep.getLastChange() - .map(change -> (State) new DateTimeType( - ZonedDateTime.ofInstant(Instant.ofEpochSecond(change.timestamp()), ZoneOffset.UTC))) + .map(change -> (State) new DateTimeType(Instant.ofEpochSecond(change.timestamp()))) .orElse(UnDefType.UNDEF)).orElse(UnDefType.UNDEF)); }