Skip to content

Commit

Permalink
[chromecast] Added configuration flag to disable background discovery (
Browse files Browse the repository at this point in the history
…openhab#11689)

* Added configuration flag to disable background discovery

Signed-off-by: Christoph Weitkamp <[email protected]>
Signed-off-by: Michael Schmidt <[email protected]>
  • Loading branch information
cweitkamp authored and mischmidt83 committed Jan 9, 2022
1 parent b4d1eaa commit 56e9bfe
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
10 changes: 8 additions & 2 deletions bundles/org.openhab.binding.chromecast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<openHAB-conf>/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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,29 +24,56 @@
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_FRIENDLY_NAME = "fn";
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;
Expand All @@ -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<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) {
Expand All @@ -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;
}
}

0 comments on commit 56e9bfe

Please sign in to comment.