import static org.openhab.binding.chromecast.internal.ChromecastBindingConstants.*;
-import java.util.HashMap;
+import java.util.Dictionary;
import java.util.Map;
import java.util.Set;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
+import org.openhab.core.config.discovery.DiscoveryService;
import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Modified;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * The {@link ChromecastDiscoveryParticipant} is responsible for discovering Chromecast devices through UPnP.
+ * The {@link ChromecastDiscoveryParticipant} is responsible for discovering Chromecast devices through mDNS.
*
* @author Kai Kreuzer - Initial contribution
* @author Daniel Walters - Change discovery protocol to mDNS
+ * @author Christoph Weitkamp - Use "discovery.chromecast:background=false" to disable discovery service
*/
-@Component
+@Component(configurationPid = "discovery.chromecast")
@NonNullByDefault
public class ChromecastDiscoveryParticipant implements MDNSDiscoveryParticipant {
+
private final Logger logger = LoggerFactory.getLogger(ChromecastDiscoveryParticipant.class);
private static final String PROPERTY_MODEL = "md";
private static final String PROPERTY_DEVICE_ID = "id";
private static final String SERVICE_TYPE = "_googlecast._tcp.local.";
+ private boolean isAutoDiscoveryEnabled = true;
+
+ @Activate
+ protected void activate(ComponentContext componentContext) {
+ activateOrModifyService(componentContext);
+ }
+
+ @Modified
+ protected void modified(ComponentContext componentContext) {
+ activateOrModifyService(componentContext);
+ }
+
+ private void activateOrModifyService(ComponentContext componentContext) {
+ Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
+ String autoDiscoveryPropertyValue = (String) properties
+ .get(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY);
+ if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
+ isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
+ }
+ }
+
@Override
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
return SUPPORTED_THING_TYPES_UIDS;
@Override
public @Nullable DiscoveryResult createResult(ServiceInfo service) {
- final ThingUID uid = getThingUID(service);
- if (uid == null) {
- return null;
+ if (isAutoDiscoveryEnabled) {
+ ThingUID uid = getThingUID(service);
+ if (uid != null) {
+ String host = service.getHostAddresses()[0];
+ int port = service.getPort();
+ logger.debug("Chromecast Found: {} {}", host, port);
+ String id = service.getPropertyString(PROPERTY_DEVICE_ID);
+ String friendlyName = service.getPropertyString(PROPERTY_FRIENDLY_NAME); // friendly name;
+ return DiscoveryResultBuilder.create(uid).withThingType(getThingType(service))
+ .withProperties(Map.of(HOST, host, PORT, port, DEVICE_ID, id))
+ .withRepresentationProperty(DEVICE_ID).withLabel(friendlyName).build();
+ }
}
-
- final Map<String, Object> properties = new HashMap<>(5);
- String host = service.getHostAddresses()[0];
- properties.put(HOST, host);
- int port = service.getPort();
- properties.put(PORT, port);
- logger.debug("Chromecast Found: {} {}", host, port);
- String id = service.getPropertyString(PROPERTY_DEVICE_ID);
- properties.put(DEVICE_ID, id);
- String friendlyName = service.getPropertyString(PROPERTY_FRIENDLY_NAME); // friendly name;
-
- final DiscoveryResult result = DiscoveryResultBuilder.create(uid).withThingType(getThingType(service))
- .withProperties(properties).withRepresentationProperty(DEVICE_ID).withLabel(friendlyName).build();
-
- return result;
+ return null;
}
private @Nullable ThingTypeUID getThingType(final ServiceInfo service) {
if (thingTypeUID != null) {
String id = service.getPropertyString(PROPERTY_DEVICE_ID); // device id
return new ThingUID(thingTypeUID, id);
- } else {
- return null;
}
+ return null;
}
}