Skip to content

Commit

Permalink
[mqtt.homeassistant] Sort channels before changing thing
Browse files Browse the repository at this point in the history
Signed-off-by: Sami Salonen <[email protected]>
  • Loading branch information
ssalonen committed Oct 8, 2022
1 parent 1273d7d commit b2e6c55
Showing 1 changed file with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.mqtt.homeassistant.internal.handler;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -81,6 +82,8 @@
public class HomeAssistantThingHandler extends AbstractMQTTThingHandler
implements ComponentDiscovered, Consumer<List<AbstractComponent<?>>> {
public static final String AVAILABILITY_CHANNEL = "availability";
private static final Comparator<Channel> CHANNEL_COMPARATOR_BY_UID = Comparator
.comparing(channel -> channel.getUID().toString());;

private final Logger logger = LoggerFactory.getLogger(HomeAssistantThingHandler.class);

Expand Down Expand Up @@ -297,7 +300,9 @@ public void accept(List<AbstractComponent<?>> discoveredComponentsList) {
removeJustRediscoveredChannels(discoveredChannels);
}

// Add newly discovered channels
// Add newly discovered channels. We sort the channels
// for (mostly) consistent jsondb serialization
discoveredChannels.sort(CHANNEL_COMPARATOR_BY_UID);
ThingHelper.addChannelsToThing(thing, discoveredChannels);
}
updateThingType();
Expand All @@ -312,6 +317,8 @@ private void removeJustRediscoveredChannels(List<Channel> discoveredChannels) {
.filter(existingChannel -> !newChannelUIDs.contains(existingChannel.getUID()))
.collect(Collectors.toList());
if (existingChannelsWithNewlyDiscoveredChannelsRemoved.size() < mutableChannels.size()) {
// We sort the channels for (mostly) consistent jsondb serialization
existingChannelsWithNewlyDiscoveredChannelsRemoved.sort(CHANNEL_COMPARATOR_BY_UID);
updateThingChannels(existingChannelsWithNewlyDiscoveredChannelsRemoved);
}
}
Expand Down

0 comments on commit b2e6c55

Please sign in to comment.