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.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.config.discovery.upnp.internal.UpnpDiscoveryService;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link WemoDiscoveryParticipant} is responsible for discovering new and
37 * removed Wemo devices. It uses the central {@link UpnpDiscoveryService}.
39 * @author Hans-Jörg Merk - Initial contribution
40 * @author Kai Kreuzer - some refactoring for performance and simplification
44 @Component(service = UpnpDiscoveryParticipant.class)
45 public class WemoDiscoveryParticipant implements UpnpDiscoveryParticipant {
47 private Logger logger = LoggerFactory.getLogger(WemoDiscoveryParticipant.class);
50 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
51 return WemoBindingConstants.SUPPORTED_THING_TYPES;
55 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
56 ThingUID uid = getThingUID(device);
58 Map<String, Object> properties = new HashMap<>(2);
59 String label = "WeMo Device";
61 label = device.getDetails().getFriendlyName();
62 } catch (Exception e) {
63 // ignore and use default label
65 properties.put(UDN, device.getIdentity().getUdn().getIdentifierString());
67 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label)
68 .withRepresentationProperty(UDN).build();
70 logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}'",
71 device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString());
80 public @Nullable ThingUID getThingUID(@Nullable RemoteDevice device) {
82 if (device.getDetails().getManufacturerDetails().getManufacturer() != null) {
83 if (device.getDetails().getManufacturerDetails().getManufacturer().toUpperCase().contains("BELKIN")) {
84 if (device.getDetails().getModelDetails().getModelName() != null) {
85 if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("socket")) {
86 logger.debug("Discovered a WeMo Socket thing with UDN '{}'",
87 device.getIdentity().getUdn().getIdentifierString());
88 return new ThingUID(THING_TYPE_SOCKET, device.getIdentity().getUdn().getIdentifierString());
90 if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("insight")) {
91 logger.debug("Discovered a WeMo Insight thing with UDN '{}'",
92 device.getIdentity().getUdn().getIdentifierString());
93 return new ThingUID(THING_TYPE_INSIGHT,
94 device.getIdentity().getUdn().getIdentifierString());
96 if (device.getDetails().getModelDetails().getModelName().toLowerCase()
97 .startsWith("lightswitch")) {
98 logger.debug("Discovered a WeMo Lightswitch thing with UDN '{}'",
99 device.getIdentity().getUdn().getIdentifierString());
100 return new ThingUID(THING_TYPE_LIGHTSWITCH,
101 device.getIdentity().getUdn().getIdentifierString());
103 if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("motion")) {
104 logger.debug("Discovered a WeMo Motion thing with UDN '{}'",
105 device.getIdentity().getUdn().getIdentifierString());
106 return new ThingUID(THING_TYPE_MOTION, device.getIdentity().getUdn().getIdentifierString());
108 if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("sensor")) {
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("bridge")) {
114 logger.debug("Discovered a WeMo Bridge thing with UDN '{}'",
115 device.getIdentity().getUdn().getIdentifierString());
116 return new ThingUID(THING_TYPE_BRIDGE, device.getIdentity().getUdn().getIdentifierString());
118 if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("maker")) {
119 logger.debug("Discovered a WeMo Maker thing with UDN '{}'",
120 device.getIdentity().getUdn().getIdentifierString());
121 return new ThingUID(THING_TYPE_MAKER, device.getIdentity().getUdn().getIdentifierString());
123 if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("coffee")) {
124 logger.debug("Discovered a WeMo Coffe Maker thing with UDN '{}'",
125 device.getIdentity().getUdn().getIdentifierString());
126 return new ThingUID(THING_TYPE_COFFEE, device.getIdentity().getUdn().getIdentifierString());
128 if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("dimmer")) {
129 logger.debug("Discovered a WeMo Dimmer Switch thing with UDN '{}'",
130 device.getIdentity().getUdn().getIdentifierString());
131 return new ThingUID(THING_TYPE_DIMMER, device.getIdentity().getUdn().getIdentifierString());
133 if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("crockpot")) {
134 logger.debug("Discovered a WeMo enabled Crockpot thing with UDN '{}'",
135 device.getIdentity().getUdn().getIdentifierString());
136 return new ThingUID(THING_TYPE_CROCKPOT,
137 device.getIdentity().getUdn().getIdentifierString());
139 if (device.getDetails().getModelDetails().getModelName().toLowerCase()
140 .startsWith("airpurifier")) {
141 logger.debug("Discovered a WeMo enabled Holmes Air Purifier thing with UDN '{}'",
142 device.getIdentity().getUdn().getIdentifierString());
143 return new ThingUID(THING_TYPE_PURIFIER,
144 device.getIdentity().getUdn().getIdentifierString());
146 if (device.getDetails().getModelDetails().getModelName().toLowerCase()
147 .startsWith("humidifier")) {
148 logger.debug("Discovered a WeMo enabled Holmes Humidifier thing with UDN '{}'",
149 device.getIdentity().getUdn().getIdentifierString());
150 return new ThingUID(THING_TYPE_HUMIDIFIER,
151 device.getIdentity().getUdn().getIdentifierString());
153 if (device.getDetails().getModelDetails().getModelName().toLowerCase().startsWith("heater")) {
154 logger.debug("Discovered a WeMo enabled Heater thing with UDN '{}'",
155 device.getIdentity().getUdn().getIdentifierString());
156 return new ThingUID(THING_TYPE_HEATER, device.getIdentity().getUdn().getIdentifierString());