Simplify DateTimeType handling for FreeboxOS

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-25 22:21:31 +01:00 committed by Ciprian Pascu
parent da51e449a0
commit 87521a3bec
4 changed files with 8 additions and 14 deletions

View File

@ -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));
}

View File

@ -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());

View File

@ -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> 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();

View File

@ -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> 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));
}