import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
+import javax.measure.Unit;
+
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.energidataservice.internal.retry.RetryPolicyFactory;
import org.openhab.binding.energidataservice.internal.retry.RetryStrategy;
import org.openhab.core.i18n.TimeZoneProvider;
-import org.openhab.core.library.dimension.EnergyPrice;
+import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
+import org.openhab.core.library.unit.CurrencyUnits;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
+import org.openhab.core.types.State;
import org.openhab.core.types.TimeSeries;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
return;
}
BigDecimal spotPrice = cacheManager.getSpotPrice();
- updateState(CHANNEL_SPOT_PRICE,
- spotPrice != null ? getEnergyPrice(spotPrice, config.getCurrency()) : UnDefType.UNDEF);
+ updatePriceState(CHANNEL_SPOT_PRICE, spotPrice, config.getCurrency());
}
private void updateCurrentTariff(String channelId, @Nullable BigDecimal tariff) {
if (!isLinked(channelId)) {
return;
}
- updateState(channelId, tariff != null ? getEnergyPrice(tariff, CURRENCY_DKK) : UnDefType.UNDEF);
+ updatePriceState(channelId, tariff, CURRENCY_DKK);
+ }
+
+ private void updatePriceState(String channelID, @Nullable BigDecimal price, Currency currency) {
+ updateState(channelID, price != null ? getEnergyPrice(price, currency) : UnDefType.UNDEF);
}
- private QuantityType<EnergyPrice> getEnergyPrice(BigDecimal price, Currency currency) {
- return new QuantityType<>(price + " " + currency.getSymbol() + "/kWh");
+ private State getEnergyPrice(BigDecimal price, Currency currency) {
+ Unit<?> unit = CurrencyUnits.getInstance().getUnit(currency.getCurrencyCode());
+ if (unit == null) {
+ logger.trace("Currency {} is unknown, falling back to DecimalType", currency.getCurrencyCode());
+ return new DecimalType(price);
+ }
+ try {
+ String currencyUnit = unit.getSymbol();
+ if (currencyUnit == null) {
+ currencyUnit = unit.getName();
+ }
+ return new QuantityType<>(price + " " + currencyUnit + "/kWh");
+ } catch (IllegalArgumentException e) {
+ logger.debug("Unable to create QuantityType, falling back to DecimalType", e);
+ return new DecimalType(price);
+ }
}
private void updateHourlyPrices() {