|
33 | 33 | import java.util.concurrent.ScheduledFuture;
|
34 | 34 | import java.util.concurrent.TimeUnit;
|
35 | 35 |
|
| 36 | +import javax.measure.Unit; |
| 37 | + |
36 | 38 | import org.eclipse.jdt.annotation.NonNullByDefault;
|
37 | 39 | import org.eclipse.jdt.annotation.Nullable;
|
38 | 40 | import org.eclipse.jetty.client.HttpClient;
|
|
56 | 58 | import org.openhab.binding.energidataservice.internal.retry.RetryPolicyFactory;
|
57 | 59 | import org.openhab.binding.energidataservice.internal.retry.RetryStrategy;
|
58 | 60 | import org.openhab.core.i18n.TimeZoneProvider;
|
59 |
| -import org.openhab.core.library.dimension.EnergyPrice; |
| 61 | +import org.openhab.core.library.types.DecimalType; |
60 | 62 | import org.openhab.core.library.types.QuantityType;
|
61 | 63 | import org.openhab.core.library.types.StringType;
|
| 64 | +import org.openhab.core.library.unit.CurrencyUnits; |
62 | 65 | import org.openhab.core.thing.Channel;
|
63 | 66 | import org.openhab.core.thing.ChannelUID;
|
64 | 67 | import org.openhab.core.thing.Thing;
|
|
68 | 71 | import org.openhab.core.thing.binding.ThingHandlerService;
|
69 | 72 | import org.openhab.core.types.Command;
|
70 | 73 | import org.openhab.core.types.RefreshType;
|
| 74 | +import org.openhab.core.types.State; |
71 | 75 | import org.openhab.core.types.TimeSeries;
|
72 | 76 | import org.openhab.core.types.UnDefType;
|
73 | 77 | import org.slf4j.Logger;
|
@@ -321,19 +325,36 @@ private void updateCurrentSpotPrice() {
|
321 | 325 | return;
|
322 | 326 | }
|
323 | 327 | BigDecimal spotPrice = cacheManager.getSpotPrice();
|
324 |
| - updateState(CHANNEL_SPOT_PRICE, |
325 |
| - spotPrice != null ? getEnergyPrice(spotPrice, config.getCurrency()) : UnDefType.UNDEF); |
| 328 | + updatePriceState(CHANNEL_SPOT_PRICE, spotPrice, config.getCurrency()); |
326 | 329 | }
|
327 | 330 |
|
328 | 331 | private void updateCurrentTariff(String channelId, @Nullable BigDecimal tariff) {
|
329 | 332 | if (!isLinked(channelId)) {
|
330 | 333 | return;
|
331 | 334 | }
|
332 |
| - updateState(channelId, tariff != null ? getEnergyPrice(tariff, CURRENCY_DKK) : UnDefType.UNDEF); |
| 335 | + updatePriceState(channelId, tariff, CURRENCY_DKK); |
| 336 | + } |
| 337 | + |
| 338 | + private void updatePriceState(String channelID, @Nullable BigDecimal price, Currency currency) { |
| 339 | + updateState(channelID, price != null ? getEnergyPrice(price, currency) : UnDefType.UNDEF); |
333 | 340 | }
|
334 | 341 |
|
335 |
| - private QuantityType<EnergyPrice> getEnergyPrice(BigDecimal price, Currency currency) { |
336 |
| - return new QuantityType<>(price + " " + currency.getSymbol() + "/kWh"); |
| 342 | + private State getEnergyPrice(BigDecimal price, Currency currency) { |
| 343 | + Unit<?> unit = CurrencyUnits.getInstance().getUnit(currency.getCurrencyCode()); |
| 344 | + if (unit == null) { |
| 345 | + logger.trace("Currency {} is unknown, falling back to DecimalType", currency.getCurrencyCode()); |
| 346 | + return new DecimalType(price); |
| 347 | + } |
| 348 | + try { |
| 349 | + String currencyUnit = unit.getSymbol(); |
| 350 | + if (currencyUnit == null) { |
| 351 | + currencyUnit = unit.getName(); |
| 352 | + } |
| 353 | + return new QuantityType<>(price + " " + currencyUnit + "/kWh"); |
| 354 | + } catch (IllegalArgumentException e) { |
| 355 | + logger.debug("Unable to create QuantityType, falling back to DecimalType", e); |
| 356 | + return new DecimalType(price); |
| 357 | + } |
337 | 358 | }
|
338 | 359 |
|
339 | 360 | private void updateHourlyPrices() {
|
|
0 commit comments