[homekit] support actual booleans in metadata config (#13228)
as well as strings, too. reduces confusion, if someone doesn't know it needed to be a string Signed-off-by: Cody Cutrer <cody@cutrer.us>
This commit is contained in:
@@ -211,14 +211,38 @@ public class HomekitTaggedItem {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns configuration value as boolean if its exists otherwise returns defaultValue
|
||||
*
|
||||
* @param key configuration key
|
||||
* @param defaultValue default value
|
||||
* @return configuration value as boolean
|
||||
*/
|
||||
public boolean getConfigurationAsBoolean(String key, boolean defaultValue) {
|
||||
if (configuration == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
final @Nullable Object value = configuration.get(key);
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (value instanceof Boolean) {
|
||||
return (Boolean) value;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
final String valueString = (String) value;
|
||||
return valueString.equalsIgnoreCase("yes") || valueString.equalsIgnoreCase("true");
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns true if inverted flag is set, i.e. item has the configuration "inverted=true"
|
||||
*
|
||||
* @return true if inverted flag is set to true
|
||||
*/
|
||||
public boolean isInverted() {
|
||||
final String invertedConfig = getConfiguration(HomekitTaggedItem.INVERTED, "false");
|
||||
return invertedConfig.equalsIgnoreCase("yes") || invertedConfig.equalsIgnoreCase("true");
|
||||
return getConfigurationAsBoolean(HomekitTaggedItem.INVERTED, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -196,6 +196,18 @@ abstract class AbstractHomekitAccessoryImpl implements HomekitAccessory {
|
||||
return accessory.getConfiguration(key, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* return configuration attached to the root accessory, e.g. groupItem.
|
||||
*
|
||||
* @param key configuration key
|
||||
* @param defaultValue default value
|
||||
* @return configuration value
|
||||
*/
|
||||
@NonNullByDefault
|
||||
protected boolean getAccessoryConfigurationAsBoolean(String key, boolean defaultValue) {
|
||||
return accessory.getConfigurationAsBoolean(key, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* return configuration of the characteristic item, e.g. currentTemperature.
|
||||
* Note: result will be casted to the type of the default value.
|
||||
|
||||
@@ -57,8 +57,7 @@ abstract class AbstractHomekitPositionAccessoryImpl extends AbstractHomekitAcces
|
||||
List<HomekitTaggedItem> mandatoryCharacteristics, HomekitAccessoryUpdater updater,
|
||||
HomekitSettings settings) {
|
||||
super(taggedItem, mandatoryCharacteristics, updater, settings);
|
||||
final String invertedConfig = getAccessoryConfiguration(HomekitTaggedItem.INVERTED, "true");
|
||||
final boolean inverted = invertedConfig.equalsIgnoreCase("yes") || invertedConfig.equalsIgnoreCase("true");
|
||||
final boolean inverted = getAccessoryConfigurationAsBoolean(HomekitTaggedItem.INVERTED, true);
|
||||
closedPosition = inverted ? 0 : 100;
|
||||
openPosition = inverted ? 100 : 0;
|
||||
positionStateMapping = new EnumMap<>(PositionStateEnum.class);
|
||||
|
||||
@@ -147,8 +147,8 @@ public class HomekitAccessoryFactory {
|
||||
characteristics.addAll(Arrays.asList(MANDATORY_CHARACTERISTICS.get(taggedItem.getAccessoryType())));
|
||||
}
|
||||
if (taggedItem.getAccessoryType() == BATTERY) {
|
||||
final String isChargeable = taggedItem.getConfiguration(HomekitBatteryImpl.BATTERY_TYPE, "false");
|
||||
if ("true".equalsIgnoreCase(isChargeable) || "yes".equalsIgnoreCase(isChargeable)) {
|
||||
final boolean isChargeable = taggedItem.getConfigurationAsBoolean(HomekitBatteryImpl.BATTERY_TYPE, false);
|
||||
if (isChargeable) {
|
||||
characteristics.add(BATTERY_CHARGING_STATE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,8 +47,7 @@ public class HomekitBatteryImpl extends AbstractHomekitAccessoryImpl implements
|
||||
HomekitAccessoryUpdater updater, HomekitSettings settings) throws IncompleteAccessoryException {
|
||||
super(taggedItem, mandatoryCharacteristics, updater, settings);
|
||||
lowBatteryReader = createBooleanReader(BATTERY_LOW_STATUS);
|
||||
final String batteryTypeConfig = getAccessoryConfiguration(BATTERY_TYPE, "false");
|
||||
isChargeable = "true".equalsIgnoreCase(batteryTypeConfig) || "yes".equalsIgnoreCase(batteryTypeConfig);
|
||||
isChargeable = getAccessoryConfigurationAsBoolean(BATTERY_TYPE, false);
|
||||
if (isChargeable) {
|
||||
chargingBatteryReader = createBooleanReader(BATTERY_CHARGING_STATE);
|
||||
}
|
||||
|
||||
@@ -78,8 +78,7 @@ public class HomekitValveImpl extends AbstractHomekitAccessoryImpl implements Va
|
||||
activeReader = createBooleanReader(ACTIVE_STATUS);
|
||||
ValveService service = new ValveService(this);
|
||||
getServices().add(service);
|
||||
final String timerConfig = getAccessoryConfiguration(CONFIG_TIMER, "");
|
||||
homekitTimer = timerConfig.equalsIgnoreCase("yes") || timerConfig.equalsIgnoreCase("true");
|
||||
homekitTimer = getAccessoryConfigurationAsBoolean(CONFIG_TIMER, false);
|
||||
if (homekitTimer) {
|
||||
addRemainingDurationCharacteristic(taggedItem, updater, service);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user