Fix NUPnP discovery after firmware change (#15622)

Fixes #15598

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2023-09-23 19:05:32 +02:00 committed by GitHub
parent e562c22a2f
commit c9e6417754
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View File

@ -15,6 +15,7 @@ package org.openhab.binding.hue.internal.discovery;
import static org.openhab.binding.hue.internal.HueBindingConstants.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@ -55,7 +56,7 @@ public class HueBridgeNupnpDiscovery extends AbstractDiscoveryService {
protected static final String BRIDGE_INDICATOR = "fffe";
private static final String MODEL_NAME_PHILIPS_HUE = "\"name\":\"Philips hue\"";
private static final String[] MODEL_NAME_PHILIPS_HUE = { "\"name\":\"Hue Bridge\"", "\"name\":\"Philips hue\"" };
private static final String DISCOVERY_URL = "https://discovery.meethue.com/";
private static final String CONFIG_URL_PATTERN = "http://%s/api/0/config";
private static final int REQUEST_TIMEOUT = 5000;
@ -156,7 +157,7 @@ public class HueBridgeNupnpDiscovery extends AbstractDiscoveryService {
logger.debug("Bridge not discovered: Failure accessing description file for ip: {}", host);
return false;
}
if (description == null || !description.contains(MODEL_NAME_PHILIPS_HUE)) {
if (description == null || !Arrays.stream(MODEL_NAME_PHILIPS_HUE).anyMatch(description::contains)) {
logger.debug("Bridge not discovered: Description does not contain the model name: {}", description);
return false;
}

View File

@ -55,15 +55,20 @@ public class HueBridgeNupnpDiscoveryOSGITest extends JavaOSGiTest {
private static final ThingTypeUID BRIDGE_THING_TYPE_UID = new ThingTypeUID("hue", "bridge");
private static final String IP1 = "192.168.31.17";
private static final String IP2 = "192.168.30.28";
private static final String IP3 = "192.168.30.29";
private static final String SN1 = "001788fffe20057f";
private static final String SN2 = "001788fffe141b41";
private static final String SN3 = "001788fffe141b42";
private static final ThingUID BRIDGE_THING_UID_1 = new ThingUID(BRIDGE_THING_TYPE_UID, SN1);
private static final ThingUID BRIDGE_THING_UID_2 = new ThingUID(BRIDGE_THING_TYPE_UID, SN2);
private static final ThingUID BRIDGE_THING_UID_3 = new ThingUID(BRIDGE_THING_TYPE_UID, SN3);
private final String validBridgeDiscoveryResult = "[{\"id\":\"" + SN1 + "\",\"internalipaddress\":" + IP1
+ "},{\"id\":\"" + SN2 + "\",\"internalipaddress\":" + IP2 + "}]";
+ "},{\"id\":\"" + SN2 + "\",\"internalipaddress\":" + IP2 + "},{\"id\":\"" + SN3
+ "\",\"internalipaddress\":" + IP3 + "}]";
private @Nullable String discoveryResult;
private String expBridgeDescription = "{\"name\":\"Philips hue\",\"datastoreversion\":\"149\",\"swversion\":\"1957113050\",\"apiversion\":\"1.57.0\",\"mac\":\"00:11:22:33:44\",\"bridgeid\":\"$SN\",\"factorynew\":false,\"replacesbridgeid\":null,\"modelid\":\"BSB002\",\"starterkitid\":\"\"}";
private String expBridgeDescription1 = "{\"name\":\"Philips hue\",\"datastoreversion\":\"149\",\"swversion\":\"1957113050\",\"apiversion\":\"1.57.0\",\"mac\":\"00:11:22:33:44\",\"bridgeid\":\"$SN\",\"factorynew\":false,\"replacesbridgeid\":null,\"modelid\":\"BSB002\",\"starterkitid\":\"\"}";
private String expBridgeDescription2 = "{\"name\":\"Hue Bridge\",\"datastoreversion\":\"161\",\"swversion\":\"1959194040\",\"apiversion\":\"1.59.0\",\"mac\":\"00:11:22:33:44\",\"bridgeid\":\"$SN\",\"factorynew\":false,\"replacesbridgeid\":null,\"modelid\":\"BSB002\",\"starterkitid\":\"\"}";
private void checkDiscoveryResult(@Nullable DiscoveryResult result, String expIp, String expSn) {
if (result == null) {
@ -98,9 +103,11 @@ public class HueBridgeNupnpDiscoveryOSGITest extends JavaOSGiTest {
if (url.contains("meethue")) {
return discoveryResult;
} else if (url.contains(IP1)) {
return expBridgeDescription.replaceAll("$SN", SN1);
return expBridgeDescription1.replaceAll("$SN", SN1);
} else if (url.contains(IP2)) {
return expBridgeDescription.replaceAll("$SN", SN2);
return expBridgeDescription1.replaceAll("$SN", SN2);
} else if (url.contains(IP3)) {
return expBridgeDescription2.replaceAll("$SN", SN3);
}
throw new IOException();
}
@ -162,20 +169,24 @@ public class HueBridgeNupnpDiscoveryOSGITest extends JavaOSGiTest {
sut.startScan();
waitForAssert(() -> {
assertThat(results.size(), is(2));
assertThat(results.size(), is(3));
assertThat(results.get(BRIDGE_THING_UID_1), is(notNullValue()));
checkDiscoveryResult(results.get(BRIDGE_THING_UID_1), IP1, SN1);
assertThat(results.get(BRIDGE_THING_UID_2), is(notNullValue()));
checkDiscoveryResult(results.get(BRIDGE_THING_UID_2), IP2, SN2);
assertThat(results.get(BRIDGE_THING_UID_3), is(notNullValue()));
checkDiscoveryResult(results.get(BRIDGE_THING_UID_3), IP3, SN3);
final List<DiscoveryResult> inboxResults = inbox.stream().filter(forThingTypeUID(BRIDGE_THING_TYPE_UID))
.collect(Collectors.toList());
assertTrue(inboxResults.size() >= 2);
assertTrue(inboxResults.size() >= 3);
assertThat(inboxResults.stream().filter(result -> result.getThingUID().equals(BRIDGE_THING_UID_1))
.findFirst().orElse(null), is(notNullValue()));
assertThat(inboxResults.stream().filter(result -> result.getThingUID().equals(BRIDGE_THING_UID_2))
.findFirst().orElse(null), is(notNullValue()));
assertThat(inboxResults.stream().filter(result -> result.getThingUID().equals(BRIDGE_THING_UID_3))
.findFirst().orElse(null), is(notNullValue()));
});
}
@ -259,7 +270,7 @@ public class HueBridgeNupnpDiscoveryOSGITest extends JavaOSGiTest {
});
// invalid bridge description
expBridgeDescription = "";
expBridgeDescription1 = "";
discoveryResult = "[{\"id\":\"001788fffe20057f\",\"internalipaddress\":" + IP1 + "}]";
sut.startScan();
waitForAssert(() -> {