2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.chromecast.internal.discovery;
15 import static org.openhab.binding.chromecast.internal.ChromecastBindingConstants.*;
17 import java.util.HashMap;
21 import javax.jmdns.ServiceInfo;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link ChromecastDiscoveryParticipant} is responsible for discovering Chromecast devices through UPnP.
37 * @author Kai Kreuzer - Initial contribution
38 * @author Daniel Walters - Change discovery protocol to mDNS
42 public class ChromecastDiscoveryParticipant implements MDNSDiscoveryParticipant {
43 private final Logger logger = LoggerFactory.getLogger(ChromecastDiscoveryParticipant.class);
45 private static final String PROPERTY_MODEL = "md";
46 private static final String PROPERTY_FRIENDLY_NAME = "fn";
47 private static final String PROPERTY_DEVICE_ID = "id";
48 private static final String SERVICE_TYPE = "_googlecast._tcp.local.";
51 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
52 return SUPPORTED_THING_TYPES_UIDS;
56 public String getServiceType() {
61 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
62 final ThingUID uid = getThingUID(service);
67 final Map<String, Object> properties = new HashMap<>(5);
68 String host = service.getHostAddresses()[0];
69 properties.put(HOST, host);
70 int port = service.getPort();
71 properties.put(PORT, port);
72 logger.debug("Chromecast Found: {} {}", host, port);
73 String id = service.getPropertyString(PROPERTY_DEVICE_ID);
74 properties.put(DEVICE_ID, id);
75 String friendlyName = service.getPropertyString(PROPERTY_FRIENDLY_NAME); // friendly name;
77 final DiscoveryResult result = DiscoveryResultBuilder.create(uid).withThingType(getThingType(service))
78 .withProperties(properties).withRepresentationProperty(DEVICE_ID).withLabel(friendlyName).build();
83 private @Nullable ThingTypeUID getThingType(final ServiceInfo service) {
84 String model = service.getPropertyString(PROPERTY_MODEL); // model
85 logger.debug("Chromecast Type: {}", model);
90 case "Chromecast Audio":
91 return THING_TYPE_AUDIO;
92 case "Google Cast Group":
93 return THING_TYPE_AUDIOGROUP;
95 return THING_TYPE_CHROMECAST;
100 public @Nullable ThingUID getThingUID(ServiceInfo service) {
101 ThingTypeUID thingTypeUID = getThingType(service);
102 if (thingTypeUID != null) {
103 String id = service.getPropertyString(PROPERTY_DEVICE_ID); // device id
104 return new ThingUID(thingTypeUID, id);