2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.homeassistant.internal;
15 import java.lang.ref.WeakReference;
16 import java.util.HashSet;
18 import java.util.concurrent.CompletableFuture;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
22 import java.util.stream.Collectors;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.mqtt.generic.AvailabilityTracker;
27 import org.openhab.binding.mqtt.generic.ChannelStateUpdateListener;
28 import org.openhab.binding.mqtt.generic.TransformationServiceProvider;
29 import org.openhab.binding.mqtt.generic.utils.FutureCollector;
30 import org.openhab.binding.mqtt.homeassistant.internal.component.AbstractComponent;
31 import org.openhab.binding.mqtt.homeassistant.internal.component.ComponentFactory;
32 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
33 import org.openhab.core.io.transport.mqtt.MqttMessageSubscriber;
34 import org.openhab.core.thing.ThingUID;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.gson.Gson;
41 * Responsible for subscribing to the HomeAssistant MQTT components wildcard topic, either
42 * in a time limited discovery mode or as a background discovery.
44 * @author David Graeff - Initial contribution
47 public class DiscoverComponents implements MqttMessageSubscriber {
48 private final Logger logger = LoggerFactory.getLogger(DiscoverComponents.class);
49 private final ThingUID thingUID;
50 private final ScheduledExecutorService scheduler;
51 private final ChannelStateUpdateListener updateListener;
52 private final AvailabilityTracker tracker;
53 private final TransformationServiceProvider transformationServiceProvider;
55 protected final CompletableFuture<@Nullable Void> discoverFinishedFuture = new CompletableFuture<>();
56 private final Gson gson;
58 private @Nullable ScheduledFuture<?> stopDiscoveryFuture;
59 private WeakReference<@Nullable MqttBrokerConnection> connectionRef = new WeakReference<>(null);
60 protected @Nullable ComponentDiscovered discoveredListener;
61 private int discoverTime;
62 private Set<String> topics = new HashSet<>();
65 * Implement this to get notified of new components
67 public static interface ComponentDiscovered {
68 void componentDiscovered(HaID homeAssistantTopicID, AbstractComponent<?> component);
72 * Create a new discovery object.
74 * @param thingUID The Thing UID to perform the discovery for.
75 * @param scheduler A scheduler for timeouts
76 * @param channelStateUpdateListener Channel update listener. Usually the handler.
78 public DiscoverComponents(ThingUID thingUID, ScheduledExecutorService scheduler,
79 ChannelStateUpdateListener channelStateUpdateListener, AvailabilityTracker tracker, Gson gson,
80 TransformationServiceProvider transformationServiceProvider) {
81 this.thingUID = thingUID;
82 this.scheduler = scheduler;
83 this.updateListener = channelStateUpdateListener;
85 this.tracker = tracker;
86 this.transformationServiceProvider = transformationServiceProvider;
90 public void processMessage(String topic, byte[] payload) {
91 if (!topic.endsWith("/config")) {
95 HaID haID = new HaID(topic);
96 String config = new String(payload);
97 AbstractComponent<?> component = null;
99 if (config.length() > 0) {
100 component = ComponentFactory.createComponent(thingUID, haID, config, updateListener, tracker, scheduler,
101 gson, transformationServiceProvider);
103 if (component != null) {
104 component.setConfigSeen();
106 logger.trace("Found HomeAssistant thing {} component {}", haID.objectID, haID.component);
107 if (discoveredListener != null) {
108 discoveredListener.componentDiscovered(haID, component);
111 logger.debug("Configuration of HomeAssistant thing {} invalid: {}", haID.objectID, config);
116 * Start a components discovery.
119 * We need to consider the case that the remote client is using node IDs
120 * and also the case that no node IDs are used.
123 * @param connection A MQTT broker connection
124 * @param discoverTime The time in milliseconds for the discovery to run. Can be 0 to disable the
126 * You need to call {@link #stopDiscovery()} at some
127 * point in that case.
128 * @param topicDescriptions Contains the object-id (=device id) and potentially a node-id as well.
129 * @param componentsDiscoveredListener Listener for results
130 * @return A future that completes normally after the given time in milliseconds or exceptionally on any error.
131 * Completes immediately if the timeout is disabled.
133 public CompletableFuture<@Nullable Void> startDiscovery(MqttBrokerConnection connection, int discoverTime,
134 Set<HaID> topicDescriptions, ComponentDiscovered componentsDiscoveredListener) {
135 this.topics = topicDescriptions.stream().map(id -> id.getTopic("config")).collect(Collectors.toSet());
136 this.discoverTime = discoverTime;
137 this.discoveredListener = componentsDiscoveredListener;
138 this.connectionRef = new WeakReference<>(connection);
140 // Subscribe to the wildcard topic and start receive MQTT retained topics
141 this.topics.parallelStream().map(t -> connection.subscribe(t, this)).collect(FutureCollector.allOf())
142 .thenRun(this::subscribeSuccess).exceptionally(this::subscribeFail);
144 return discoverFinishedFuture;
147 private void subscribeSuccess() {
148 final MqttBrokerConnection connection = connectionRef.get();
149 // Set up a scheduled future that will stop the discovery after the given time
150 if (connection != null && discoverTime > 0) {
151 this.stopDiscoveryFuture = scheduler.schedule(() -> {
152 this.stopDiscoveryFuture = null;
153 this.topics.parallelStream().forEach(t -> connection.unsubscribe(t, this));
154 this.discoveredListener = null;
155 discoverFinishedFuture.complete(null);
156 }, discoverTime, TimeUnit.MILLISECONDS);
158 // No timeout -> complete immediately
159 discoverFinishedFuture.complete(null);
163 private @Nullable Void subscribeFail(Throwable e) {
164 final ScheduledFuture<?> scheduledFuture = this.stopDiscoveryFuture;
165 if (scheduledFuture != null) { // Cancel timeout
166 scheduledFuture.cancel(false);
167 this.stopDiscoveryFuture = null;
169 this.discoveredListener = null;
170 final MqttBrokerConnection connection = connectionRef.get();
171 if (connection != null) {
172 this.topics.parallelStream().forEach(t -> connection.unsubscribe(t, this));
173 connectionRef.clear();
175 discoverFinishedFuture.completeExceptionally(e);
180 * Stops an ongoing discovery or do nothing if no discovery is running.
182 public void stopDiscovery() {
183 subscribeFail(new Throwable("Stopped"));