Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[basicprofiles] Support double quoted strings in state filter #18117

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundles/org.openhab.transform.basicprofiles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ The `LHS_OPERAND` and the `RHS_OPERAND` can be either one of these:
- An item name, which will be evaluated to its state.
- A type constant, such as `ON`, `OFF`, `UNDEF`, `NULL`, `OPEN`, `CLOSED`, `PLAY`, `PAUSE`, `UP`, `DOWN`, etc.
Note that these are unquoted.
- A String value, enclosed with single quotes, e.g. `'ON'`.
- A String value, enclosed with single or double quotes, e.g. `'ON'`, `"FOO"`.
A string value is different to the actual `OnOffType.ON`.
To compare against an actual OnOffType, use an unquoted `ON`.
- A plain number to represent a `DecimalType`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ State parseState(@Nullable String stateString, List<Class<? extends State>> acce
// Quoted strings are parsed as StringType
if (stateString == null || stateString.isEmpty()) {
return null;
} else if (stateString.startsWith("'") && stateString.endsWith("'")) {
} else if (isQuotedString(stateString)) {
return new StringType(stateString.substring(1, stateString.length() - 1));
} else if (parseFunction(stateString) instanceof FunctionType function) {
return function;
Expand All @@ -258,6 +258,11 @@ State parseState(@Nullable String stateString, List<Class<? extends State>> acce
return null;
}

private boolean isQuotedString(String value) {
return (value.startsWith("'") && value.endsWith("'")) || //
(value.startsWith("\"") && value.endsWith("\""));
}

@Nullable
FunctionType parseFunction(String functionDefinition) {
if (!functionDefinition.startsWith("$")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void testSingleConditionMatch() throws ItemNotFoundException {
}

@Test
public void testSingleConditionMatchQuoted() throws ItemNotFoundException {
public void testSingleConditionMatchSingleQuoted() throws ItemNotFoundException {
when(mockContext.getConfiguration()).thenReturn(new Configuration(Map.of("conditions", "ItemName eq 'Value'")));
when(mockItemRegistry.getItem("ItemName")).thenReturn(stringItemWithState("ItemName", "Value"));

Expand All @@ -184,6 +184,19 @@ public void testSingleConditionMatchQuoted() throws ItemNotFoundException {
verify(mockCallback, times(1)).sendUpdate(eq(expectation));
}

@Test
public void testSingleConditionMatchDoubleQuoted() throws ItemNotFoundException {
when(mockContext.getConfiguration())
.thenReturn(new Configuration(Map.of("conditions", "ItemName eq \"Value\"")));
when(mockItemRegistry.getItem("ItemName")).thenReturn(stringItemWithState("ItemName", "Value"));

StateFilterProfile profile = new StateFilterProfile(mockCallback, mockContext, mockItemRegistry);

State expectation = new StringType("NewValue");
profile.onStateUpdateFromHandler(expectation);
verify(mockCallback, times(1)).sendUpdate(eq(expectation));
}

private Item stringItemWithState(String itemName, String value) {
StringItem item = new StringItem(itemName);
item.setState(new StringType(value));
Expand Down