Skip to content

Commit

Permalink
[hueemulation] Change uniqueid to make the earlier octets more unique…
Browse files Browse the repository at this point in the history
… to fix Alexa … (openhab#17772)

* Change uniqueid to make the earlier octects more unique to fix Alexa discovery.

Signed-off-by: Mike Major <[email protected]>
  • Loading branch information
MikeJMajor authored Nov 21, 2024
1 parent 1825a4d commit a5c6210
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ public class ConfigStore {

private int highestAssignedHueID = 1;

private String hueIDPrefix = "";

public ConfigStore() {
scheduler = ThreadPoolManager.getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON);
}
Expand Down Expand Up @@ -236,8 +234,6 @@ public void modified(Map<String, Object> properties) {
ds.config.bridgeid = ds.config.bridgeid.substring(0, 12);
}

hueIDPrefix = getHueIDPrefixFromUUID(config.uuid);

if (config.permanentV1bridge) {
ds.config.makeV1bridge();
}
Expand All @@ -263,31 +259,6 @@ private String getConfiguredHostAddress(InetAddress configuredAddress) {
}
}

/**
* Get the prefix used to create a unique id
*
* @param uuid The uuid
* @return The prefix in the format of AA:BB:CC:DD:EE:FF:00:11 if uuid is a valid UUID, otherwise uuid is returned.
*/
private String getHueIDPrefixFromUUID(final String uuid) {
// Hue API example of a unique id is AA:BB:CC:DD:EE:FF:00:11-XX
// 00:11-XX is generated from the item.
String prefix = uuid;
try {
// Generate prefix if uuid is a randomly generated UUID
if (UUID.fromString(uuid).version() == 4) {
final StringBuilder sb = new StringBuilder(17);
sb.append(uuid, 0, 2).append(":").append(uuid, 2, 4).append(":").append(uuid, 4, 6).append(":")
.append(uuid, 6, 8).append(":").append(uuid, 9, 11).append(":").append(uuid, 11, 13);
prefix = sb.toString().toUpperCase();
}
} catch (final IllegalArgumentException e) {
// uuid is not a valid UUID
}

return prefix;
}

@Deactivate
public void deactive(int reason) {
ScheduledFuture<?> future = pairingOffFuture;
Expand Down Expand Up @@ -351,17 +322,26 @@ public String mapItemUIDtoHueID(Item item) {
* @return The unique id
*/
public String getHueUniqueId(final String hueId) {
// From the Hue API:
// Format: AA:BB:CC:DD:EE:FF:00:11-XX
// Content: Device MAC + unique endpoint id
// Example: 00:17:88:01:00:bd:c7:b9-0b
// Using the item's hueID for every three octets ensures both the MAC and
// endpoint are unique for each item which seems important for Alexa discovery.
String unique;

try {
final String id = String.format("%06X", Integer.valueOf(hueId));
final String id = String.format("%06x", Integer.valueOf(hueId));
final StringBuilder sb = new StringBuilder(26);
sb.append(hueIDPrefix).append(":").append(id, 0, 2).append(":").append(id, 2, 4).append("-").append(id, 4,
6);

sb.append(id, 0, 2).append(":").append(id, 2, 4).append(":").append(id, 4, 6).append(":")//
.append(id, 0, 2).append(":").append(id, 2, 4).append(":").append(id, 4, 6).append(":")//
.append(id, 0, 2).append(":").append(id, 2, 4).append("-").append(id, 4, 6);

unique = sb.toString();
} catch (final NumberFormatException | IllegalFormatException e) {
// Use the hueId as is
unique = hueIDPrefix + "-" + hueId;
unique = hueId;
}

return unique;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void uniqueIdForLargeHueID() {
HueLightEntry device = cs.ds.lights.get(hueID);
assertThat(device.item, is(item));
assertThat(device.state, is(instanceOf(HueStatePlug.class)));
assertThat(device.uniqueid, CoreMatchers.is("A6:68:DC:9B:71:72:00:00-FF"));
assertThat(device.uniqueid, CoreMatchers.is("00:00:ff:00:00:ff:00:00-ff"));

item = new SwitchItem("switch2");
item.setCategory("Light");
Expand All @@ -146,6 +146,6 @@ public void uniqueIdForLargeHueID() {
device = cs.ds.lights.get(hueID);
assertThat(device.item, is(item));
assertThat(device.state, is(instanceOf(HueStatePlug.class)));
assertThat(device.uniqueid, CoreMatchers.is("A6:68:DC:9B:71:72:03:E8-00"));
assertThat(device.uniqueid, CoreMatchers.is("03:e8:00:03:e8:00:03:e8-00"));
}
}

0 comments on commit a5c6210

Please sign in to comment.