From 2fd2e5175f7f1672b1d3c1112a97ecddd9de24e3 Mon Sep 17 00:00:00 2001 From: Sami Salonen Date: Sun, 18 Sep 2022 19:20:49 +0300 Subject: [PATCH] [mqtt.homeassistant] Stable jsondb discovery result (#13401) * [mqtt.homeassistant] Stable jsondb serialization for discovery results Similar to openhab/openhab-core#2436, we want to have consistent ordering of data in JSONDB. This is fixing the jsondb order for mqtt.homeassistant discovery results, specifically, the "topics" property. * [mqtt.homeassistant] order using full topic string, not by subcomponent Signed-off-by: Sami Salonen --- .../discovery/HomeAssistantDiscovery.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/discovery/HomeAssistantDiscovery.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/discovery/HomeAssistantDiscovery.java index 7cef2b954..b8b9ae597 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/discovery/HomeAssistantDiscovery.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/main/java/org/openhab/binding/mqtt/homeassistant/internal/discovery/HomeAssistantDiscovery.java @@ -16,9 +16,11 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; @@ -166,8 +168,21 @@ public class HomeAssistantDiscovery extends AbstractMQTTDiscovery { thingIDPerTopic.put(topic, thingUID); // We need to keep track of already found component topics for a specific thing - Set components = componentsPerThingID.computeIfAbsent(thingID, key -> ConcurrentHashMap.newKeySet()); - components.add(haID); + final List components; + { + Set componentsUnordered = componentsPerThingID.computeIfAbsent(thingID, + key -> ConcurrentHashMap.newKeySet()); + + // Invariant. For compiler, computeIfAbsent above returns always + // non-null + Objects.requireNonNull(componentsUnordered); + componentsUnordered.add(haID); + + components = componentsUnordered.stream().collect(Collectors.toList()); + // We sort the components for consistent jsondb serialization order of 'topics' thing property + // Sorting key is HaID::toString, i.e. using the full topic string + components.sort(Comparator.comparing(HaID::toString)); + } final String componentNames = components.stream().map(id -> id.component) .map(c -> HA_COMP_TO_NAME.getOrDefault(c, c)).collect(Collectors.joining(", "));