From a66495a707a7cb4c91a7c5ffa99827d437b0e429 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Thu, 25 Jul 2024 01:16:41 -0600 Subject: [PATCH] [homekit] don't pre-fill enums from switches if metadata has enum values (#17142) this is especially important when linking a many-valued enum (such as CurrentHeatingCoolingState) to a switch, and you want to use a value beyond 0 and 1 (i.e. OFF (0) and COOL (2)). Signed-off-by: Cody Cutrer --- .../HomekitCharacteristicFactory.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitCharacteristicFactory.java b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitCharacteristicFactory.java index 851d0ac330..37fcede6c8 100644 --- a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitCharacteristicFactory.java +++ b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitCharacteristicFactory.java @@ -339,13 +339,24 @@ public class HomekitCharacteristicFactory { @Nullable T offEnumValue = null, onEnumValue = null; + var configuration = item.getConfiguration(); + boolean configurationDefinesEnumValues = false; + if (configuration != null && !configuration.isEmpty()) { + for (var k : klazz.getEnumConstants()) { + if (configuration.containsKey(k.toString())) { + configurationDefinesEnumValues = true; + break; + } + } + } + for (var k : klazz.getEnumConstants()) { if (numberType) { int code = k.getCode(); - if ((switchType || contactType) && code == 0) { + if ((switchType || contactType) && code == 0 && !configurationDefinesEnumValues) { map.put(k, inverted ? onValue : offValue); offEnumValue = k; - } else if ((switchType || contactType) && code == 1) { + } else if ((switchType || contactType) && code == 1 && !configurationDefinesEnumValues) { map.put(k, inverted ? offValue : onValue); onEnumValue = k; } else if (percentType && code == 0) { @@ -359,8 +370,7 @@ public class HomekitCharacteristicFactory { map.put(k, k.toString()); } } - var configuration = item.getConfiguration(); - if (configuration != null) { + if (configuration != null && !configuration.isEmpty()) { map.forEach((k, current_value) -> { final Object newValue = configuration.get(k.toString()); if (newValue instanceof String || newValue instanceof Number) { -- 2.47.3