2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.wemo.internal.discovery;
15 import static org.openhab.binding.wemo.internal.WemoBindingConstants.*;
17 import java.util.HashMap;
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;
35 * The {@link WemoDiscoveryParticipant} is responsible for discovering new and
36 * removed Wemo devices. It uses the central {@link UpnpDiscoveryService}.
38 * @author Hans-Jörg Merk - Initial contribution
39 * @author Kai Kreuzer - some refactoring for performance and simplification
42 @Component(service = UpnpDiscoveryParticipant.class)
43 public class WemoDiscoveryParticipant implements UpnpDiscoveryParticipant {
45 private Logger logger = LoggerFactory.getLogger(WemoDiscoveryParticipant.class);
48 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49 return WemoBindingConstants.SUPPORTED_THING_TYPES;
53 public DiscoveryResult createResult(RemoteDevice device) {
54 ThingUID uid = getThingUID(device);
56 Map<String, Object> properties = new HashMap<>(2);
57 String label = "WeMo Device";
59 label = device.getDetails().getFriendlyName();
60 } catch (Exception e) {
61 // ignore and use default label
63 properties.put(UDN, device.getIdentity().getUdn().getIdentifierString());
65 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label)
66 .withRepresentationProperty(UDN).build();
68 logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}'",
69 device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString());
78 public ThingUID getThingUID(@Nullable RemoteDevice device) {
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());