Skip to content

Commit

Permalink
Merge pull request #71 from efirestone/firestone/get-config-entries-c…
Browse files Browse the repository at this point in the history
…ommand

Add support for the config_entries/get command
  • Loading branch information
efirestone authored Jul 16, 2023
2 parents 87fcbbe + 8f7d75d commit 03906ff
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

![GitHub Actions status](https://github.com/efirestone/hassle/workflows/Latest%20push/badge.svg)
![LINE](https://img.shields.io/badge/line--coverage-30.88%25-red.svg)
![LINE](https://img.shields.io/badge/line--coverage-32.23%25-red.svg)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.codellyrandom.hassle/hassle/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.codellyrandom.hassle/hassle)

# Hassle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ internal class ConfigEntityRegistrationListCommand(
) : CommandImpl(id) {
override fun copy(id: Int?) = ConfigEntityRegistrationListCommand(id = id)
}

@Serializable
internal class GetConfigEntriesCommand(
override var id: Int? = null,
private val type: String = "config_entries/get",
) : CommandImpl(id) {
override fun copy(id: Int?) = GetConfigEntriesCommand(id = id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ package com.codellyrandom.hassle.extending.commands
import com.codellyrandom.hassle.HomeAssistantApiClient
import com.codellyrandom.hassle.await
import com.codellyrandom.hassle.communicating.ConfigEntityRegistrationListCommand
import com.codellyrandom.hassle.communicating.GetConfigEntriesCommand
import com.codellyrandom.hassle.values.ConfigEntry
import com.codellyrandom.hassle.values.EntityRegistration

suspend fun HomeAssistantApiClient.getConfigEntries(): List<ConfigEntry> =
await(GetConfigEntriesCommand())

suspend fun HomeAssistantApiClient.getEntityRegistrations(): List<EntityRegistration> =
await(ConfigEntityRegistrationListCommand())
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.codellyrandom.hassle.values

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ConfigEntry(
@SerialName("entry_id")
val entryId: ConfigEntryId,
val domain: Domain,
val title: String,
val source: Source,
val state: State,
@SerialName("supports_options")
val supportsOptions: Boolean,
@SerialName("supports_remove_device")
val supportsRemoveDevice: Boolean,
@SerialName("supports_unload")
val supportsUnload: Boolean,
@SerialName("disabled_by")
val disabledBy: String? = null,
val reason: String? = null,
) {
@Serializable
enum class Source {
@SerialName("import")
IMPORT,

@SerialName("onboarding")
ONBOARDING,

@SerialName("user")
USER,
}

@Serializable
enum class State {
@SerialName("loaded")
LOADED,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,98 @@ package com.codellyrandom.hassle.communicating

import DeviceId
import com.codellyrandom.hassle.TestHomeAssistantApiClient
import com.codellyrandom.hassle.extending.commands.getConfigEntries
import com.codellyrandom.hassle.extending.commands.getEntityRegistrations
import com.codellyrandom.hassle.values.*
import com.codellyrandom.hassle.values.ConfigEntry.Source.IMPORT
import com.codellyrandom.hassle.values.ConfigEntry.Source.ONBOARDING
import com.codellyrandom.hassle.values.ConfigEntry.State.LOADED
import kotlinx.coroutines.runBlocking
import kotlin.test.Test
import kotlin.test.assertEquals

class ConfigCommandTest {
class ConfigCommandsTest {
@Test
fun parseGetConfigEntriesCommand() = runBlocking {
val client = TestHomeAssistantApiClient()
client.setResponse(
"""
[
{
"id": 1,
"type": "result",
"success": true,
"result":
[
{
"entry_id": "541e0e1b4dac48d9aff5a07dd3076983",
"domain": "sun",
"title": "Sun",
"source": "import",
"state": "loaded",
"supports_options": false,
"supports_remove_device": false,
"supports_unload": true,
"pref_disable_new_entities": false,
"pref_disable_polling": false,
"disabled_by": null,
"reason": null
},
{
"entry_id": "133ca90f35a92d2b54626a23ef7783e7",
"domain": "radio_browser",
"title": "Radio Browser",
"source": "onboarding",
"state": "loaded",
"supports_options": true,
"supports_remove_device": true,
"supports_unload": false,
"pref_disable_new_entities": false,
"pref_disable_polling": false,
"disabled_by": null,
"reason": null
}
]
}
]
""".trimIndent(),
forCommandType = "config_entries/get",
)

val entries = client.getConfigEntries()
val expectedEntries = listOf(
ConfigEntry(
entryId = ConfigEntryId("541e0e1b4dac48d9aff5a07dd3076983"),
domain = Domain("sun"),
title = "Sun",
source = IMPORT,
state = LOADED,
supportsOptions = false,
supportsRemoveDevice = false,
supportsUnload = true,
),
ConfigEntry(
entryId = ConfigEntryId("133ca90f35a92d2b54626a23ef7783e7"),
domain = Domain("radio_browser"),
title = "Radio Browser",
source = ONBOARDING,
state = LOADED,
supportsOptions = true,
supportsRemoveDevice = true,
supportsUnload = false,
),
)
assertEquals(expectedEntries, entries)
}

@Test
fun parseListEntityRegistrationCommand() = runBlocking {
val client = TestHomeAssistantApiClient()
client.setResponse(
"""
[
{
"id": 38,
"id": 1,
"type": "result",
"success": true,
"result":
Expand Down

0 comments on commit 03906ff

Please sign in to comment.