Skip to content

Commit

Permalink
Better visibility and logging
Browse files Browse the repository at this point in the history
Fixued up some doc and build stuff
  • Loading branch information
EAGrahamJr committed Nov 24, 2024
1 parent 26590ed commit 43b83e2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ bin/

Foo.*
*.json
.kotlin
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# HomeAssistant Client for Kotlin

- Extremely simple API with some DSL goodies
- Requires Java 11<sup>**1**</sup> for included HttpClient stuff
- uses _async_ under the covers, so multi-threading is provided out-of-the-box
- Uses _async_ under the covers, so limited multi-threading is provided out-of-the-box
- Runtime requires:
- Java 17 or newer
- Kotlin 1.9.0 or newer
- Minimal external libraries (`SLF4J`, `org.json`)

![Just Build](https://github.com/EAGrahamJr/HAssK/actions/workflows/build.yaml/badge.svg) ![Kotlin](https://badgen.net/badge/Kotlin/1.9,0/purple) ![Java](https://badgen.net/badge/Java/17/orange) ![Apache License](https://badgen.net/github/license/EAGrahamJr/HAssK)
Expand Down Expand Up @@ -38,7 +40,3 @@ This project uses [Gradle](https://gradle.org), so the only thing you need is a
A default build will use the [gradle-plugins](https://github.com/EAGrahamJr/gradle-scripts) to publish to the "local" Maven repository.

[Documentation](docs) is created via the `dokka` plugin: Javadocs **are** created on build (but not published, yet).

---

<sup>**1**</sup>Java 17 is the current build target.
21 changes: 19 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
#Sat Apr 15 22:23:53 PDT 2023
#
# Copyright 2022-2024 by E. A. Graham, Jr.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
#

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
18 changes: 11 additions & 7 deletions src/main/kotlin/crackers/hassk/HAssKClient.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2023 by E. A. Graham, Jr.
* Copyright 2022-2024 by E. A. Graham, Jr.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -69,7 +69,7 @@ object MediaConstants {
* @property serverUri the location for the API on HA
*/
open class HAssKClient(private val token: String, haServer: String, haPort: Int = 8123) {
protected val serverUri = "http://$haServer:$haPort/api"
val serverUri = "http://$haServer:$haPort/api"
protected val client: HttpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.build()
Expand Down Expand Up @@ -99,7 +99,7 @@ open class HAssKClient(private val token: String, haServer: String, haPort: Int
logger.debug("Request: {}", uri)
logger.debug("Payload: $payload")
}
return sendIt(request)
return sendIt(request, entityId, serviceCommand)
}

/**
Expand All @@ -112,7 +112,7 @@ open class HAssKClient(private val token: String, haServer: String, haPort: Int
val request = startRequest(uri)
.GET()
.build()
return sendIt(request).let {
return sendIt(request, entityId, "states").let {
logger.debug("Receiving: $it")
parseState(JSONObject(it))
}
Expand Down Expand Up @@ -141,11 +141,12 @@ open class HAssKClient(private val token: String, haServer: String, haPort: Int
/**
* Off we go... (call [startRequest] to kick things off)
*/
fun sendIt(request: HttpRequest): String {
fun sendIt(request: HttpRequest, entity: String, command: String): String {
return client.sendAsync(request, requestPayloadHandler).let {
val response = it.get()
if (response.statusCode() != 200) {
logger.error("Error response: ${response.statusCode()}")
logger.error("Error response for entity '$entity' doing '$command': ${response.statusCode()}")
logger.error("\t${response.body()}")
throw IOException("Error from HA - see logs")
}
response.body().also {
Expand Down Expand Up @@ -248,7 +249,7 @@ open class HAssKClient(private val token: String, haServer: String, haPort: Int
* Uses the state JSON API to get a list of entities and their states. It is filtered by the optional [domain].
*/
fun states(domain: String? = null): List<EntityState> {
val response = sendIt(startRequest(URI.create("$serverUri/states")).build())
val response = sendIt(startRequest(URI.create("$serverUri/states")).build(), "all", "states")
return JSONArray(response)
.map { parseState(it as JSONObject) }
.filter { domain == null || it.entityId.startsWith("$domain.") }
Expand Down Expand Up @@ -305,6 +306,9 @@ open class HAssKClient(private val token: String, haServer: String, haPort: Int
override val entityId = "$domain.$name"
}

/**
* A Spotify player has an additional "who's playing" attribute.
*/
class SpotifyPlayer(name: String) : MediaPlayer(name) {
var currentPlayer: String = "None"
}
Expand Down
22 changes: 19 additions & 3 deletions version.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
#
# Copyright 2022-2024 by E. A. Graham, Jr.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied. See the License for the specific language governing
# permissions and limitations under the License.
#

#Generated by the Semver Plugin for Gradle
#Tue Jun 11 16:00:29 PDT 2024
#Sat Jun 15 10:59:49 PDT 2024
version.buildmeta=
version.major=0
version.minor=0
version.patch=5
version.patch=6
version.prerelease=
version.semver=0.0.5
version.semver=0.0.6

0 comments on commit 43b83e2

Please sign in to comment.