]> git.basschouten.com Git - openhab-addons.git/blob
940946c7b630c0464bdbd52d1c5ec01325cbd5ce
[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.wemo.internal.discovery;
14
15 import static org.openhab.binding.wemo.internal.WemoBindingConstants.*;
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.RemoteDevice;
24 import org.openhab.binding.wemo.internal.WemoBindingConstants;
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.config.discovery.upnp.internal.UpnpDiscoveryService;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The {@link WemoDiscoveryParticipant} is responsible for discovering new and
37  * removed Wemo devices. It uses the central {@link UpnpDiscoveryService}.
38  *
39  * @author Hans-Jörg Merk - Initial contribution
40  * @author Kai Kreuzer - some refactoring for performance and simplification
41  *
42  */
43 @NonNullByDefault
44 @Component(service = UpnpDiscoveryParticipant.class)
45 public class WemoDiscoveryParticipant implements UpnpDiscoveryParticipant {
46
47     private Logger logger = LoggerFactory.getLogger(WemoDiscoveryParticipant.class);
48
49     @Override
50     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51         return WemoBindingConstants.SUPPORTED_THING_TYPES;
52     }
53
54     @Override
55     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
56         ThingUID uid = getThingUID(device);
57         if (uid != null) {
58             Map<String, Object> properties = new HashMap<>(2);
59             String label = "WeMo Device";
60             try {
61                 label = device.getDetails().getFriendlyName();
62             } catch (Exception e) {
63                 // ignore and use default label
64             }
65             properties.put(UDN, device.getIdentity().getUdn().getIdentifierString());
66
67             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label)
68                     .withRepresentationProperty(UDN).build();
69
70             logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}'",
71                     device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString());
72
73             return result;
74         } else {
75             return null;
76         }
77     }
78
79     @Override
80     public @Nullable ThingUID getThingUID(@Nullable RemoteDevice device) {
81         if (device != null) {
82             if (device.getDetails().getManufacturerDetails().getManufacturer() != null) {
83                 if (device.getDetails().getManufacturerDetails().getManufacturer().toUpperCase().contains("BELKIN")) {
84                     if (device.getDetails().getModelDetails().getModelName() != null) {
85                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("socket")) {
86                             logger.debug("Discovered a WeMo Socket thing with UDN '{}'",
87                                     device.getIdentity().getUdn().getIdentifierString());
88                             return new ThingUID(THING_TYPE_SOCKET, device.getIdentity().getUdn().getIdentifierString());
89                         }
90                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("insight")) {
91                             logger.debug("Discovered a WeMo Insight thing with UDN '{}'",
92                                     device.getIdentity().getUdn().getIdentifierString());
93                             return new ThingUID(THING_TYPE_INSIGHT,
94                                     device.getIdentity().getUdn().getIdentifierString());
95                         }
96                         if (device.getDetails().getModelDetails().getModelName().toLowerCase()
97                                 .startsWith("lightswitch")) {
98                             logger.debug("Discovered a WeMo Lightswitch thing with UDN '{}'",
99                                     device.getIdentity().getUdn().getIdentifierString());
100                             return new ThingUID(THING_TYPE_LIGHTSWITCH,
101                                     device.getIdentity().getUdn().getIdentifierString());
102                         }
103                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("motion")) {
104                             logger.debug("Discovered a WeMo Motion thing with UDN '{}'",
105                                     device.getIdentity().getUdn().getIdentifierString());
106                             return new ThingUID(THING_TYPE_MOTION, device.getIdentity().getUdn().getIdentifierString());
107                         }
108                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("sensor")) {
109                             logger.debug("Discovered a WeMo Motion thing with UDN '{}'",
110                                     device.getIdentity().getUdn().getIdentifierString());
111                             return new ThingUID(THING_TYPE_MOTION, device.getIdentity().getUdn().getIdentifierString());
112                         }
113                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("bridge")) {
114                             logger.debug("Discovered a WeMo Bridge thing with UDN '{}'",
115                                     device.getIdentity().getUdn().getIdentifierString());
116                             return new ThingUID(THING_TYPE_BRIDGE, device.getIdentity().getUdn().getIdentifierString());
117                         }
118                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("maker")) {
119                             logger.debug("Discovered a WeMo Maker thing with UDN '{}'",
120                                     device.getIdentity().getUdn().getIdentifierString());
121                             return new ThingUID(THING_TYPE_MAKER, device.getIdentity().getUdn().getIdentifierString());
122                         }
123                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("coffee")) {
124                             logger.debug("Discovered a WeMo Coffe Maker thing with UDN '{}'",
125                                     device.getIdentity().getUdn().getIdentifierString());
126                             return new ThingUID(THING_TYPE_COFFEE, device.getIdentity().getUdn().getIdentifierString());
127                         }
128                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("dimmer")) {
129                             logger.debug("Discovered a WeMo Dimmer Switch thing with UDN '{}'",
130                                     device.getIdentity().getUdn().getIdentifierString());
131                             return new ThingUID(THING_TYPE_DIMMER, device.getIdentity().getUdn().getIdentifierString());
132                         }
133                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("crockpot")) {
134                             logger.debug("Discovered a WeMo enabled Crockpot thing with UDN '{}'",
135                                     device.getIdentity().getUdn().getIdentifierString());
136                             return new ThingUID(THING_TYPE_CROCKPOT,
137                                     device.getIdentity().getUdn().getIdentifierString());
138                         }
139                         if (device.getDetails().getModelDetails().getModelName().toLowerCase()
140                                 .startsWith("airpurifier")) {
141                             logger.debug("Discovered a WeMo enabled Holmes Air Purifier thing with UDN '{}'",
142                                     device.getIdentity().getUdn().getIdentifierString());
143                             return new ThingUID(THING_TYPE_PURIFIER,
144                                     device.getIdentity().getUdn().getIdentifierString());
145                         }
146                         if (device.getDetails().getModelDetails().getModelName().toLowerCase()
147                                 .startsWith("humidifier")) {
148                             logger.debug("Discovered a WeMo enabled Holmes Humidifier thing with UDN '{}'",
149                                     device.getIdentity().getUdn().getIdentifierString());
150                             return new ThingUID(THING_TYPE_HUMIDIFIER,
151                                     device.getIdentity().getUdn().getIdentifierString());
152                         }
153                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("heater")) {
154                             logger.debug("Discovered a WeMo enabled Heater thing with UDN '{}'",
155                                     device.getIdentity().getUdn().getIdentifierString());
156                             return new ThingUID(THING_TYPE_HEATER, device.getIdentity().getUdn().getIdentifierString());
157                         }
158                     }
159                 }
160             }
161         }
162         return null;
163     }
164 }