2 * Copyright (c) 2010-2022 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().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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());
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());