From f1b67eac20e863a70d218dda54c019db0020e474 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Tue, 19 Dec 2023 23:14:21 +0100 Subject: [PATCH 1/2] Provide work-around for currency issues Signed-off-by: Jacob Laursen --- .../README.md | 7 +++++++ .../handler/EnergiDataServiceHandler.java | 21 +++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.binding.energidataservice/README.md b/bundles/org.openhab.binding.energidataservice/README.md index a203dbabd1f19..f08aafc6565c0 100644 --- a/bundles/org.openhab.binding.energidataservice/README.md +++ b/bundles/org.openhab.binding.energidataservice/README.md @@ -68,6 +68,13 @@ This has the following advantages: If you want electricity tax included in your total price, please add either `electricity-tax` or `reduced-electricity-tax` to the group - depending on which one applies. See [Electricity Tax](#electricity-tax) for further information. +#### Currencies + +There are some existing limitations related to currency support. +While the binding attempts to update channels in the correct currency, such attempts may face rejection. +In such cases, the binding will resort to omitting the currency unit. +While this ensures correct prices, it's important to note that the currency information may be incorrect in these instances. + #### Value-Added Tax VAT is not included in any of the prices. diff --git a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java index 5f6ed20d78dbe..11b9f622f3262 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java +++ b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java @@ -56,7 +56,7 @@ 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.thing.Channel; @@ -68,6 +68,7 @@ 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; @@ -321,19 +322,27 @@ private void updateCurrentSpotPrice() { 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 QuantityType getEnergyPrice(BigDecimal price, Currency currency) { - return new QuantityType<>(price + " " + currency.getSymbol() + "/kWh"); + private void updatePriceState(String channelID, @Nullable BigDecimal price, Currency currency) { + updateState(channelID, price != null ? getEnergyPrice(price, currency) : UnDefType.UNDEF); + } + + private State getEnergyPrice(BigDecimal price, Currency currency) { + try { + return new QuantityType<>(price + " " + currency.getSymbol() + "/kWh"); + } catch (IllegalArgumentException e) { + logger.trace("Unable to create QuantityType, falling back to DecimalType", e); + return new DecimalType(price); + } } private void updateHourlyPrices() { From bc3a7d7e24bd81d5694c5ce2762696132df0549a Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Wed, 20 Dec 2023 10:57:23 +0100 Subject: [PATCH 2/2] Verify unit before trying to instantiate QuantityType Signed-off-by: Jacob Laursen --- .../handler/EnergiDataServiceHandler.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java index 11b9f622f3262..11b69e9532090 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java +++ b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java @@ -33,6 +33,8 @@ 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; @@ -59,6 +61,7 @@ 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; @@ -337,10 +340,19 @@ private void updatePriceState(String channelID, @Nullable BigDecimal price, Curr } 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 { - return new QuantityType<>(price + " " + currency.getSymbol() + "/kWh"); + String currencyUnit = unit.getSymbol(); + if (currencyUnit == null) { + currencyUnit = unit.getName(); + } + return new QuantityType<>(price + " " + currencyUnit + "/kWh"); } catch (IllegalArgumentException e) { - logger.trace("Unable to create QuantityType, falling back to DecimalType", e); + logger.debug("Unable to create QuantityType, falling back to DecimalType", e); return new DecimalType(price); } }