]> git.basschouten.com Git - openhab-addons.git/blob
2c632d9a6e6f4e57a07e2dfde24919f6e5a305d2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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                 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())
87                         && !udn.isEmpty()) {
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;
95                             logger.warn(
96                                     "'{}' is not yet a supported model, thing type '{}' is considered as default; please open an issue",
97                                     device.getDetails().getModelDetails().getModelName(), thingTypeUID);
98                         }
99                     }
100
101                     logger.debug("Discovered a Sonos '{}' thing with UDN '{}'", thingTypeUID, udn);
102                     return new ThingUID(thingTypeUID, udn);
103                 }
104             }
105         }
106
107         return null;
108     }
109
110     private @Nullable String getSonosRoomName(RemoteDevice device) {
111         return SonosXMLParser.getRoomName(device.getIdentity().getDescriptorURL().toString());
112     }
113 }