]> git.basschouten.com Git - openhab-addons.git/blob
805bded37b1f58dc89300cae6ef8537a2c252cd9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 "Roam SL":
99                         modelName = "RoamSL";
100                         break;
101                     case "Sub":
102                         // The Sonos Sub is ignored
103                         ignored = true;
104                         break;
105                     default:
106                         break;
107                 }
108                 if (!ignored) {
109                     ThingTypeUID thingUID = new ThingTypeUID(SonosBindingConstants.BINDING_ID, modelName);
110                     if (!SonosBindingConstants.SUPPORTED_KNOWN_THING_TYPES_UIDS.contains(thingUID)) {
111                         // Try with the model name all in uppercase
112                         thingUID = new ThingTypeUID(SonosBindingConstants.BINDING_ID, modelName.toUpperCase());
113                         // In case a new "unknown" Sonos player is discovered a generic ThingTypeUID will be used
114                         if (!SonosBindingConstants.SUPPORTED_KNOWN_THING_TYPES_UIDS.contains(thingUID)) {
115                             thingUID = SonosBindingConstants.ZONEPLAYER_THING_TYPE_UID;
116                         }
117                     }
118
119                     logger.debug("Discovered a Sonos '{}' thing with UDN '{}'", thingUID,
120                             device.getIdentity().getUdn().getIdentifierString());
121                     return new ThingUID(thingUID, device.getIdentity().getUdn().getIdentifierString());
122                 }
123             }
124         }
125
126         return null;
127     }
128
129     private String getModelName(RemoteDevice device) {
130         // For Ikea SYMFONISK models, the model name now starts with "SYMFONISK" with recent firmwares
131         // We can no more use extractModelName as it deletes the first word ("Sonos" for all other devices)
132         return device.getDetails().getModelDetails().getModelName().toUpperCase().contains("SYMFONISK") ? "SYMFONISK"
133                 : SonosXMLParser.extractModelName(device.getDetails().getModelDetails().getModelName());
134     }
135
136     private @Nullable String getSonosRoomName(RemoteDevice device) {
137         return SonosXMLParser.getRoomName(device.getIdentity().getDescriptorURL().toString());
138     }
139 }