[network] Fix last-seen channel being set to UNDEF at startup (#17971)

Signed-off-by: Leo Siepel <leosiepel@gmail.com>
This commit is contained in:
lsiepel 2024-12-24 15:37:16 +01:00 committed by GitHub
parent 7e9966989a
commit 50e3ca62c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -100,11 +100,11 @@ public class NetworkHandler extends BaseThingHandler
}); });
break; break;
case CHANNEL_LASTSEEN: case CHANNEL_LASTSEEN:
// We should not set the last seen state to UNDEF, it prevents restoreOnStartup from working
// For reference: https://github.com/openhab/openhab-addons/issues/17404
Instant lastSeen = presenceDetection.getLastSeen(); Instant lastSeen = presenceDetection.getLastSeen();
if (lastSeen != null) { if (lastSeen != null) {
updateState(CHANNEL_LASTSEEN, new DateTimeType(lastSeen)); updateState(CHANNEL_LASTSEEN, new DateTimeType(lastSeen));
} else {
updateState(CHANNEL_LASTSEEN, UnDefType.UNDEF);
} }
break; break;
default: default:
@ -144,9 +144,9 @@ public class NetworkHandler extends BaseThingHandler
Instant lastSeen = presenceDetection.getLastSeen(); Instant lastSeen = presenceDetection.getLastSeen();
if (value.isReachable() && lastSeen != null) { if (value.isReachable() && lastSeen != null) {
updateState(CHANNEL_LASTSEEN, new DateTimeType(lastSeen)); updateState(CHANNEL_LASTSEEN, new DateTimeType(lastSeen));
} else if (!value.isReachable() && lastSeen == null) {
updateState(CHANNEL_LASTSEEN, UnDefType.UNDEF);
} }
// We should not set the last seen state to UNDEF, it prevents restoreOnStartup from working
// For reference: https://github.com/openhab/openhab-addons/issues/17404
updateNetworkProperties(); updateNetworkProperties();
} }