2 * Copyright (c) 2010-2024 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.binding.samsungtv.internal.Utils;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link SamsungTvDiscoveryParticipant} is responsible for processing the
37 * results of searched UPnP devices
39 * @author Pauli Anttila - Initial contribution
40 * @author Arjan Mels - Changed to upnp.UpnpDiscoveryParticipant
41 * @author Nick Waterton - use Utils class
45 public class SamsungTvDiscoveryParticipant implements UpnpDiscoveryParticipant {
46 private final Logger logger = LoggerFactory.getLogger(SamsungTvDiscoveryParticipant.class);
49 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50 return Set.of(SAMSUNG_TV_THING_TYPE);
54 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
55 ThingUID uid = getThingUID(device);
57 Map<String, Object> properties = new HashMap<>();
58 properties.put(HOST_NAME, Utils.getHost(device));
60 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
61 .withRepresentationProperty(HOST_NAME).withLabel(getLabel(device)).build();
63 logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}' and properties: {}",
64 Utils.getModelName(device), Utils.getUdn(device), properties);
70 private String getLabel(RemoteDevice device) {
71 String label = Utils.getFriendlyName(device);
72 return label.isBlank() ? "Samsung TV" : label;
76 public @Nullable ThingUID getThingUID(RemoteDevice device) {
77 if (Utils.getManufacturer(device).toUpperCase().contains("SAMSUNG ELECTRONICS")) {
78 // One Samsung TV contains several UPnP devices.
79 // Create unique Samsung TV thing for every MediaRenderer
80 // device and ignore rest of the UPnP devices.
81 // use MediaRenderer udn for ThingID.
83 if ("MediaRenderer".equals(Utils.getType(device))) {
84 String udn = Utils.getUdn(device);
85 if (logger.isDebugEnabled()) {
86 logger.debug("Retrieved Thing UID for a Samsung TV '{}' model '{}' thing with UDN '{}'",
87 Utils.getFriendlyName(device), Utils.getModelName(device), udn);
90 return new ThingUID(SAMSUNG_TV_THING_TYPE, udn);