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.sonos.internal.discovery;
15 import java.util.HashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.jupnp.model.meta.RemoteDevice;
22 import org.openhab.binding.sonos.internal.SonosBindingConstants;
23 import org.openhab.binding.sonos.internal.SonosXMLParser;
24 import org.openhab.binding.sonos.internal.config.ZonePlayerConfiguration;
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 ZonePlayerDiscoveryParticipant} is responsible processing the
36 * results of searches for UPNP devices
38 * @author Karel Goderis - Initial contribution
42 public class ZonePlayerDiscoveryParticipant implements UpnpDiscoveryParticipant {
44 private final Logger logger = LoggerFactory.getLogger(ZonePlayerDiscoveryParticipant.class);
47 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48 return SonosBindingConstants.SUPPORTED_THING_TYPES_UIDS;
52 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
53 ThingUID uid = getThingUID(device);
55 String roomName = getSonosRoomName(device);
56 if (roomName != null) {
57 Map<String, Object> properties = new HashMap<>(3);
58 String label = "Sonos device";
60 label = device.getDetails().getModelDetails().getModelName();
61 } catch (Exception e) {
62 // ignore and use default label
64 label += " (" + roomName + ")";
65 properties.put(ZonePlayerConfiguration.UDN, device.getIdentity().getUdn().getIdentifierString());
66 properties.put(SonosBindingConstants.IDENTIFICATION, roomName);
68 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label)
69 .withRepresentationProperty(ZonePlayerConfiguration.UDN).build();
71 logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}'",
72 device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString());
80 public @Nullable ThingUID getThingUID(RemoteDevice device) {
81 if (device.getDetails().getManufacturerDetails().getManufacturer() != null) {
82 if (device.getDetails().getManufacturerDetails().getManufacturer().toUpperCase().contains("SONOS")) {
83 String id = SonosXMLParser
84 .buildThingTypeIdFromModelName(device.getDetails().getModelDetails().getModelName());
85 String udn = device.getIdentity().getUdn().getIdentifierString();
86 if (!id.isEmpty() && !SonosBindingConstants.UNSUPPORTED_KNOWN_IDS.contains(id.toLowerCase())
88 ThingTypeUID thingTypeUID = new ThingTypeUID(SonosBindingConstants.BINDING_ID, id);
89 if (!SonosBindingConstants.SUPPORTED_KNOWN_THING_TYPES_UIDS.contains(thingTypeUID)) {
90 // Try with the model name all in uppercase
91 thingTypeUID = new ThingTypeUID(SonosBindingConstants.BINDING_ID, id.toUpperCase());
92 // In case a new "unknown" Sonos player is discovered a generic ThingTypeUID will be used
93 if (!SonosBindingConstants.SUPPORTED_KNOWN_THING_TYPES_UIDS.contains(thingTypeUID)) {
94 thingTypeUID = SonosBindingConstants.ZONEPLAYER_THING_TYPE_UID;
96 "'{}' is not yet a supported model, thing type '{}' is considered as default; please open an issue",
97 device.getDetails().getModelDetails().getModelName(), thingTypeUID);
101 logger.debug("Discovered a Sonos '{}' thing with UDN '{}'", thingTypeUID, udn);
102 return new ThingUID(thingTypeUID, udn);
110 private @Nullable String getSonosRoomName(RemoteDevice device) {
111 return SonosXMLParser.getRoomName(device.getIdentity().getDescriptorURL().toString());