Simplify DateTimeType handling for Freecurrency

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-12-17 21:52:56 +01:00 committed by lsiepel
parent f0ba39f121
commit dd6c1f6d93
2 changed files with 5 additions and 6 deletions

View File

@ -16,7 +16,7 @@ import static org.openhab.binding.freecurrency.internal.FreecurrencyBindingConst
import static org.openhab.binding.freecurrency.internal.FreecurrencyBindingConstants.CHANNEL_TYPE_LAST_UPDATE; import static org.openhab.binding.freecurrency.internal.FreecurrencyBindingConstants.CHANNEL_TYPE_LAST_UPDATE;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.ZonedDateTime; import java.time.Instant;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.freecurrency.internal.config.FreecurrencyExhangeRateChannelConfig; import org.openhab.binding.freecurrency.internal.config.FreecurrencyExhangeRateChannelConfig;
@ -72,7 +72,7 @@ public class FreecurrencyHandler extends BaseThingHandler implements ExchangeRat
BigDecimal val = freecurrencyProvider.getExchangeRate(config.currency1, config.currency2); BigDecimal val = freecurrencyProvider.getExchangeRate(config.currency1, config.currency2);
updateState(channel.getUID(), val != null ? new DecimalType(val) : UnDefType.UNDEF); updateState(channel.getUID(), val != null ? new DecimalType(val) : UnDefType.UNDEF);
} else if (CHANNEL_TYPE_LAST_UPDATE.equals(channel.getChannelTypeUID())) { } else if (CHANNEL_TYPE_LAST_UPDATE.equals(channel.getChannelTypeUID())) {
ZonedDateTime lastUpdated = freecurrencyProvider.getLastUpdated(); Instant lastUpdated = freecurrencyProvider.getLastUpdated();
updateState(channel.getUID(), lastUpdated == null ? UnDefType.UNDEF : new DateTimeType(lastUpdated)); updateState(channel.getUID(), lastUpdated == null ? UnDefType.UNDEF : new DateTimeType(lastUpdated));
} }
} }

View File

@ -17,7 +17,6 @@ import java.math.MathContext;
import java.net.URI; import java.net.URI;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@ -80,7 +79,7 @@ public class FreecurrencyProvider implements CurrencyProvider, ConfigOptionProvi
private @Nullable ScheduledFuture<?> refreshJob; private @Nullable ScheduledFuture<?> refreshJob;
private Map<String, CurrencyInformation> currencies = Map.of(); private Map<String, CurrencyInformation> currencies = Map.of();
private Map<Unit<Currency>, BigDecimal> exchangeRates = Map.of(); private Map<Unit<Currency>, BigDecimal> exchangeRates = Map.of();
private @Nullable ZonedDateTime lastUpdated = null; private @Nullable Instant lastUpdated = null;
private Set<ExchangeRateListener> exchangeRateListeners = new HashSet<>(); private Set<ExchangeRateListener> exchangeRateListeners = new HashSet<>();
@Activate @Activate
@ -133,7 +132,7 @@ public class FreecurrencyProvider implements CurrencyProvider, ConfigOptionProvi
return rate2.divide(rate1, MathContext.DECIMAL128); return rate2.divide(rate1, MathContext.DECIMAL128);
} }
public @Nullable ZonedDateTime getLastUpdated() { public @Nullable Instant getLastUpdated() {
return lastUpdated; return lastUpdated;
} }
@ -181,7 +180,7 @@ public class FreecurrencyProvider implements CurrencyProvider, ConfigOptionProvi
}); });
exchangeRates = newExchangeRates; exchangeRates = newExchangeRates;
logger.debug("Retrieved exchange rates for {} currencies", newExchangeRates.size()); logger.debug("Retrieved exchange rates for {} currencies", newExchangeRates.size());
lastUpdated = ZonedDateTime.now(); lastUpdated = Instant.now();
// exchange rates are refreshed every day at midnight UTC // exchange rates are refreshed every day at midnight UTC
// we refresh one minute later to be sure we are not too early. // we refresh one minute later to be sure we are not too early.