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.Collections;
20 import java.util.HashMap;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.jupnp.model.meta.RemoteDevice;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Component;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link YamahaDiscoveryParticipant} is responsible for processing the
38 * results of searched UPnP devices
40 * @author David Graeff - Initial contribution
41 * @author Tomasz Maruszak - Introduced config object, migrated to newer UPnP api
45 public class YamahaDiscoveryParticipant implements UpnpDiscoveryParticipant {
47 private final Logger logger = LoggerFactory.getLogger(YamahaDiscoveryParticipant.class);
49 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(BRIDGE_THING_TYPE);
52 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53 return SUPPORTED_THING_TYPES;
57 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
58 ThingUID uid = getThingUID(device);
63 Map<String, Object> properties = new HashMap<>(3);
65 String label = "Yamaha Receiver";
67 label += " " + device.getDetails().getModelDetails().getModelName();
68 } catch (Exception e) {
69 // ignore and use the default label
72 URL url = device.getIdentity().getDescriptorURL();
73 properties.put(CONFIG_HOST_NAME, url.getHost());
75 // The port via UPNP is unreliable, sometimes it is 8080, on some models 49154.
76 // But so far the API was always reachable via port 80.
77 // We provide the port config therefore, if the user ever needs to adjust the port.
78 // Note the port is set in the thing-types.xml to 80 by default.
80 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withTTL(MIN_MAX_AGE_SECS).withProperties(properties)
81 .withLabel(label).withRepresentationProperty(CONFIG_HOST_NAME).build();
83 logger.debug("Discovered a Yamaha Receiver '{}' model '{}' thing with UDN '{}'",
84 device.getDetails().getFriendlyName(), device.getDetails().getModelDetails().getModelName(),
85 device.getIdentity().getUdn().getIdentifierString());
90 public static @Nullable ThingUID getThingUID(@Nullable String manufacturer, @Nullable String deviceType,
92 if (manufacturer == null || deviceType == null) {
96 if (manufacturer.toUpperCase().contains(UPNP_MANUFACTURER) && deviceType.equals(UPNP_TYPE)) {
97 return new ThingUID(BRIDGE_THING_TYPE, udn);
104 public @Nullable ThingUID getThingUID(RemoteDevice device) {
105 String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
106 String deviceType = device.getType().getType();
108 // UDN shouldn't contain '-' characters.
109 return getThingUID(manufacturer, deviceType,
110 device.getIdentity().getUdn().getIdentifierString().replace("-", "_"));