]> git.basschouten.com Git - openhab-addons.git/blob
88067dbfc1ffc411e518f329c7486dd9f2e66da0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.heos.internal.discovery;
14
15 import static org.openhab.binding.heos.internal.HeosBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.jupnp.model.meta.DeviceDetails;
24 import org.jupnp.model.meta.ModelDetails;
25 import org.jupnp.model.meta.RemoteDevice;
26 import org.openhab.binding.heos.internal.configuration.BridgeConfiguration;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.osgi.service.component.annotations.Component;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link HeosDiscoveryParticipant} discovers the HEOS Player of the
39  * network via an UPnP interface.
40  *
41  * @author Johannes Einig - Initial contribution
42  */
43 @NonNullByDefault
44 @Component(service = UpnpDiscoveryParticipant.class, configurationPid = "discovery.heos")
45 public class HeosDiscoveryParticipant implements UpnpDiscoveryParticipant {
46     private final Logger logger = LoggerFactory.getLogger(HeosDiscoveryParticipant.class);
47
48     @Override
49     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50         return Set.of(THING_TYPE_BRIDGE);
51     }
52
53     @Override
54     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
55         ThingUID uid = getThingUID(device);
56         if (uid != null) {
57             Map<String, Object> properties = new HashMap<>();
58             properties.put(Thing.PROPERTY_VENDOR, device.getDetails().getManufacturerDetails().getManufacturer());
59             properties.put(Thing.PROPERTY_MODEL_ID, getModel(device.getDetails().getModelDetails()));
60             properties.put(Thing.PROPERTY_SERIAL_NUMBER, device.getDetails().getSerialNumber());
61             properties.put(BridgeConfiguration.IP_ADDRESS, device.getIdentity().getDescriptorURL().getHost());
62             properties.put(PROP_NAME, device.getDetails().getFriendlyName());
63             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
64                     .withLabel(" Bridge - " + device.getDetails().getFriendlyName())
65                     .withRepresentationProperty(Thing.PROPERTY_VENDOR).build();
66             logger.debug("Found HEOS device with UID: {}", uid.getAsString());
67             return result;
68         }
69         return null;
70     }
71
72     private String getModel(ModelDetails modelDetails) {
73         return String.format("%s (%s)", modelDetails.getModelName(), modelDetails.getModelNumber());
74     }
75
76     @Override
77     public @Nullable ThingUID getThingUID(RemoteDevice device) {
78         DeviceDetails details = device.getDetails();
79         String modelName = details.getModelDetails().getModelName();
80         String modelManufacturer = details.getManufacturerDetails().getManufacturer();
81         if ("Denon".equals(modelManufacturer)
82                 && (modelName.startsWith("HEOS") || modelName.endsWith("H") || modelName.contains("Home"))) {
83             String deviceType = device.getType().getType();
84             if (deviceType.startsWith("ACT") || deviceType.startsWith("Aios")) {
85                 return new ThingUID(THING_TYPE_BRIDGE, device.getIdentity().getUdn().getIdentifierString());
86             }
87         }
88         return null;
89     }
90 }