[hueemulation] Change uniqueid to make the earlier octets more unique to fix Alexa … (#17772)

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

Signed-off-by: Mike Major <mike_j_major@hotmail.com>
This commit is contained in:
Mike Major 2024-11-21 19:04:05 +00:00 committed by GitHub
parent 1825a4ddd8
commit a5c62106c1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 35 deletions

View File

@ -122,8 +122,6 @@ public class ConfigStore {
private int highestAssignedHueID = 1; private int highestAssignedHueID = 1;
private String hueIDPrefix = "";
public ConfigStore() { public ConfigStore() {
scheduler = ThreadPoolManager.getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON); scheduler = ThreadPoolManager.getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON);
} }
@ -236,8 +234,6 @@ public class ConfigStore {
ds.config.bridgeid = ds.config.bridgeid.substring(0, 12); ds.config.bridgeid = ds.config.bridgeid.substring(0, 12);
} }
hueIDPrefix = getHueIDPrefixFromUUID(config.uuid);
if (config.permanentV1bridge) { if (config.permanentV1bridge) {
ds.config.makeV1bridge(); ds.config.makeV1bridge();
} }
@ -263,31 +259,6 @@ public class ConfigStore {
} }
} }
/**
* 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 @Deactivate
public void deactive(int reason) { public void deactive(int reason) {
ScheduledFuture<?> future = pairingOffFuture; ScheduledFuture<?> future = pairingOffFuture;
@ -351,17 +322,26 @@ public class ConfigStore {
* @return The unique id * @return The unique id
*/ */
public String getHueUniqueId(final String hueId) { 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; String unique;
try { 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); 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(); unique = sb.toString();
} catch (final NumberFormatException | IllegalFormatException e) { } catch (final NumberFormatException | IllegalFormatException e) {
// Use the hueId as is // Use the hueId as is
unique = hueIDPrefix + "-" + hueId; unique = hueId;
} }
return unique; return unique;

View File

@ -133,7 +133,7 @@ public class ItemUIDtoHueIDMappingTests {
HueLightEntry device = cs.ds.lights.get(hueID); HueLightEntry device = cs.ds.lights.get(hueID);
assertThat(device.item, is(item)); assertThat(device.item, is(item));
assertThat(device.state, is(instanceOf(HueStatePlug.class))); 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 = new SwitchItem("switch2");
item.setCategory("Light"); item.setCategory("Light");
@ -146,6 +146,6 @@ public class ItemUIDtoHueIDMappingTests {
device = cs.ds.lights.get(hueID); device = cs.ds.lights.get(hueID);
assertThat(device.item, is(item)); assertThat(device.item, is(item));
assertThat(device.state, is(instanceOf(HueStatePlug.class))); 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"));
} }
} }