From bb26e978c285b78b1d54929ff498d84ce8b1260c Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Wed, 24 Jul 2024 01:22:42 -0600 Subject: [PATCH] [homekit] Automatically assume valid enum values when linking to Switch/Contact (#17134) 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 --- bundles/org.openhab.io.homekit/README.md | 4 ++-- .../io/homekit/internal/HomekitChangeListener.java | 2 +- .../accessories/HomekitCharacteristicFactory.java | 12 +++++++++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.io.homekit/README.md b/bundles/org.openhab.io.homekit/README.md index 610da1a0f85..6736931fb7c 100644 --- a/bundles/org.openhab.io.homekit/README.md +++ b/bundles/org.openhab.io.homekit/README.md @@ -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) | | diff --git a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitChangeListener.java b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitChangeListener.java index 8da0410312f..5b8f50871a2 100644 --- a/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitChangeListener.java +++ b/bundles/org.openhab.io.homekit/src/main/java/org/openhab/io/homekit/internal/HomekitChangeListener.java @@ -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()); } } 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 3d52d23c9dc..851d0ac3304 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 @@ -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);