]> git.basschouten.com Git - openhab-addons.git/blob
8b84df74bd513b48f2fc9abe0ff0391e5b907817
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.sonos.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Set;
18
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;
33
34 /**
35  * The {@link ZonePlayerDiscoveryParticipant} is responsible processing the
36  * results of searches for UPNP devices
37  *
38  * @author Karel Goderis - Initial contribution
39  */
40 @NonNullByDefault
41 @Component
42 public class ZonePlayerDiscoveryParticipant implements UpnpDiscoveryParticipant {
43
44     private final Logger logger = LoggerFactory.getLogger(ZonePlayerDiscoveryParticipant.class);
45
46     @Override
47     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48         return SonosBindingConstants.SUPPORTED_THING_TYPES_UIDS;
49     }
50
51     @Override
52     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
53         ThingUID uid = getThingUID(device);
54         if (uid != null) {
55             String roomName = getSonosRoomName(device);
56             if (roomName != null) {
57                 Map<String, Object> properties = new HashMap<>(3);
58                 String label = "Sonos device";
59                 try {
60                     label = device.getDetails().getModelDetails().getModelName();
61                 } catch (Exception e) {
62                     // ignore and use default label
63                 }
64                 label += " (" + roomName + ")";
65                 properties.put(ZonePlayerConfiguration.UDN, device.getIdentity().getUdn().getIdentifierString());
66                 properties.put(SonosBindingConstants.IDENTIFICATION, roomName);
67
68                 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label)
69                         .withRepresentationProperty(ZonePlayerConfiguration.UDN).build();
70
71                 logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}'",
72                         device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString());
73                 return result;
74             }
75         }
76         return null;
77     }
78
79     @Override
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);
85                 switch (modelName) {
86                     case "ZP80":
87                         modelName = "CONNECT";
88                         break;
89                     case "ZP100":
90                         modelName = "CONNECTAMP";
91                         break;
92                     case "One SL":
93                         modelName = "OneSL";
94                         break;
95                     case "Arc SL":
96                         modelName = "ArcSL";
97                         break;
98                     case "Sub":
99                         // The Sonos Sub is ignored
100                         ignored = true;
101                         break;
102                     default:
103                         break;
104                 }
105                 if (!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;
113                         }
114                     }
115
116                     logger.debug("Discovered a Sonos '{}' thing with UDN '{}'", thingUID,
117                             device.getIdentity().getUdn().getIdentifierString());
118                     return new ThingUID(thingUID, device.getIdentity().getUdn().getIdentifierString());
119                 }
120             }
121         }
122
123         return null;
124     }
125
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());
131     }
132
133     private @Nullable String getSonosRoomName(RemoteDevice device) {
134         return SonosXMLParser.getRoomName(device.getIdentity().getDescriptorURL().toString());
135     }
136 }