Skip to content

Commit

Permalink
Fix stateFromLabel when label contains square brackets in pattern (#3770
Browse files Browse the repository at this point in the history
)

Fix #3756

Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
  • Loading branch information
jimtng authored Jul 30, 2024
1 parent b115600 commit 0736700
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion mobile/src/main/java/org/openhab/habdroid/model/Widget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ data class Widget(
) : Parcelable {
val label get() = rawLabel.split("[", "]")[0].trim()
val stateFromLabel: String? get() {
val value = rawLabel.split("[", "]").getOrNull(1)?.trim()
val value = Widget.stateLabelRegex.find(rawLabel)?.groupValues?.getOrNull(1)
val optionLabel = mappingsOrItemOptions.find { it.value == value }?.label
return optionLabel ?: value
}
Expand Down Expand Up @@ -242,6 +242,8 @@ data class Widget(
state.toParsedState(item.state?.asNumber?.format)
else -> state.toParsedState()
}

internal val stateLabelRegex = Regex("\\[(.*)\\]$")
}
}

Expand Down
23 changes: 22 additions & 1 deletion mobile/src/test/java/org/openhab/habdroid/model/WidgetTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class WidgetTest {
fun testCountInstances() {
assertEquals(sut1.size, 2)
assertEquals(sut2.size, 1)
assertEquals(sut3.size, 4)
assertEquals(sut3.size, 5)
}

@Test
Expand Down Expand Up @@ -157,6 +157,7 @@ class WidgetTest {
assertNull(sut1[0].stateFromLabel)
assertNull(sut2[0].stateFromLabel)
assertEquals("81 %", sut3[1].stateFromLabel)
assertEquals("Value [42]", sut3[4].stateFromLabel)
}

@Test
Expand Down Expand Up @@ -382,6 +383,26 @@ class WidgetTest {
'timeout': false
},
'widgets': []
}, {
'widgetId': '0202_0_0_1',
'type': 'Switch',
'label': 'Test [Value [42]]',
'icon': 'input',
'mappings': [],
'item': {
'link': 'http://openhab.local:8080/rest/items/DemoString',
'state': 'Value [42]',
'stateDescription': {
'pattern': '%s',
'readOnly': false,
'options': []
},
'type': 'String',
'name': 'DemoString',
'tags': [],
'groupNames': []
},
'widgets': []
} ]
}
"""
Expand Down

0 comments on commit 0736700

Please sign in to comment.