]> git.basschouten.com Git - openhab-addons.git/commitdiff
[homekit] don't pre-fill enums from switches if metadata has enum values (#17142)
authorCody Cutrer <cody@cutrer.us>
Thu, 25 Jul 2024 07:16:41 +0000 (01:16 -0600)
committerGitHub <noreply@github.com>
Thu, 25 Jul 2024 07:16:41 +0000 (09:16 +0200)
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 <cody@cutrer.us>
bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitCharacteristicFactory.java

index 851d0ac330464133735bc197fafd449cfbb79156..37fcede6c8ee1758b86bffb795bd86af5ca42d3f 100644 (file)
@@ -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) {