[homekit] Sort optional characteristics before adding them to the service (#18329)

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 <cody@cutrer.us>
pull/18333/head
Cody Cutrer 2025-02-26 13:33:36 -07:00 committed by GitHub
parent c57f833e31
commit 80d64ccd06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 38 additions and 38 deletions

View File

@ -242,7 +242,8 @@ public abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
services.add(service); services.add(service);
var serviceClass = service.getClass(); var serviceClass = service.getClass();
rawCharacteristics.values().forEach(characteristic -> { rawCharacteristics.values().stream().sorted((lhs, rhs) -> lhs.getType().compareTo(rhs.getType()))
.forEach(characteristic -> {
// belongs on the accessory information service // belongs on the accessory information service
if (characteristic.getClass() == NameCharacteristic.class) { if (characteristic.getClass() == NameCharacteristic.class) {
return; return;
@ -565,8 +566,7 @@ public abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
serviceBuilder.add("type", service.getType()); serviceBuilder.add("type", service.getType());
var characteristics = Json.createArrayBuilder(); var characteristics = Json.createArrayBuilder();
service.getCharacteristics().stream().sorted((l, r) -> l.getClass().getName().compareTo(r.getClass().getName())) service.getCharacteristics().forEach(c -> {
.forEach(c -> {
try { try {
var cJson = c.toJson(0).get(); var cJson = c.toJson(0).get();
var cBuilder = Json.createObjectBuilder(); var cBuilder = Json.createObjectBuilder();