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.yamahareceiver.internal.discovery;
15 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.*;
16 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Configs.CONFIG_HOST_NAME;
19 import java.util.HashMap;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.jupnp.model.meta.RemoteDevice;
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 YamahaDiscoveryParticipant} is responsible for processing the
37 * results of searched UPnP devices
39 * @author David Graeff - Initial contribution
40 * @author Tomasz Maruszak - Introduced config object, migrated to newer UPnP api
44 public class YamahaDiscoveryParticipant implements UpnpDiscoveryParticipant {
46 private final Logger logger = LoggerFactory.getLogger(YamahaDiscoveryParticipant.class);
48 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(BRIDGE_THING_TYPE);
51 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
52 return SUPPORTED_THING_TYPES;
56 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
57 ThingUID uid = getThingUID(device);
62 Map<String, Object> properties = new HashMap<>(3);
64 String label = "Yamaha Receiver";
66 label += " " + device.getDetails().getModelDetails().getModelName();
67 } catch (Exception e) {
68 // ignore and use the default label
71 URL url = device.getIdentity().getDescriptorURL();
72 properties.put(CONFIG_HOST_NAME, url.getHost());
74 // The port via UPNP is unreliable, sometimes it is 8080, on some models 49154.
75 // But so far the API was always reachable via port 80.
76 // We provide the port config therefore, if the user ever needs to adjust the port.
77 // Note the port is set in the thing-types.xml to 80 by default.
79 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withTTL(MIN_MAX_AGE_SECS).withProperties(properties)
80 .withLabel(label).withRepresentationProperty(CONFIG_HOST_NAME).build();
82 logger.debug("Discovered a Yamaha Receiver '{}' model '{}' thing with UDN '{}'",
83 device.getDetails().getFriendlyName(), device.getDetails().getModelDetails().getModelName(),
84 device.getIdentity().getUdn().getIdentifierString());
89 public static @Nullable ThingUID getThingUID(@Nullable String manufacturer, @Nullable String deviceType,
91 if (manufacturer == null || deviceType == null) {
95 if (manufacturer.toUpperCase().contains(UPNP_MANUFACTURER) && deviceType.equals(UPNP_TYPE)) {
96 return new ThingUID(BRIDGE_THING_TYPE, udn);
103 public @Nullable ThingUID getThingUID(RemoteDevice device) {
104 String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
105 String deviceType = device.getType().getType();
107 // UDN shouldn't contain '-' characters.
108 return getThingUID(manufacturer, deviceType,
109 device.getIdentity().getUdn().getIdentifierString().replace("-", "_"));