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.hue.internal.discovery;
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
18 import java.util.Dictionary;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23 import java.util.regex.PatternSyntaxException;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.jupnp.model.meta.DeviceDetails;
28 import org.jupnp.model.meta.ModelDetails;
29 import org.jupnp.model.meta.RemoteDevice;
30 import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.config.discovery.DiscoveryService;
34 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
35 import org.openhab.core.config.discovery.upnp.internal.UpnpDiscoveryService;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.osgi.service.component.ComponentContext;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Modified;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * The {@link HueBridgeUPNPDiscoveryParticipant} is responsible for discovering new and removed Hue Bridges. It uses the
48 * central {@link UpnpDiscoveryService}.
50 * The discovery through UPnP was replaced by mDNS discovery for recent bridges (V2).
51 * For old bridges (V1), the UPnP discovery is still required (as mDNS is not implemented).
52 * This class allows discovering only old bridges using UPnP.
54 * @author Laurent Garnier - Initial contribution
56 @Component(configurationPid = "discovery.hue")
58 public class HueBridgeUPNPDiscoveryParticipant implements UpnpDiscoveryParticipant {
60 private static final String EXPECTED_MODEL_NAME_PREFIX = "Philips hue bridge";
62 private final Logger logger = LoggerFactory.getLogger(HueBridgeUPNPDiscoveryParticipant.class);
64 private long removalGracePeriod = 50L;
66 private boolean isAutoDiscoveryEnabled = true;
69 protected void activate(ComponentContext componentContext) {
70 activateOrModifyService(componentContext);
74 protected void modified(ComponentContext componentContext) {
75 activateOrModifyService(componentContext);
78 private void activateOrModifyService(ComponentContext componentContext) {
79 Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
80 String autoDiscoveryPropertyValue = (String) properties
81 .get(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY);
82 if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
83 isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
85 String removalGracePeriodPropertyValue = (String) properties.get(REMOVAL_GRACE_PERIOD);
86 if (removalGracePeriodPropertyValue != null && !removalGracePeriodPropertyValue.isBlank()) {
88 removalGracePeriod = Long.parseLong(removalGracePeriodPropertyValue);
89 } catch (NumberFormatException e) {
90 logger.warn("Configuration property '{}' has invalid value: {}", REMOVAL_GRACE_PERIOD,
91 removalGracePeriodPropertyValue);
97 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
98 return HueBridgeHandler.SUPPORTED_THING_TYPES;
102 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
103 if (!isAutoDiscoveryEnabled) {
106 DeviceDetails details = device.getDetails();
107 ThingUID uid = getThingUID(device);
108 if (details == null || uid == null) {
111 URL baseUrl = details.getBaseURL();
112 String serialNumber = details.getSerialNumber();
113 if (baseUrl == null || serialNumber == null || serialNumber.isBlank()) {
116 String label = String.format(DISCOVERY_LABEL_PATTERN, baseUrl.getHost());
117 String modelName = EXPECTED_MODEL_NAME_PREFIX;
118 ModelDetails modelDetails = details.getModelDetails();
119 if (modelDetails != null && modelDetails.getModelName() != null && modelDetails.getModelNumber() != null) {
120 modelName = String.format("%s (%s)", modelDetails.getModelName(), modelDetails.getModelNumber());
122 return DiscoveryResultBuilder.create(uid) //
123 .withProperties(Map.of( //
124 HOST, baseUrl.getHost(), //
125 PORT, baseUrl.getPort(), //
126 PROTOCOL, baseUrl.getProtocol(), //
127 Thing.PROPERTY_MODEL_ID, modelName, //
128 Thing.PROPERTY_SERIAL_NUMBER, serialNumber.toLowerCase())) //
130 .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER) //
135 public @Nullable ThingUID getThingUID(RemoteDevice device) {
136 DeviceDetails details = device.getDetails();
137 if (details == null) {
140 String serialNumber = details.getSerialNumber();
141 ModelDetails modelDetails = details.getModelDetails();
142 if (serialNumber == null || serialNumber.isBlank() || modelDetails == null) {
145 String modelName = modelDetails.getModelName();
146 // Model name has the format "Philips hue bridge <year>" with <year> being 2012
147 // for a hue bridge V1 or 2015 for a hue bridge V2.
148 if (modelName == null || !modelName.startsWith(EXPECTED_MODEL_NAME_PREFIX)) {
152 Pattern pattern = Pattern.compile("\\d{4}");
153 Matcher matcher = pattern.matcher(modelName);
154 int year = Integer.parseInt(matcher.find() ? matcher.group() : "9999");
155 // The bridge is ignored if year is greater or equal to 2015
159 } catch (PatternSyntaxException | NumberFormatException e) {
160 // No int value found, this bridge is ignored
163 return new ThingUID(THING_TYPE_BRIDGE, serialNumber.toLowerCase());
167 public long getRemovalGracePeriodSeconds(RemoteDevice device) {
168 return removalGracePeriod;