[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 <cody@cutrer.us>
This commit is contained in:
Cody Cutrer 2024-07-24 01:22:42 -06:00 committed by GitHub
parent e9369b6956
commit bb26e978c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 4 deletions

View 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) | |

View 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());
}
}

View 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);