2 * Copyright (c) 2010-2020 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.hue.internal.discovery;
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
16 import static org.openhab.core.thing.Thing.PROPERTY_SERIAL_NUMBER;
18 import java.util.Collections;
19 import java.util.HashMap;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.jupnp.model.meta.DeviceDetails;
26 import org.jupnp.model.meta.ModelDetails;
27 import org.jupnp.model.meta.RemoteDevice;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
31 import org.openhab.core.config.discovery.upnp.internal.UpnpDiscoveryService;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.osgi.service.component.annotations.Component;
37 * The {@link HueBridgeDiscoveryParticipant} is responsible for discovering new and
38 * removed hue bridges. It uses the central {@link UpnpDiscoveryService}.
40 * @author Kai Kreuzer - Initial contribution
41 * @author Thomas Höfer - Added representation
44 @Component(service = UpnpDiscoveryParticipant.class)
45 public class HueBridgeDiscoveryParticipant implements UpnpDiscoveryParticipant {
48 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49 return Collections.singleton(THING_TYPE_BRIDGE);
53 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
54 ThingUID uid = getThingUID(device);
56 Map<String, Object> properties = new HashMap<>();
57 properties.put(HOST, device.getDetails().getBaseURL().getHost());
58 properties.put(PORT, device.getDetails().getBaseURL().getPort());
59 properties.put(PROTOCOL, device.getDetails().getBaseURL().getProtocol());
60 properties.put(PROPERTY_SERIAL_NUMBER, device.getDetails().getSerialNumber());
62 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
63 .withLabel(device.getDetails().getFriendlyName()).withRepresentationProperty(PROPERTY_SERIAL_NUMBER)
72 public @Nullable ThingUID getThingUID(RemoteDevice device) {
73 DeviceDetails details = device.getDetails();
74 if (details != null) {
75 ModelDetails modelDetails = details.getModelDetails();
76 if (modelDetails != null) {
77 String modelName = modelDetails.getModelName();
78 if (modelName != null) {
79 if (modelName.startsWith("Philips hue bridge")) {
80 return new ThingUID(THING_TYPE_BRIDGE, details.getSerialNumber());