]> git.basschouten.com Git - openhab-addons.git/blob
1524248995eae704a34dffe697efd5d800bd3ea6
[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.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()
90                                 .startsWith("outdoorplug")) {
91                             logger.debug("Discovered a WeMo Outdoor Plug thing with UDN '{}'",
92                                     device.getIdentity().getUdn().getIdentifierString());
93                             return new ThingUID(THING_TYPE_SOCKET, device.getIdentity().getUdn().getIdentifierString());
94                         }
95                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("insight")) {
96                             logger.debug("Discovered a WeMo Insight thing with UDN '{}'",
97                                     device.getIdentity().getUdn().getIdentifierString());
98                             return new ThingUID(THING_TYPE_INSIGHT,
99                                     device.getIdentity().getUdn().getIdentifierString());
100                         }
101                         if (device.getDetails().getModelDetails().getModelName().toLowerCase()
102                                 .startsWith("lightswitch")) {
103                             logger.debug("Discovered a WeMo Lightswitch thing with UDN '{}'",
104                                     device.getIdentity().getUdn().getIdentifierString());
105                             return new ThingUID(THING_TYPE_LIGHTSWITCH,
106                                     device.getIdentity().getUdn().getIdentifierString());
107                         }
108                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("motion")) {
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("sensor")) {
114                             logger.debug("Discovered a WeMo Motion thing with UDN '{}'",
115                                     device.getIdentity().getUdn().getIdentifierString());
116                             return new ThingUID(THING_TYPE_MOTION, device.getIdentity().getUdn().getIdentifierString());
117                         }
118                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("bridge")) {
119                             logger.debug("Discovered a WeMo Bridge thing with UDN '{}'",
120                                     device.getIdentity().getUdn().getIdentifierString());
121                             return new ThingUID(THING_TYPE_BRIDGE, device.getIdentity().getUdn().getIdentifierString());
122                         }
123                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("maker")) {
124                             logger.debug("Discovered a WeMo Maker thing with UDN '{}'",
125                                     device.getIdentity().getUdn().getIdentifierString());
126                             return new ThingUID(THING_TYPE_MAKER, device.getIdentity().getUdn().getIdentifierString());
127                         }
128                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("coffee")) {
129                             logger.debug("Discovered a WeMo Coffe Maker thing with UDN '{}'",
130                                     device.getIdentity().getUdn().getIdentifierString());
131                             return new ThingUID(THING_TYPE_COFFEE, device.getIdentity().getUdn().getIdentifierString());
132                         }
133                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("dimmer")) {
134                             logger.debug("Discovered a WeMo Dimmer Switch thing with UDN '{}'",
135                                     device.getIdentity().getUdn().getIdentifierString());
136                             return new ThingUID(THING_TYPE_DIMMER, device.getIdentity().getUdn().getIdentifierString());
137                         }
138                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("crockpot")) {
139                             logger.debug("Discovered a WeMo enabled Crockpot thing with UDN '{}'",
140                                     device.getIdentity().getUdn().getIdentifierString());
141                             return new ThingUID(THING_TYPE_CROCKPOT,
142                                     device.getIdentity().getUdn().getIdentifierString());
143                         }
144                         if (device.getDetails().getModelDetails().getModelName().toLowerCase()
145                                 .startsWith("airpurifier")) {
146                             logger.debug("Discovered a WeMo enabled Holmes Air Purifier thing with UDN '{}'",
147                                     device.getIdentity().getUdn().getIdentifierString());
148                             return new ThingUID(THING_TYPE_PURIFIER,
149                                     device.getIdentity().getUdn().getIdentifierString());
150                         }
151                         if (device.getDetails().getModelDetails().getModelName().toLowerCase()
152                                 .startsWith("humidifier")) {
153                             logger.debug("Discovered a WeMo enabled Holmes Humidifier thing with UDN '{}'",
154                                     device.getIdentity().getUdn().getIdentifierString());
155                             return new ThingUID(THING_TYPE_HUMIDIFIER,
156                                     device.getIdentity().getUdn().getIdentifierString());
157                         }
158                         if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("heater")) {
159                             logger.debug("Discovered a WeMo enabled Heater thing with UDN '{}'",
160                                     device.getIdentity().getUdn().getIdentifierString());
161                             return new ThingUID(THING_TYPE_HEATER, device.getIdentity().getUdn().getIdentifierString());
162                         }
163                     }
164                 }
165             }
166         }
167         return null;
168     }
169 }