2 * Copyright (c) 2010-2022 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 boolean ignored = false;
84 String modelName = getModelName(device);
87 modelName = "CONNECT";
90 modelName = "CONNECTAMP";
99 // The Sonos Sub is ignored
106 ThingTypeUID thingUID = new ThingTypeUID(SonosBindingConstants.BINDING_ID, modelName);
107 if (!SonosBindingConstants.SUPPORTED_KNOWN_THING_TYPES_UIDS.contains(thingUID)) {
108 // Try with the model name all in uppercase
109 thingUID = new ThingTypeUID(SonosBindingConstants.BINDING_ID, modelName.toUpperCase());
110 // In case a new "unknown" Sonos player is discovered a generic ThingTypeUID will be used
111 if (!SonosBindingConstants.SUPPORTED_KNOWN_THING_TYPES_UIDS.contains(thingUID)) {
112 thingUID = SonosBindingConstants.ZONEPLAYER_THING_TYPE_UID;
116 logger.debug("Discovered a Sonos '{}' thing with UDN '{}'", thingUID,
117 device.getIdentity().getUdn().getIdentifierString());
118 return new ThingUID(thingUID, device.getIdentity().getUdn().getIdentifierString());
126 private String getModelName(RemoteDevice device) {
127 // For Ikea SYMFONISK models, the model name now starts with "SYMFONISK" with recent firmwares
128 // We can no more use extractModelName as it deletes the first word ("Sonos" for all other devices)
129 return device.getDetails().getModelDetails().getModelName().toUpperCase().contains("SYMFONISK") ? "SYMFONISK"
130 : SonosXMLParser.extractModelName(device.getDetails().getModelDetails().getModelName());
133 private @Nullable String getSonosRoomName(RemoteDevice device) {
134 return SonosXMLParser.getRoomName(device.getIdentity().getDescriptorURL().toString());