Skip to content

Commit 3b0c787

Browse files
authored
[basicprofiles] Support double quoted strings in state filter (openhab#18117)
Signed-off-by: Jimmy Tanagra <[email protected]>
1 parent 34f81c6 commit 3b0c787

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

bundles/org.openhab.transform.basicprofiles/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ The `LHS_OPERAND` and the `RHS_OPERAND` can be either one of these:
218218
- An item name, which will be evaluated to its state.
219219
- A type constant, such as `ON`, `OFF`, `UNDEF`, `NULL`, `OPEN`, `CLOSED`, `PLAY`, `PAUSE`, `UP`, `DOWN`, etc.
220220
Note that these are unquoted.
221-
- A String value, enclosed with single quotes, e.g. `'ON'`.
221+
- A String value, enclosed with single or double quotes, e.g. `'ON'`, `"FOO"`.
222222
A string value is different to the actual `OnOffType.ON`.
223223
To compare against an actual OnOffType, use an unquoted `ON`.
224224
- A plain number to represent a `DecimalType`.

bundles/org.openhab.transform.basicprofiles/src/main/java/org/openhab/transform/basicprofiles/internal/profiles/StateFilterProfile.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ State parseState(@Nullable String stateString, List<Class<? extends State>> acce
248248
// Quoted strings are parsed as StringType
249249
if (stateString == null || stateString.isEmpty()) {
250250
return null;
251-
} else if (stateString.startsWith("'") && stateString.endsWith("'")) {
251+
} else if (isQuotedString(stateString)) {
252252
return new StringType(stateString.substring(1, stateString.length() - 1));
253253
} else if (parseFunction(stateString) instanceof FunctionType function) {
254254
return function;
@@ -258,6 +258,11 @@ State parseState(@Nullable String stateString, List<Class<? extends State>> acce
258258
return null;
259259
}
260260

261+
private boolean isQuotedString(String value) {
262+
return (value.startsWith("'") && value.endsWith("'")) || //
263+
(value.startsWith("\"") && value.endsWith("\""));
264+
}
265+
261266
@Nullable
262267
FunctionType parseFunction(String functionDefinition) {
263268
if (!functionDefinition.startsWith("$")) {

bundles/org.openhab.transform.basicprofiles/src/test/java/org/openhab/transform/basicprofiles/internal/profiles/StateFilterProfileTest.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void testSingleConditionMatch() throws ItemNotFoundException {
173173
}
174174

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

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

187+
@Test
188+
public void testSingleConditionMatchDoubleQuoted() throws ItemNotFoundException {
189+
when(mockContext.getConfiguration())
190+
.thenReturn(new Configuration(Map.of("conditions", "ItemName eq \"Value\"")));
191+
when(mockItemRegistry.getItem("ItemName")).thenReturn(stringItemWithState("ItemName", "Value"));
192+
193+
StateFilterProfile profile = new StateFilterProfile(mockCallback, mockContext, mockItemRegistry);
194+
195+
State expectation = new StringType("NewValue");
196+
profile.onStateUpdateFromHandler(expectation);
197+
verify(mockCallback, times(1)).sendUpdate(eq(expectation));
198+
}
199+
187200
private Item stringItemWithState(String itemName, String value) {
188201
StringItem item = new StringItem(itemName);
189202
item.setState(new StringType(value));

0 commit comments

Comments
 (0)