diff --git a/mobile/src/main/java/org/openhab/habdroid/model/Widget.kt b/mobile/src/main/java/org/openhab/habdroid/model/Widget.kt index e672745ed1..7d92173caa 100644 --- a/mobile/src/main/java/org/openhab/habdroid/model/Widget.kt +++ b/mobile/src/main/java/org/openhab/habdroid/model/Widget.kt @@ -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 } @@ -242,6 +242,8 @@ data class Widget( state.toParsedState(item.state?.asNumber?.format) else -> state.toParsedState() } + + internal val stateLabelRegex = Regex("\\[(.*)\\]$") } } diff --git a/mobile/src/test/java/org/openhab/habdroid/model/WidgetTest.kt b/mobile/src/test/java/org/openhab/habdroid/model/WidgetTest.kt index 69ff3ea739..9ea2e51e47 100644 --- a/mobile/src/test/java/org/openhab/habdroid/model/WidgetTest.kt +++ b/mobile/src/test/java/org/openhab/habdroid/model/WidgetTest.kt @@ -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 @@ -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 @@ -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': [] } ] } """