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.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 String serialNumber = device.getDetails().getSerialNumber();
61 DiscoveryResult result;
62 if (serialNumber != null && !serialNumber.isBlank()) {
63 properties.put(PROPERTY_SERIAL_NUMBER, serialNumber.toLowerCase());
65 result = DiscoveryResultBuilder.create(uid).withProperties(properties)
66 .withLabel(device.getDetails().getFriendlyName())
67 .withRepresentationProperty(PROPERTY_SERIAL_NUMBER).build();
69 result = DiscoveryResultBuilder.create(uid).withProperties(properties)
70 .withLabel(device.getDetails().getFriendlyName()).build();
79 public @Nullable ThingUID getThingUID(RemoteDevice device) {
80 DeviceDetails details = device.getDetails();
81 if (details != null) {
82 ModelDetails modelDetails = details.getModelDetails();
83 String serialNumber = details.getSerialNumber();
84 if (modelDetails != null && serialNumber != null && !serialNumber.isBlank()) {
85 String modelName = modelDetails.getModelName();
86 if (modelName != null) {
87 if (modelName.startsWith("Philips hue bridge")) {
88 return new ThingUID(THING_TYPE_BRIDGE, serialNumber.toLowerCase());