[chromecast] Added configuration flag to disable background discovery (#11689)
* Added configuration flag to disable background discovery Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
parent
6da7defddf
commit
7abd6b5413
|
@ -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:
|
||||
|
|
|
@ -14,7 +14,7 @@ package org.openhab.binding.chromecast.internal.discovery;
|
|||
|
||||
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.NonNullByDefault;
|
|||
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<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;
|
||||
|
@ -59,25 +86,20 @@ public class ChromecastDiscoveryParticipant implements MDNSDiscoveryParticipant
|
|||
|
||||
@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) {
|
||||
|
@ -102,8 +124,7 @@ public class ChromecastDiscoveryParticipant implements MDNSDiscoveryParticipant
|
|||
if (thingTypeUID != null) {
|
||||
String id = service.getPropertyString(PROPERTY_DEVICE_ID); // device id
|
||||
return new ThingUID(thingTypeUID, id);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue