Skip to content

Commit 4a31685

Browse files
committed
Add special handling for on/off defaulting to noArg set list entries
1 parent 0b9f216 commit 4a31685

File tree

4 files changed

+31
-32
lines changed

4 files changed

+31
-32
lines changed

app/src/main/java/li/klass/fhem/domain/setlist/SetList.kt

+16-13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package li.klass.fhem.domain.setlist
2626

27+
import li.klass.fhem.domain.setlist.typeEntry.NoArgSetListEntry
2728
import li.klass.fhem.domain.setlist.typeEntry.NotFoundSetListEntry
2829
import li.klass.fhem.domain.setlist.typeEntry.TextFieldSetListEntry
2930
import org.apache.commons.lang3.StringUtils
@@ -42,11 +43,11 @@ class SetList constructor(val entries: Map<String, SetListEntry>) : Serializable
4243

4344
fun contains(vararg keys: String): Boolean = contains(keys.toList())
4445

45-
fun contains(keys: Iterable<String>): Boolean =
46-
keys.any { entries.containsKey(it) }
46+
fun contains(keys: Iterable<String>): Boolean = keys.any { entries.containsKey(it) }
4747

48-
fun existingStatesOf(toSearch: Set<String>): Set<String> =
49-
entries.keys.filter { toSearch.contains(it) }.toSet()
48+
fun existingStatesOf(toSearch: Set<String>): Set<String> = entries.keys.filter {
49+
toSearch.contains(it)
50+
}.toSet()
5051

5152
fun size(): Int = entries.size
5253

@@ -59,23 +60,26 @@ class SetList constructor(val entries: Map<String, SetListEntry>) : Serializable
5960
.joinToString(separator = " ") { it.asText() }
6061
}
6162

62-
fun getFirstPresentStateOf(vararg states: String): String? =
63-
states.asSequence().firstOrNull { contains(it) }
63+
fun getFirstPresentStateOf(
64+
vararg states: String): String? = states.asSequence().firstOrNull { contains(it) }
6465

6566
companion object {
6667
fun parse(inputText: String): SetList {
6768
if (isEmpty(inputText)) return SetList(emptyMap())
6869

69-
val parts = inputText.trim()
70-
.split(" ".toRegex()).toTypedArray()
70+
val parts = inputText.trim().split(" ".toRegex()).toTypedArray()
7171

7272
return SetList(parts.mapNotNull { handlePart(it) }.toMap())
7373
}
7474

7575
private fun handlePart(part: String): Pair<String, SetListEntry>? {
7676

7777
if (!part.contains(":")) {
78-
return Pair(part, TextFieldSetListEntry(part))
78+
val entry = when (part) {
79+
"on", "off" -> NoArgSetListEntry(part)
80+
else -> TextFieldSetListEntry(part)
81+
}
82+
return Pair(part, entry)
7983
}
8084

8185
val keyValue = part.split(":".toRegex(), 2).toTypedArray()
@@ -101,10 +105,9 @@ class SetList constructor(val entries: Map<String, SetListEntry>) : Serializable
101105
return type
102106
}
103107
}
104-
return if (parts.isEmpty() || parts.isNotEmpty() && "colorpicker".equals(parts[0], ignoreCase = true))
105-
SetListItemType.NO_ARG
106-
else
107-
SetListItemType.GROUP
108+
return if (parts.isEmpty() || parts.isNotEmpty() && "colorpicker".equals(parts[0],
109+
ignoreCase = true)) SetListItemType.NO_ARG
110+
else SetListItemType.GROUP
108111
}
109112
}
110113
}

app/src/test/java/li/klass/fhem/domain/setlist/SetListTest.kt

+13-9
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,25 @@ class SetListTest {
6565
TestCase(desc = "only colon",
6666
setList = " : ",
6767
expected = emptyMap()),
68-
TestCase(desc = "noArg",
69-
setList = "on:noArg",
70-
expected = mapOf("on" to NoArgSetListEntry("on"))),
71-
TestCase(desc = "no arg defaults to textField item type", setList = "on",
72-
expected = mapOf("on" to TextFieldSetListEntry("on"))),
68+
TestCase(desc = "noArg", setList = "bla:noArg",
69+
expected = mapOf("bla" to NoArgSetListEntry("bla"))),
70+
TestCase(desc = "noArg on", setList = "on:noArg",
71+
expected = mapOf("on" to NoArgSetListEntry("on"))),
72+
TestCase(desc = "no arg defaults to textField item type", setList = "bla",
73+
expected = mapOf("bla" to TextFieldSetListEntry("bla"))),
74+
TestCase(desc = "no arg defaults to noArg for on", setList = "on",
75+
expected = mapOf("on" to NoArgSetListEntry("on"))),
76+
TestCase(desc = "no arg defaults to noArg for off", setList = "off",
77+
expected = mapOf("off" to NoArgSetListEntry("off"))),
7378
TestCase(desc = "empty :textField",
7479
setList = "0:noArg 1:noArg :textField",
7580
expected = mapOf("0" to NoArgSetListEntry("0"), "1" to NoArgSetListEntry("1"), "state" to TextFieldSetListEntry("state"))),
7681
TestCase(desc = "empty set list",
7782
setList = "",
7883
expected = emptyMap()),
79-
TestCase(desc = "leading trailing whitepace",
80-
setList = " on off ",
81-
expected = mapOf("on" to TextFieldSetListEntry("on"),
82-
"off" to TextFieldSetListEntry("off"))),
84+
TestCase(desc = "leading trailing whitepace", setList = " bla blub ",
85+
expected = mapOf("bla" to TextFieldSetListEntry("bla"),
86+
"blub" to TextFieldSetListEntry("blub"))),
8387
TestCase(desc = "colon without values leads to group",
8488
setList = "internalState:",
8589
expected = emptyMap()),

app/whatsnew-de.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
* LightScene: Aktueller Zustand wird in Übersicht markiert
2-
* Wochenplan (von Heizungen) kann von einem anderen Gerät kopiert werden
3-
* SetList: Standardmäßig ist ein SetList-Attribut jetzt ein TextField (analog zu FHEMWEB)
4-
* Link zu Verbindungsverwaltung in DummyData-Benachrichtigung
5-
* Konfiguration für YeeLight
1+
* Behandle on/off immer als noArg set list Eintrag, sodass kein weiterer Parameter eingegeben werden muss

app/whatsnew-en.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
* LightScene: Currently active scene will be marked within the overview
2-
* Add ability to copy a weekprofile from a different device
3-
* SetList: By default a SetList attribute is now a TextField (analogue to FHEMWEB)
4-
* Link to the connection management in the DummyData notification
5-
* Configuration for YeeLight
1+
* Treat on/off always as noArg set list entry requiring no additional field

0 commit comments

Comments
 (0)