Skip to content

Commit

Permalink
[homekit] Automatically assume valid enum values when linking to Swit…
Browse files Browse the repository at this point in the history
…ch/Contact (openhab#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 <[email protected]>
  • Loading branch information
ccutrer authored Jul 24, 2024
1 parent e9369b6 commit bb26e97
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions bundles/org.openhab.io.homekit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ private void createRootAccessories(Item item) {
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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,18 @@ public static <T extends Enum<T> & CharacteristicEnum> Map<T, String> createMapp
}
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) {
Expand All @@ -368,7 +372,13 @@ public static <T extends Enum<T> & CharacteristicEnum> Map<T, String> createMapp
});
}
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);
Expand Down

0 comments on commit bb26e97

Please sign in to comment.