2 * Copyright (c) 2010-2023 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.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;
35 * The {@link WemoDiscoveryParticipant} is responsible for discovering new and
36 * removed Wemo devices.
38 * @author Hans-Jörg Merk - Initial contribution
39 * @author Kai Kreuzer - some refactoring for performance and simplification
43 @Component(service = UpnpDiscoveryParticipant.class)
44 public class WemoDiscoveryParticipant implements UpnpDiscoveryParticipant {
46 private Logger logger = LoggerFactory.getLogger(WemoDiscoveryParticipant.class);
49 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
50 return WemoBindingConstants.SUPPORTED_THING_TYPES;
54 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
55 ThingUID uid = getThingUID(device);
57 Map<String, Object> properties = new HashMap<>(2);
58 String label = "WeMo Device";
60 label = device.getDetails().getFriendlyName();
61 } catch (Exception e) {
62 // ignore and use default label
64 properties.put(UDN, device.getIdentity().getUdn().getIdentifierString());
66 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label)
67 .withRepresentationProperty(UDN).build();
69 logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}'",
70 device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString());
79 public @Nullable ThingUID getThingUID(@Nullable RemoteDevice device) {
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());