mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[hue] Restore UPnP discovery for old bridges (#14914)
* [hue] Restore UPnP discovery for old bridges Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
parent
26a52a1540
commit
fb16e90168
@ -5,6 +5,7 @@
|
||||
<feature name="openhab-binding-hue" description="Hue Binding" version="${project.version}">
|
||||
<feature>openhab-runtime-base</feature>
|
||||
<feature>openhab-transport-mdns</feature>
|
||||
<feature>openhab-transport-upnp</feature>
|
||||
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.hue/${project.version}</bundle>
|
||||
</feature>
|
||||
</features>
|
||||
|
@ -89,6 +89,8 @@ public class HueBindingConstants {
|
||||
|
||||
// Bridge config properties
|
||||
public static final String HOST = "ipAddress";
|
||||
public static final String PORT = "port";
|
||||
public static final String PROTOCOL = "protocol";
|
||||
public static final String USER_NAME = "userName";
|
||||
|
||||
// Thing configuration properties
|
||||
|
@ -54,8 +54,6 @@ public class HueBridgeMDNSDiscoveryParticipant implements MDNSDiscoveryParticipa
|
||||
private static final String MDNS_PROPERTY_BRIDGE_ID = "bridgeid";
|
||||
private static final String MDNS_PROPERTY_MODEL_ID = "modelid";
|
||||
|
||||
private static final String CONFIG_PROPERTY_REMOVAL_GRACE_PERIOD = "removalGracePeriod";
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(HueBridgeMDNSDiscoveryParticipant.class);
|
||||
|
||||
private long removalGracePeriod = 0L;
|
||||
@ -79,12 +77,12 @@ public class HueBridgeMDNSDiscoveryParticipant implements MDNSDiscoveryParticipa
|
||||
if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
|
||||
isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
|
||||
}
|
||||
String removalGracePeriodPropertyValue = (String) properties.get(CONFIG_PROPERTY_REMOVAL_GRACE_PERIOD);
|
||||
String removalGracePeriodPropertyValue = (String) properties.get(REMOVAL_GRACE_PERIOD);
|
||||
if (removalGracePeriodPropertyValue != null && !removalGracePeriodPropertyValue.isBlank()) {
|
||||
try {
|
||||
removalGracePeriod = Long.parseLong(removalGracePeriodPropertyValue);
|
||||
} catch (NumberFormatException e) {
|
||||
logger.warn("Configuration property '{}' has invalid value: {}", CONFIG_PROPERTY_REMOVAL_GRACE_PERIOD,
|
||||
logger.warn("Configuration property '{}' has invalid value: {}", REMOVAL_GRACE_PERIOD,
|
||||
removalGracePeriodPropertyValue);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,170 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2023 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.hue.internal.discovery;
|
||||
|
||||
import static org.openhab.binding.hue.internal.HueBindingConstants.*;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.Dictionary;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.jupnp.model.meta.DeviceDetails;
|
||||
import org.jupnp.model.meta.ModelDetails;
|
||||
import org.jupnp.model.meta.RemoteDevice;
|
||||
import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
|
||||
import org.openhab.core.config.discovery.DiscoveryResult;
|
||||
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
|
||||
import org.openhab.core.config.discovery.DiscoveryService;
|
||||
import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
|
||||
import org.openhab.core.config.discovery.upnp.internal.UpnpDiscoveryService;
|
||||
import org.openhab.core.thing.Thing;
|
||||
import org.openhab.core.thing.ThingTypeUID;
|
||||
import org.openhab.core.thing.ThingUID;
|
||||
import org.osgi.service.component.ComponentContext;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Modified;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* The {@link HueBridgeUPNPDiscoveryParticipant} is responsible for discovering new and removed Hue Bridges. It uses the
|
||||
* central {@link UpnpDiscoveryService}.
|
||||
*
|
||||
* The discovery through UPnP was replaced by mDNS discovery for recent bridges (V2).
|
||||
* For old bridges (V1), the UPnP discovery is still required (as mDNS is not implemented).
|
||||
* This class allows discovering only old bridges using UPnP.
|
||||
*
|
||||
* @author Laurent Garnier - Initial contribution
|
||||
*/
|
||||
@Component(configurationPid = "discovery.hue")
|
||||
@NonNullByDefault
|
||||
public class HueBridgeUPNPDiscoveryParticipant implements UpnpDiscoveryParticipant {
|
||||
|
||||
private static final String EXPECTED_MODEL_NAME_PREFIX = "Philips hue bridge";
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(HueBridgeUPNPDiscoveryParticipant.class);
|
||||
|
||||
private long removalGracePeriod = 50L;
|
||||
|
||||
private boolean isAutoDiscoveryEnabled = true;
|
||||
|
||||
@Activate
|
||||
protected void activate(ComponentContext componentContext) {
|
||||
activateOrModifyService(componentContext);
|
||||
}
|
||||
|
||||
@Modified
|
||||
protected void modified(ComponentContext componentContext) {
|
||||
activateOrModifyService(componentContext);
|
||||
}
|
||||
|
||||
private void activateOrModifyService(ComponentContext componentContext) {
|
||||
Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
|
||||
String autoDiscoveryPropertyValue = (String) properties
|
||||
.get(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY);
|
||||
if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
|
||||
isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
|
||||
}
|
||||
String removalGracePeriodPropertyValue = (String) properties.get(REMOVAL_GRACE_PERIOD);
|
||||
if (removalGracePeriodPropertyValue != null && !removalGracePeriodPropertyValue.isBlank()) {
|
||||
try {
|
||||
removalGracePeriod = Long.parseLong(removalGracePeriodPropertyValue);
|
||||
} catch (NumberFormatException e) {
|
||||
logger.warn("Configuration property '{}' has invalid value: {}", REMOVAL_GRACE_PERIOD,
|
||||
removalGracePeriodPropertyValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
|
||||
return HueBridgeHandler.SUPPORTED_THING_TYPES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable DiscoveryResult createResult(RemoteDevice device) {
|
||||
if (!isAutoDiscoveryEnabled) {
|
||||
return null;
|
||||
}
|
||||
DeviceDetails details = device.getDetails();
|
||||
ThingUID uid = getThingUID(device);
|
||||
if (details == null || uid == null) {
|
||||
return null;
|
||||
}
|
||||
URL baseUrl = details.getBaseURL();
|
||||
String serialNumber = details.getSerialNumber();
|
||||
if (baseUrl == null || serialNumber == null || serialNumber.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String label = String.format(DISCOVERY_LABEL_PATTERN, baseUrl.getHost());
|
||||
String modelName = EXPECTED_MODEL_NAME_PREFIX;
|
||||
ModelDetails modelDetails = details.getModelDetails();
|
||||
if (modelDetails != null && modelDetails.getModelName() != null && modelDetails.getModelNumber() != null) {
|
||||
modelName = String.format("%s (%s)", modelDetails.getModelName(), modelDetails.getModelNumber());
|
||||
}
|
||||
return DiscoveryResultBuilder.create(uid) //
|
||||
.withProperties(Map.of( //
|
||||
HOST, baseUrl.getHost(), //
|
||||
PORT, baseUrl.getPort(), //
|
||||
PROTOCOL, baseUrl.getProtocol(), //
|
||||
Thing.PROPERTY_MODEL_ID, modelName, //
|
||||
Thing.PROPERTY_SERIAL_NUMBER, serialNumber.toLowerCase())) //
|
||||
.withLabel(label) //
|
||||
.withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER) //
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ThingUID getThingUID(RemoteDevice device) {
|
||||
DeviceDetails details = device.getDetails();
|
||||
if (details == null) {
|
||||
return null;
|
||||
}
|
||||
String serialNumber = details.getSerialNumber();
|
||||
ModelDetails modelDetails = details.getModelDetails();
|
||||
if (serialNumber == null || serialNumber.isBlank() || modelDetails == null) {
|
||||
return null;
|
||||
}
|
||||
String modelName = modelDetails.getModelName();
|
||||
// Model name has the format "Philips hue bridge <year>" with <year> being 2012
|
||||
// for a hue bridge V1 or 2015 for a hue bridge V2.
|
||||
if (modelName == null || !modelName.startsWith(EXPECTED_MODEL_NAME_PREFIX)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
Pattern pattern = Pattern.compile("\\d{4}");
|
||||
Matcher matcher = pattern.matcher(modelName);
|
||||
int year = Integer.parseInt(matcher.find() ? matcher.group() : "9999");
|
||||
// The bridge is ignored if year is greater or equal to 2015
|
||||
if (year >= 2015) {
|
||||
return null;
|
||||
}
|
||||
} catch (PatternSyntaxException | NumberFormatException e) {
|
||||
// No int value found, this bridge is ignored
|
||||
return null;
|
||||
}
|
||||
return new ThingUID(THING_TYPE_BRIDGE, serialNumber.toLowerCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getRemovalGracePeriodSeconds(RemoteDevice device) {
|
||||
return removalGracePeriod;
|
||||
}
|
||||
}
|
@ -36,6 +36,7 @@ Fragment-Host: org.openhab.binding.hue
|
||||
org.osgi.util.promise;version='[1.2.0,1.2.1)',\
|
||||
org.eclipse.jdt.annotation;version='[2.2.100,2.2.101)',\
|
||||
javax.jmdns;version='[3.5.8,3.5.9)',\
|
||||
org.jupnp;version='[2.7.0,2.7.1)',\
|
||||
ch.qos.logback.classic;version='[1.2.11,1.2.12)',\
|
||||
ch.qos.logback.core;version='[1.2.11,1.2.12)',\
|
||||
biz.aQute.tester.junit-platform;version='[6.4.0,6.4.1)',\
|
||||
@ -45,9 +46,11 @@ Fragment-Host: org.openhab.binding.hue
|
||||
org.openhab.core.config.core;version='[4.0.0,4.0.1)',\
|
||||
org.openhab.core.config.discovery;version='[4.0.0,4.0.1)',\
|
||||
org.openhab.core.config.discovery.mdns;version='[4.0.0,4.0.1)',\
|
||||
org.openhab.core.config.discovery.upnp;version='[4.0.0,4.0.1)',\
|
||||
org.openhab.core.io.console;version='[4.0.0,4.0.1)',\
|
||||
org.openhab.core.io.net;version='[4.0.0,4.0.1)',\
|
||||
org.openhab.core.io.transport.mdns;version='[4.0.0,4.0.1)',\
|
||||
org.openhab.core.io.transport.upnp;version='[4.0.0,4.0.1)',\
|
||||
org.openhab.core.test;version='[4.0.0,4.0.1)',\
|
||||
org.openhab.core.thing;version='[4.0.0,4.0.1)',\
|
||||
com.google.gson;version='[2.9.1,2.9.2)',\
|
||||
@ -68,6 +71,7 @@ Fragment-Host: org.openhab.binding.hue
|
||||
org.eclipse.jetty.websocket.client;version='[9.4.50,9.4.51)',\
|
||||
org.eclipse.jetty.websocket.common;version='[9.4.50,9.4.51)',\
|
||||
org.ops4j.pax.logging.pax-logging-api;version='[2.2.0,2.2.1)',\
|
||||
org.ops4j.pax.web.pax-web-api;version='[8.0.15,8.0.16)',\
|
||||
org.osgi.service.component;version='[1.5.0,1.5.1)',\
|
||||
junit-jupiter-api;version='[5.9.2,5.9.3)',\
|
||||
junit-jupiter-engine;version='[5.9.2,5.9.3)',\
|
||||
|
Loading…
Reference in New Issue
Block a user