From 295df504c8da8cbefb869ffe0b13a1686bd9217e Mon Sep 17 00:00:00 2001 From: Sami Salonen Date: Sun, 18 Sep 2022 22:22:06 +0300 Subject: [PATCH] [mqtt.homeassistant] More readable label for discovered things (#13402) * [mqtt.openassistant] Better labels for discoverd things This PR introduces more simple label for things, instead of "My Sensor (Sensor, Sensor, Sensor, Sensor, Sensor, Switch)" we have simply "Me Sensor (5x Sensor, Switch)". Signed-off-by: Sami Salonen --- .../discovery/HomeAssistantDiscovery.java | 34 +++++++++++++++++-- .../HomeAssistantDiscoveryTests.java | 10 ++++++ 2 files changed, 42 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 b8b9ae597..3d90235cb 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 @@ -26,7 +26,10 @@ import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import java.util.function.Function; +import java.util.stream.Collector; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -128,6 +131,33 @@ public class HomeAssistantDiscovery extends AbstractMQTTDiscovery { return typeProvider.getThingTypeUIDs(); } + /** + * Summarize components such as {Switch, Switch, Sensor} into string "Sensor, 2x Switch" + * + * @param componentNames stream of component names + * @return summary string of component names and their counts + */ + static String getComponentNamesSummary(Stream componentNames) { + StringBuilder summary = new StringBuilder(); + Collector countingCollector = Collectors.counting(); + Map componentCounts = componentNames + .collect(Collectors.groupingBy(Function.identity(), countingCollector)); + componentCounts.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEach(entry -> { + String componentName = entry.getKey(); + long count = entry.getValue(); + if (summary.length() > 0) { + // not the first entry, so let's add the separating comma + summary.append(", "); + } + if (count > 1) { + summary.append(count); + summary.append("x "); + } + summary.append(componentName); + }); + return summary.toString(); + } + @Override public void receivedMessage(ThingUID connectionBridge, MqttBrokerConnection connection, String topic, byte[] payload) { @@ -184,8 +214,8 @@ public class HomeAssistantDiscovery extends AbstractMQTTDiscovery { 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(", ")); + final String componentNames = getComponentNamesSummary( + components.stream().map(id -> id.component).map(c -> HA_COMP_TO_NAME.getOrDefault(c, c))); final List topics = components.stream().map(HaID::toShortTopic).collect(Collectors.toList()); diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/discovery/HomeAssistantDiscoveryTests.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/discovery/HomeAssistantDiscoveryTests.java index d8614eaa3..3ac574196 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/discovery/HomeAssistantDiscoveryTests.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/discovery/HomeAssistantDiscoveryTests.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -54,6 +55,14 @@ public class HomeAssistantDiscoveryTests extends AbstractHomeAssistantTests { discovery = new TestHomeAssistantDiscovery(channelTypeProvider); } + @Test + public void testComponentNameSummary() { + assertThat( + HomeAssistantDiscovery.getComponentNamesSummary( + Stream.of("Sensor", "Switch", "Sensor", "Foobar", "Foobar", "Foobar")), // + is("3x Foobar, 2x Sensor, Switch")); + } + @Test public void testOneThingDiscovery() throws Exception { var discoveryListener = new LatchDiscoveryListener(); @@ -79,6 +88,7 @@ public class HomeAssistantDiscoveryTests extends AbstractHomeAssistantTests { assertThat(result.getProperties().get(Thing.PROPERTY_VENDOR), is("TuYa")); assertThat(result.getProperties().get(Thing.PROPERTY_FIRMWARE_VERSION), is("Zigbee2MQTT 1.18.2")); assertThat(result.getProperties().get(HandlerConfiguration.PROPERTY_BASETOPIC), is("homeassistant")); + assertThat(result.getLabel(), is("th1 (Climate Control, Switch)")); assertThat((List) result.getProperties().get(HandlerConfiguration.PROPERTY_TOPICS), hasItems( "climate/0x847127fffe11dd6a_climate_zigbee2mqtt", "switch/0x847127fffe11dd6a_auto_lock_zigbee2mqtt")); }