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