Skip to content

Commit 9872ca7

Browse files
authored
[energidataservice] Provide work-around for currency issues (openhab#16085)
* Provide work-around for currency issues * Verify unit before trying to instantiate QuantityType Signed-off-by: Jacob Laursen <[email protected]>
1 parent fe2d521 commit 9872ca7

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

bundles/org.openhab.binding.energidataservice/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ This has the following advantages:
6868
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.
6969
See [Electricity Tax](#electricity-tax) for further information.
7070

71+
#### Currencies
72+
73+
There are some existing limitations related to currency support.
74+
While the binding attempts to update channels in the correct currency, such attempts may face rejection.
75+
In such cases, the binding will resort to omitting the currency unit.
76+
While this ensures correct prices, it's important to note that the currency information may be incorrect in these instances.
77+
7178
#### Value-Added Tax
7279

7380
VAT is not included in any of the prices.

bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java

+27-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.util.concurrent.ScheduledFuture;
3434
import java.util.concurrent.TimeUnit;
3535

36+
import javax.measure.Unit;
37+
3638
import org.eclipse.jdt.annotation.NonNullByDefault;
3739
import org.eclipse.jdt.annotation.Nullable;
3840
import org.eclipse.jetty.client.HttpClient;
@@ -56,9 +58,10 @@
5658
import org.openhab.binding.energidataservice.internal.retry.RetryPolicyFactory;
5759
import org.openhab.binding.energidataservice.internal.retry.RetryStrategy;
5860
import org.openhab.core.i18n.TimeZoneProvider;
59-
import org.openhab.core.library.dimension.EnergyPrice;
61+
import org.openhab.core.library.types.DecimalType;
6062
import org.openhab.core.library.types.QuantityType;
6163
import org.openhab.core.library.types.StringType;
64+
import org.openhab.core.library.unit.CurrencyUnits;
6265
import org.openhab.core.thing.Channel;
6366
import org.openhab.core.thing.ChannelUID;
6467
import org.openhab.core.thing.Thing;
@@ -68,6 +71,7 @@
6871
import org.openhab.core.thing.binding.ThingHandlerService;
6972
import org.openhab.core.types.Command;
7073
import org.openhab.core.types.RefreshType;
74+
import org.openhab.core.types.State;
7175
import org.openhab.core.types.TimeSeries;
7276
import org.openhab.core.types.UnDefType;
7377
import org.slf4j.Logger;
@@ -321,19 +325,36 @@ private void updateCurrentSpotPrice() {
321325
return;
322326
}
323327
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());
326329
}
327330

328331
private void updateCurrentTariff(String channelId, @Nullable BigDecimal tariff) {
329332
if (!isLinked(channelId)) {
330333
return;
331334
}
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);
333340
}
334341

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+
}
337358
}
338359

339360
private void updateHourlyPrices() {

0 commit comments

Comments
 (0)