Skip to content

Commit d3e48f7

Browse files
committed
[homekit] Sort optional characteristics before adding them to the service
So that they'll always be added in a consistent order. Otherwise we might accidentally change the order (and thus the IID) of a characteristic, but not actually detect that it's different and signal Home to reload accessory structures. Signed-off-by: Cody Cutrer <[email protected]>
1 parent 6a1a41a commit d3e48f7

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/AbstractHomekitAccessoryImpl.java

+14-13
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,20 @@ public void addService(Service service) {
242242
services.add(service);
243243

244244
var serviceClass = service.getClass();
245-
rawCharacteristics.values().forEach(characteristic -> {
246-
// belongs on the accessory information service
247-
if (characteristic.getClass() == NameCharacteristic.class) {
248-
return;
249-
}
250-
try {
251-
// if the service supports adding this characteristic as optional, add it!
252-
serviceClass.getMethod("addOptionalCharacteristic", characteristic.getClass()).invoke(service,
253-
characteristic);
254-
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
255-
// the service doesn't support this optional characteristic; ignore it
256-
}
257-
});
245+
rawCharacteristics.values().stream().sorted((lhs, rhs) -> lhs.getType().compareTo(rhs.getType()))
246+
.forEach(characteristic -> {
247+
// belongs on the accessory information service
248+
if (characteristic.getClass() == NameCharacteristic.class) {
249+
return;
250+
}
251+
try {
252+
// if the service supports adding this characteristic as optional, add it!
253+
serviceClass.getMethod("addOptionalCharacteristic", characteristic.getClass()).invoke(service,
254+
characteristic);
255+
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
256+
// the service doesn't support this optional characteristic; ignore it
257+
}
258+
});
258259
}
259260

260261
protected HomekitAccessoryUpdater getUpdater() {

0 commit comments

Comments
 (0)