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