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;
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<String> componentNames) {
+ StringBuilder summary = new StringBuilder();
+ Collector<String, ?, Long> countingCollector = Collectors.counting();
+ Map<String, Long> 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) {
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<String> topics = components.stream().map(HaID::toShortTopic).collect(Collectors.toList());
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;
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();
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<String>) result.getProperties().get(HandlerConfiguration.PROPERTY_TOPICS), hasItems(
"climate/0x847127fffe11dd6a_climate_zigbee2mqtt", "switch/0x847127fffe11dd6a_auto_lock_zigbee2mqtt"));
}