From 31c33561e89728ea9f8a4c4b5a6c352de5e68b60 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Tue, 16 Apr 2024 08:21:26 +0200 Subject: [PATCH] Fix tariff time series update during spot price unavailability (#16654) Signed-off-by: Jacob Laursen --- .../handler/EnergiDataServiceHandler.java | 65 +++++++------------ 1 file changed, 23 insertions(+), 42 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 e2a9bb70e1008..eac6f08be7bd7 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 @@ -25,7 +25,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Currency; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -496,53 +495,35 @@ private State getEnergyPrice(BigDecimal price, Currency currency) { } private void updateTimeSeries() { - TimeSeries spotPriceTimeSeries = new TimeSeries(REPLACE); - Map datahubTimeSeriesMap = new HashMap<>(); - Map datahubPreviousTariff = new HashMap<>(); + updatePriceTimeSeries(CHANNEL_SPOT_PRICE, cacheManager.getSpotPrices(), config.getCurrency(), false); + for (DatahubTariff datahubTariff : DatahubTariff.values()) { - datahubTimeSeriesMap.put(datahubTariff, new TimeSeries(REPLACE)); + String channelId = datahubTariff.getChannelId(); + updatePriceTimeSeries(channelId, cacheManager.getTariffs(datahubTariff), CURRENCY_DKK, true); } + } - Map spotPriceMap = cacheManager.getSpotPrices(); - List> spotPrices = spotPriceMap.entrySet().stream() - .sorted(Map.Entry.comparingByKey()).toList(); - for (Entry spotPrice : spotPrices) { - Instant hourStart = spotPrice.getKey(); - if (isLinked(CHANNEL_SPOT_PRICE)) { - spotPriceTimeSeries.add(hourStart, getEnergyPrice(spotPrice.getValue(), config.getCurrency())); - } - for (Map.Entry entry : datahubTimeSeriesMap.entrySet()) { - DatahubTariff datahubTariff = entry.getKey(); - String channelId = datahubTariff.getChannelId(); - if (!isLinked(channelId)) { - continue; - } - BigDecimal tariff = cacheManager.getTariff(datahubTariff, hourStart); - if (tariff != null) { - BigDecimal previousTariff = datahubPreviousTariff.get(datahubTariff); - if (previousTariff != null && tariff.equals(previousTariff)) { - // Skip redundant states. - continue; - } - TimeSeries timeSeries = entry.getValue(); - timeSeries.add(hourStart, getEnergyPrice(tariff, CURRENCY_DKK)); - datahubPreviousTariff.put(datahubTariff, tariff); - } - } - } - if (spotPriceTimeSeries.size() > 0) { - sendTimeSeries(CHANNEL_SPOT_PRICE, spotPriceTimeSeries); + private void updatePriceTimeSeries(String channelId, Map priceMap, Currency currency, + boolean deduplicate) { + if (!isLinked(channelId)) { + return; } - for (Map.Entry entry : datahubTimeSeriesMap.entrySet()) { - DatahubTariff datahubTariff = entry.getKey(); - String channelId = datahubTariff.getChannelId(); - if (!isLinked(channelId)) { + List> prices = priceMap.entrySet().stream().sorted(Map.Entry.comparingByKey()) + .toList(); + TimeSeries timeSeries = new TimeSeries(REPLACE); + BigDecimal previousTariff = null; + for (Entry price : prices) { + Instant hourStart = price.getKey(); + BigDecimal priceValue = price.getValue(); + if (deduplicate && priceValue.equals(previousTariff)) { + // Skip redundant states. continue; } - TimeSeries timeSeries = entry.getValue(); - if (timeSeries.size() > 0) { - sendTimeSeries(channelId, timeSeries); - } + timeSeries.add(hourStart, getEnergyPrice(priceValue, currency)); + previousTariff = priceValue; + } + if (timeSeries.size() > 0) { + sendTimeSeries(channelId, timeSeries); } }