]> git.basschouten.com Git - openhab-addons.git/commitdiff
[homekit] Automatically assume valid enum values when linking to Switch/Contact ...
authorCody Cutrer <cody@cutrer.us>
Wed, 24 Jul 2024 07:22:42 +0000 (01:22 -0600)
committerGitHub <noreply@github.com>
Wed, 24 Jul 2024 07:22:42 +0000 (09:22 +0200)
this allows using switches and contacts for enums without additional configuration
if they already line up well with the first two values (such as they do for a
thermostat, when it represents a simple heater)

Signed-off-by: Cody Cutrer <cody@cutrer.us>
bundles/org.openhab.io.homekit/README.md
bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitChangeListener.java
bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/accessories/HomekitCharacteristicFactory.java

index 610da1a0f8501498257e11be79480f41408e17d5..6736931fb7c79c145cd45279a6ab412c2ad0c7ae 100644 (file)
@@ -923,9 +923,9 @@ All accessories also support the following optional characteristic that can be l
 |                             |                             | FaultStatus                 | Contact, Number, String, Switch         | Fault status                                                                                                                                                                                                                                                                                                                                                  | inverted (false)                                                      | NO_FAULT (0, OFF, CLOSED), GENERAL_FAULT (1, ON, OPEN)                                                      |
 |                             |                             | TamperedStatus              | Contact, Number, String, Switch         | Tampered status                                                                                                                                                                                                                                                                                                                                               | inverted (false)                                                      | NOT_TAMPERED (0, OFF, CLOSED), TAMPERED (1, ON, OPEN)                                                       |
 | Thermostat                  |                             |                             |                                         | A thermostat requires at least one of TargetTemperature, CoolingThresholdTemperature, or HeatingThresholdTemperature must be provided.                                                                                                                                                                                                                        |                                                                       |                                                                                                             |
-|                             | CurrentHeatingCoolingMode   |                             | Number, String                          | Current heating cooling mode                                                                                                                                                                                                                                                                                                                                  |                                                                       | OFF (0, OFF), HEAT (1, ON), COOL (2)                                                                        |
+|                             | CurrentHeatingCoolingMode   |                             | Number, String, Switch                  | Current heating cooling mode                                                                                                                                                                                                                                                                                                                                  |                                                                       | OFF (0, OFF), HEAT (1, ON), COOL (2)                                                                        |
 |                             | CurrentTemperature          |                             | Number                                  | Current temperature.                                                                                                                                                                                                                                                                                                                                          | minValue (0), maxValue (100), step (0.1)                              |                                                                                                             |
-|                             | TargetHeatingCoolingMode    |                             | Number, String                          | Target heating cooling mode                                                                                                                                                                                                                                                                                                                                   |                                                                       | OFF (0, OFF), HEAT (1, ON), COOL (2), AUTO (3) [*](#customizable-enum)                                      |
+|                             | TargetHeatingCoolingMode    |                             | Number, String, Switch                  | Target heating cooling mode                                                                                                                                                                                                                                                                                                                                   |                                                                       | OFF (0, OFF), HEAT (1, ON), COOL (2), AUTO (3) [*](#customizable-enum)                                      |
 |                             |                             | TargetTemperature           | Number                                  | Target temperature. If CoolingThresholdTemperature and HeatingThresholdTemperature are also provided, this characteristic is used when the thermostat is in HEAT or COOL mode. In AUTO mode, this characteristic receives the average of the two thresholds.                                                                                                  | minValue (10), maxValue (38), step (0.1)                              |                                                                                                             |
 |                             |                             | CoolingThresholdTemperature | Number                                  | Maximum temperature that must be reached before cooling is turned on. If TargetTemperature is not provided, this characteristic will also be used in COOL mode.                                                                                                                                                                                               | minValue (10), maxValue (35), step (0.1)                              |                                                                                                             |
 |                             |                             | HeatingThresholdTemperature | Number                                  | Minimum temperature that must be reached before heating is turned on. If TargetTemperature is not provided, this characteristic will also be used in HEAT mode.                                                                                                                                                                                               | minValue (0), maxValue (25), step (0.1)                               |                                                                                                             |
index 8da0410312f220423388757c10577d955e07a957..5b8f50871a2c048423990de6e842945b73ee7b4a 100644 (file)
@@ -483,7 +483,7 @@ public class HomekitChangeListener implements ItemRegistryChangeListener {
             knownAccessories.put(taggedItem.getName(), accessory.toJson());
             accessoryRegistry.addRootAccessory(taggedItem.getName(), accessory);
         } catch (HomekitException e) {
-            logger.warn("Cannot create accessory {}", taggedItem);
+            logger.warn("Cannot create accessory {}: {}", taggedItem, e.getMessage());
         }
     }
 
index 3d52d23c9dcd444e14878fcf8a8e4c58686f2eab..851d0ac330464133735bc197fafd449cfbb79156 100644 (file)
@@ -336,14 +336,18 @@ public class HomekitCharacteristicFactory {
         }
         String onValue = switchType ? OnOffType.ON.toString() : OpenClosedType.OPEN.toString();
         String offValue = switchType ? OnOffType.OFF.toString() : OpenClosedType.CLOSED.toString();
+        @Nullable
+        T offEnumValue = null, onEnumValue = null;
 
         for (var k : klazz.getEnumConstants()) {
             if (numberType) {
                 int code = k.getCode();
                 if ((switchType || contactType) && code == 0) {
                     map.put(k, inverted ? onValue : offValue);
+                    offEnumValue = k;
                 } else if ((switchType || contactType) && code == 1) {
                     map.put(k, inverted ? offValue : onValue);
+                    onEnumValue = k;
                 } else if (percentType && code == 0) {
                     map.put(k, "OFF");
                 } else if (percentType && code == 1) {
@@ -368,7 +372,13 @@ public class HomekitCharacteristicFactory {
             });
         }
         if (customEnumList != null && customEnumList.isEmpty()) {
-            customEnumList.addAll(map.keySet());
+            if (switchType || contactType) {
+                // Switches and Contacts automatically filter the valid values to the first two
+                customEnumList.add(Objects.requireNonNull(offEnumValue));
+                customEnumList.add(Objects.requireNonNull(onEnumValue));
+            } else {
+                customEnumList.addAll(map.keySet());
+            }
         }
         LOGGER.debug("Created {} mapping for item {} ({}): {}", klazz.getSimpleName(), item.getName(),
                 item.getBaseItem().getClass().getSimpleName(), map);