2 * Copyright (c) 2010-2023 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.samsungtv.internal.discovery;
15 import static org.openhab.binding.samsungtv.internal.SamsungTvBindingConstants.SAMSUNG_TV_THING_TYPE;
16 import static org.openhab.binding.samsungtv.internal.config.SamsungTvConfiguration.HOST_NAME;
18 import java.util.HashMap;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.jupnp.model.meta.RemoteDevice;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
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 SamsungTvDiscoveryParticipant} is responsible for processing the
36 * results of searched UPnP devices
38 * @author Pauli Anttila - Initial contribution
39 * @author Arjan Mels - Changed to upnp.UpnpDiscoveryParticipant
43 public class SamsungTvDiscoveryParticipant implements UpnpDiscoveryParticipant {
44 private final Logger logger = LoggerFactory.getLogger(SamsungTvDiscoveryParticipant.class);
47 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48 return Set.of(SAMSUNG_TV_THING_TYPE);
52 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
53 ThingUID uid = getThingUID(device);
55 Map<String, Object> properties = new HashMap<>();
56 properties.put(HOST_NAME, device.getIdentity().getDescriptorURL().getHost());
58 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
59 .withRepresentationProperty(HOST_NAME).withLabel(getLabel(device)).build();
61 logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}' and properties: {}",
62 device.getDetails().getModelDetails().getModelName(),
63 device.getIdentity().getUdn().getIdentifierString(), properties);
70 private String getLabel(RemoteDevice device) {
71 String label = "Samsung TV";
73 label = device.getDetails().getFriendlyName();
74 } catch (Exception e) {
75 // ignore and use the default label
81 public @Nullable ThingUID getThingUID(RemoteDevice device) {
82 if (device.getDetails() != null && device.getDetails().getManufacturerDetails() != null) {
83 String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
85 if (manufacturer != null && manufacturer.toUpperCase().contains("SAMSUNG ELECTRONICS")) {
86 // One Samsung TV contains several UPnP devices.
87 // Create unique Samsung TV thing for every MediaRenderer
88 // device and ignore rest of the UPnP devices.
90 if (device.getType() != null && "MediaRenderer".equals(device.getType().getType())) {
91 // UDN shouldn't contain '-' characters.
92 String udn = device.getIdentity().getUdn().getIdentifierString().replace("-", "_");
94 if (logger.isDebugEnabled()) {
95 String modelName = device.getDetails().getModelDetails().getModelName();
96 String friendlyName = device.getDetails().getFriendlyName();
97 logger.debug("Retrieved Thing UID for a Samsung TV '{}' model '{}' thing with UDN '{}'",
98 friendlyName, modelName, udn);
101 return new ThingUID(SAMSUNG_TV_THING_TYPE, udn);