Skip to content

Commit

Permalink
Updates based on feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Arne Seime <[email protected]>
  • Loading branch information
seime committed Dec 3, 2023
1 parent 0136882 commit 9140a9e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
10 changes: 6 additions & 4 deletions bundles/org.smarthomej.transform.basicprofiles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,18 @@ Use case: Ignore values from a binding unless some other item(s) have a specific

### Configuration

| Configuration Parameter | Type | Description |
|-------------------------|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `conditions` | text | Comma separated list of expressions on the format `ITEM_NAME OPERATOR ITEM_STATE`, ie `MyItem EQ OFF`. Use quotes around `ITEM_STATE` to treat value as string ie `'OFF`' |
| `mismatchState` | text | Optional state to pass instead if conditions are NOT met. Use quotes to treat as `StringType`. Defaults to `UNDEF` |
| Configuration Parameter | Type | Description |
|-------------------------|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `conditions` | text | Comma separated list of expressions on the format `ITEM_NAME OPERATOR ITEM_STATE`, ie `MyItem EQ OFF`. Use quotes around `ITEM_STATE` to treat value as string ie `'OFF'` and not `OnOffType.OFF` |
| `mismatchState` | text | Optional state to pass instead if conditions are NOT met. Use single quotes to treat as `StringType`. Defaults to `UNDEF` |
| `separator` | text | Optional separator string to separate expressions when using multiple. Defaults to `,` |

Possible values for token `OPERATOR` in `conditions`:

- `EQ` - Equals
- `NEQ` - Not equals


### Full Example

```Java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public class StateFilterProfileConfig {
public String conditions = "";

public String mismatchState = UnDefType.UNDEF.toString();

public String separator = ",";
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ public StateFilterProfile(ProfileCallback callback, ProfileContext context, Item

StateFilterProfileConfig config = context.getConfiguration().as(StateFilterProfileConfig.class);
if (config != null) {
conditions.addAll(parseConditions(config.conditions));
conditions.addAll(parseConditions(config.conditions, config.separator));
configMismatchState = parseState(config.mismatchState);
}
}

private List parseConditions(String config) {
private List<StateCondition> parseConditions(@Nullable String config, String separator) {
if (config == null)
return List.of();

List<StateCondition> parsedConditions = new ArrayList<>();
try {
String[] expressions = config.split(",");
String[] expressions = config.split(separator);
for (String expression : expressions) {
String[] parts = expression.trim().split("\s");
if (parts.length == 3) {
Expand Down Expand Up @@ -193,23 +193,16 @@ public StateCondition(String itemName, ComparisonType comparisonType, String val
public boolean matches(String state) {
switch (comparisonType) {
case EQ:
if (!state.equals(value)) {
return false;
}
break;
return state.equals(value);
case NEQ: {
if (state.equals(value)) {
return false;
}
break;
return !state.equals(value);
}
default:
logger.warn("Unknown condition type {}. Expected 'eq' or 'neq' - skipping state update",
comparisonType);
return false;

}
return true;
}

enum ComparisonType {
Expand All @@ -219,8 +212,8 @@ enum ComparisonType {

@Override
public String toString() {
return "StateCondition{" + "itemName='" + itemName + '\'' + ", comparisonType=" + comparisonType + ", value='"
+ value + '\'' + '}';
return "Condition{itemName='" + itemName + "', comparisonType=" + comparisonType + ", value='"
+ value + "'}'";
}
}
}

0 comments on commit 9140a9e

Please sign in to comment.