]> git.basschouten.com Git - openhab-addons.git/blob
c55c651bb8dcd8740360037b341eb95792646913
[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.lgwebos.internal.discovery;
14
15 import static org.openhab.binding.lgwebos.internal.LGWebOSBindingConstants.*;
16
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.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.osgi.service.component.annotations.Component;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * This Upnp Discovery participant add the ability to auto discover LG Web OS devices on the network.
34  * Some users choose to not use upnp. Therefore this can only play an optional role and help discover the device and its
35  * ip.
36  *
37  * @author Sebastian Prehn - Initial contribution
38  */
39 @NonNullByDefault
40 @Component(service = UpnpDiscoveryParticipant.class, configurationPid = "discovery.lgwebos.upnp")
41 public class LGWebOSUpnpDiscoveryParticipant implements UpnpDiscoveryParticipant {
42
43     private final Logger logger = LoggerFactory.getLogger(LGWebOSUpnpDiscoveryParticipant.class);
44
45     @Override
46     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
47         return SUPPORTED_THING_TYPES_UIDS;
48     }
49
50     @Override
51     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
52         ThingUID thingUID = getThingUID(device);
53
54         if (thingUID == null) {
55             return null;
56         }
57
58         String modelName = device.getDetails().getModelDetails().getModelName();
59         if (device.getDetails().getModelDetails().getModelNumber() != null) {
60             modelName += " " + device.getDetails().getModelDetails().getModelNumber();
61         }
62
63         return DiscoveryResultBuilder.create(thingUID).withLabel(device.getDetails().getFriendlyName())
64                 .withProperty(PROPERTY_DEVICE_ID, device.getIdentity().getUdn().getIdentifierString())
65                 .withProperty(CONFIG_HOST, device.getIdentity().getDescriptorURL().getHost())
66                 .withProperty(Thing.PROPERTY_MODEL_ID, modelName)
67                 .withProperty(Thing.PROPERTY_VENDOR, device.getDetails().getManufacturerDetails().getManufacturer())
68                 .withRepresentationProperty(PROPERTY_DEVICE_ID).withThingType(THING_TYPE_WEBOSTV).build();
69     }
70
71     @Override
72     public @Nullable ThingUID getThingUID(RemoteDevice device) {
73         logger.trace("Discovered remote device {}", device);
74         if (device.findService(UPNP_SERVICE_TYPE) != null) {
75             logger.debug("Found LG WebOS TV: {}", device);
76             return new ThingUID(THING_TYPE_WEBOSTV, device.getIdentity().getUdn().getIdentifierString());
77         }
78         return null;
79     }
80 }