[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
|
## 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.
|
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
|
## Thing Configuration
|
||||||
|
|
||||||
Chromecast devices can also be manually added.
|
Chromecast devices can also be manually added.
|
||||||
The only configuration parameter is the `ipAddress`.
|
The only configuration parameter is the `ipAddress`.
|
||||||
For an audio group also the port is necessary.
|
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.
|
With manual thing configuration the parameter `port` must be determined manually.
|
||||||
|
|
||||||
Example for audio group:
|
Example for audio group:
|
||||||
|
|
|
@ -14,7 +14,7 @@ package org.openhab.binding.chromecast.internal.discovery;
|
||||||
|
|
||||||
import static org.openhab.binding.chromecast.internal.ChromecastBindingConstants.*;
|
import static org.openhab.binding.chromecast.internal.ChromecastBindingConstants.*;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.Dictionary;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -24,22 +24,28 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.openhab.core.config.discovery.DiscoveryResult;
|
import org.openhab.core.config.discovery.DiscoveryResult;
|
||||||
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
|
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.config.discovery.mdns.MDNSDiscoveryParticipant;
|
||||||
import org.openhab.core.thing.ThingTypeUID;
|
import org.openhab.core.thing.ThingTypeUID;
|
||||||
import org.openhab.core.thing.ThingUID;
|
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.Component;
|
||||||
|
import org.osgi.service.component.annotations.Modified;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
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 Kai Kreuzer - Initial contribution
|
||||||
* @author Daniel Walters - Change discovery protocol to mDNS
|
* @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
|
@NonNullByDefault
|
||||||
public class ChromecastDiscoveryParticipant implements MDNSDiscoveryParticipant {
|
public class ChromecastDiscoveryParticipant implements MDNSDiscoveryParticipant {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(ChromecastDiscoveryParticipant.class);
|
private final Logger logger = LoggerFactory.getLogger(ChromecastDiscoveryParticipant.class);
|
||||||
|
|
||||||
private static final String PROPERTY_MODEL = "md";
|
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 PROPERTY_DEVICE_ID = "id";
|
||||||
private static final String SERVICE_TYPE = "_googlecast._tcp.local.";
|
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
|
@Override
|
||||||
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
|
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
|
||||||
return SUPPORTED_THING_TYPES_UIDS;
|
return SUPPORTED_THING_TYPES_UIDS;
|
||||||
|
@ -59,25 +86,20 @@ public class ChromecastDiscoveryParticipant implements MDNSDiscoveryParticipant
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @Nullable DiscoveryResult createResult(ServiceInfo service) {
|
public @Nullable DiscoveryResult createResult(ServiceInfo service) {
|
||||||
final ThingUID uid = getThingUID(service);
|
if (isAutoDiscoveryEnabled) {
|
||||||
if (uid == null) {
|
ThingUID uid = getThingUID(service);
|
||||||
return null;
|
if (uid != null) {
|
||||||
}
|
|
||||||
|
|
||||||
final Map<String, Object> properties = new HashMap<>(5);
|
|
||||||
String host = service.getHostAddresses()[0];
|
String host = service.getHostAddresses()[0];
|
||||||
properties.put(HOST, host);
|
|
||||||
int port = service.getPort();
|
int port = service.getPort();
|
||||||
properties.put(PORT, port);
|
|
||||||
logger.debug("Chromecast Found: {} {}", host, port);
|
logger.debug("Chromecast Found: {} {}", host, port);
|
||||||
String id = service.getPropertyString(PROPERTY_DEVICE_ID);
|
String id = service.getPropertyString(PROPERTY_DEVICE_ID);
|
||||||
properties.put(DEVICE_ID, id);
|
|
||||||
String friendlyName = service.getPropertyString(PROPERTY_FRIENDLY_NAME); // friendly name;
|
String friendlyName = service.getPropertyString(PROPERTY_FRIENDLY_NAME); // friendly name;
|
||||||
|
return DiscoveryResultBuilder.create(uid).withThingType(getThingType(service))
|
||||||
final DiscoveryResult result = DiscoveryResultBuilder.create(uid).withThingType(getThingType(service))
|
.withProperties(Map.of(HOST, host, PORT, port, DEVICE_ID, id))
|
||||||
.withProperties(properties).withRepresentationProperty(DEVICE_ID).withLabel(friendlyName).build();
|
.withRepresentationProperty(DEVICE_ID).withLabel(friendlyName).build();
|
||||||
|
}
|
||||||
return result;
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private @Nullable ThingTypeUID getThingType(final ServiceInfo service) {
|
private @Nullable ThingTypeUID getThingType(final ServiceInfo service) {
|
||||||
|
@ -102,8 +124,7 @@ public class ChromecastDiscoveryParticipant implements MDNSDiscoveryParticipant
|
||||||
if (thingTypeUID != null) {
|
if (thingTypeUID != null) {
|
||||||
String id = service.getPropertyString(PROPERTY_DEVICE_ID); // device id
|
String id = service.getPropertyString(PROPERTY_DEVICE_ID); // device id
|
||||||
return new ThingUID(thingTypeUID, id);
|
return new ThingUID(thingTypeUID, id);
|
||||||
} else {
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue