]> git.basschouten.com Git - openhab-addons.git/commitdiff
[homematic] Fix invalid default values ending up in the thing type (#12437)
authorFlole998 <Flole998@users.noreply.github.com>
Mon, 7 Mar 2022 20:51:17 +0000 (21:51 +0100)
committerGitHub <noreply@github.com>
Mon, 7 Mar 2022 20:51:17 +0000 (21:51 +0100)
Sometimes invalid default values ended up in the default value for a channel of a thing type. Initializing the thing would fail completely complaining that it is not an allowed option. This patch makes sure those values are actually valid and attempts to correct them if they are invalid.

Signed-off-by: Flole <flole@flole.de>
bundles/org.openhab.binding.homematic/src/main/java/org/openhab/binding/homematic/internal/type/HomematicTypeGeneratorImpl.java

index 0ea073789b9ddbf7dd0b75298008887f9be1d7e1..cf0ac66c0c9ff1baaa151cad4e090d5fecd4e720 100644 (file)
@@ -342,6 +342,33 @@ public class HomematicTypeGeneratorImpl implements HomematicTypeGenerator {
                                     }
                                 });
                         builder.withOptions(options);
+                        if (dp.isEnumType()) {
+                            logger.trace("Checking if default option {} is valid",
+                                    Objects.toString(dp.getDefaultValue(), ""));
+                            boolean needsChange = true;
+                            for (ParameterOption option : options) {
+                                if (option.getValue().equals(Objects.toString(dp.getDefaultValue(), ""))) {
+                                    needsChange = false;
+                                    break;
+                                }
+                            }
+                            if (needsChange) {
+                                String defStr = Objects.toString(dp.getDefaultValue(), "0");
+                                if (defStr == null) {
+                                    defStr = "0";
+                                }
+                                int offset = Integer.parseInt(defStr);
+                                if (offset >= 0 && offset < options.size()) {
+                                    ParameterOption defaultOption = options.get(offset);
+                                    logger.trace("Changing default option to {} (offset {})", defaultOption, defStr);
+                                    builder.withDefault(defaultOption.getValue());
+                                } else if (options.size() > 0) {
+                                    ParameterOption defaultOption = options.get(0);
+                                    logger.trace("Changing default option to {} (first value)", defaultOption);
+                                    builder.withDefault(defaultOption.getValue());
+                                }
+                            }
+                        }
                     }
 
                     if (dp.isNumberType()) {