Skip to content

Commit

Permalink
Support command options
Browse files Browse the repository at this point in the history
Fixes openhab#2655

Signed-off-by: mueller-ma <[email protected]>
  • Loading branch information
mueller-ma committed Jun 26, 2022
1 parent 048b595 commit 0b92d94
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
16 changes: 12 additions & 4 deletions mobile/src/main/java/org/openhab/habdroid/model/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,18 @@ fun JSONObject.toItem(): Item {
val stateDescription = optJSONObject("stateDescription")
val readOnly = stateDescription != null && stateDescription.optBoolean("readOnly", false)

val options = if (stateDescription?.has("options") == true) {
stateDescription.getJSONArray("options").map { obj -> obj.toLabeledValue("value", "label") }
} else {
null
val options = when {
has("commandDescription") -> {
getJSONObject("commandDescription")
.optJSONArray("commandOptions")
?.map { obj -> obj.toLabeledValue("command", "label") }
}
stateDescription?.has("options") == true -> {
stateDescription.getJSONArray("options").map { obj -> obj.toLabeledValue("value", "label") }
}
else -> {
null
}
}

val members = if (has("members")) {
Expand Down
27 changes: 27 additions & 0 deletions mobile/src/test/java/org/openhab/habdroid/model/ItemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ class ItemTest {
assertEquals("Location 2", sut.members[1].label)
}

@Test
fun getCommandOptions() {
val sut = itemWithCommandOptions.toItem()
assertEquals(LabeledValue("1", "One"), sut.options!!.component1())
assertEquals(LabeledValue("2", "Two"), sut.options!!.component2())
}

@Test
fun getTags() {
val sut = itemAsJsonObject.toItem()
Expand Down Expand Up @@ -192,5 +199,25 @@ class ItemTest {
'name': 'LocationGroup' }
""".trimIndent()
)
private val itemWithCommandOptions = JSONObject(
"""
{ 'state': 'NULL',
'type': 'Number',
'name': 'Foo',
'commandDescription': {
'commandOptions': [
{
'command': '1',
'label': 'One'
},
{
'command': '2',
'label': 'Two'
},
]
}
}
""".trimIndent()
)
}
}

0 comments on commit 0b92d94

Please sign in to comment.