From 56e9bfe1978fea30fafbb86891b6dfca2cbe33ad Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Sat, 4 Dec 2021 15:03:29 +0100 Subject: [PATCH] [chromecast] Added configuration flag to disable background discovery (#11689) * Added configuration flag to disable background discovery Signed-off-by: Christoph Weitkamp Signed-off-by: Michael Schmidt --- .../org.openhab.binding.chromecast/README.md | 10 ++- .../ChromecastDiscoveryParticipant.java | 67 ++++++++++++------- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/bundles/org.openhab.binding.chromecast/README.md b/bundles/org.openhab.binding.chromecast/README.md index f4d3f2f59e49c..2d9fd4b3c0597 100644 --- a/bundles/org.openhab.binding.chromecast/README.md +++ b/bundles/org.openhab.binding.chromecast/README.md @@ -28,15 +28,21 @@ Configure a Callback URL when the Chromecast cannot connect using the Primary Ad ## Discovery -Chromecast devices are discovered on the network using UPnP. +Chromecast devices are discovered on the network using mDNS. No authentication is required for accessing the devices on the network. +Auto-discovery is enabled by default. +To disable it, you can add the following line to `/services/runtime.cfg`: + +``` +discovery.chromecast:background=false +``` ## Thing Configuration Chromecast devices can also be manually added. The only configuration parameter is the `ipAddress`. For an audio group also the port is necessary. -The autodiscovery process finds the port automatically. +The auto-discovery process finds the port automatically. With manual thing configuration the parameter `port` must be determined manually. Example for audio group: diff --git a/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/discovery/ChromecastDiscoveryParticipant.java b/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/discovery/ChromecastDiscoveryParticipant.java index 2475b6578fff5..aaa187b0fc1ba 100644 --- a/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/discovery/ChromecastDiscoveryParticipant.java +++ b/bundles/org.openhab.binding.chromecast/src/main/java/org/openhab/binding/chromecast/internal/discovery/ChromecastDiscoveryParticipant.java @@ -14,7 +14,7 @@ import static org.openhab.binding.chromecast.internal.ChromecastBindingConstants.*; -import java.util.HashMap; +import java.util.Dictionary; import java.util.Map; import java.util.Set; @@ -24,22 +24,28 @@ 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"; @@ -47,6 +53,27 @@ public class ChromecastDiscoveryParticipant implements MDNSDiscoveryParticipant 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 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 getSupportedThingTypeUIDs() { return SUPPORTED_THING_TYPES_UIDS; @@ -59,25 +86,20 @@ public String getServiceType() { @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 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) { @@ -102,8 +124,7 @@ public String getServiceType() { if (thingTypeUID != null) { String id = service.getPropertyString(PROPERTY_DEVICE_ID); // device id return new ThingUID(thingTypeUID, id); - } else { - return null; } + return null; } }