Skip to content

Commit

Permalink
RTFM: get spotify player correct
Browse files Browse the repository at this point in the history
  • Loading branch information
EAGrahamJr committed Jun 15, 2024
1 parent 522b626 commit 26590ed
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
45 changes: 33 additions & 12 deletions src/main/kotlin/crackers/hassk/HAssKClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package crackers.hassk

import org.json.JSONArray
import org.json.JSONObject
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.IOException
import java.net.URI
Expand Down Expand Up @@ -49,6 +50,13 @@ object Constants {
const val off = false
}

/**
* Media things
*/
object MediaConstants {
const val SPOTIFY = "Spotify"
}

/**
* A very simple [HomeAssistant](https://www.home-assistant.io/) REST API client with minimal functionality.
*
Expand All @@ -60,12 +68,12 @@ object Constants {
* @param haPort the port (default sto `8123`
* @property serverUri the location for the API on HA
*/
open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123) {
val serverUri = "http://$haServer:$haPort/api"
protected val client = HttpClient.newBuilder()
open class HAssKClient(private val token: String, haServer: String, haPort: Int = 8123) {
protected val serverUri = "http://$haServer:$haPort/api"
protected val client: HttpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.build()
protected val logger = LoggerFactory.getLogger(javaClass.simpleName)
protected val logger: Logger = LoggerFactory.getLogger(javaClass.simpleName)

/**
* Set up generic service call that uses the entity ID as the payload.
Expand Down Expand Up @@ -220,8 +228,13 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123)

/**
* Create an entity in the "media_player" domain ( do **not** prefix with "media_player.")
*
* Specialized players are also initialized here (e.g. Spotify)
*/
fun media(name: String) = MediaPlayer(name)
fun media(name: String) = when {
name.equals(MediaConstants.SPOTIFY, true) -> SpotifyPlayer(name)
else -> MediaPlayer(name)
}

/**
* Create an entity in the "sensor" domain (do **not** prefix with "sensor.")
Expand Down Expand Up @@ -287,11 +300,15 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123)
override val entityId = "$domain.$name"
}

class MediaPlayer(val name: String) : Entity {
override val domain = "media_player"
open class MediaPlayer(name: String) : Entity {
final override val domain = "media_player"
override val entityId = "$domain.$name"
}

class SpotifyPlayer(name: String) : MediaPlayer(name) {
var currentPlayer: String = "None"
}

fun MediaPlayer.pause(): List<EntityState> {
val response = callService(entityId, domain, "media_pause")
return JSONArray(response).map { parseState(it as JSONObject) }
Expand All @@ -302,6 +319,15 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123)
return JSONArray(response).map { parseState(it as JSONObject) }
}

fun SpotifyPlayer.play(player: String? = null): List<EntityState> {
if (player != null) currentPlayer = player
if (currentPlayer == "None") throw IllegalStateException("Current player must be set before invoking play")

callService(entityId, domain, "select_source", mapOf("source" to currentPlayer))
val response = callService(entityId, domain, "media_play")
return JSONArray(response).map { parseState(it as JSONObject) }
}

fun MediaPlayer.stop(): List<EntityState> {
val response = callService(entityId, domain, "media_stop")
return JSONArray(response).map { parseState(it as JSONObject) }
Expand Down Expand Up @@ -329,9 +355,4 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123)

operator fun MediaPlayer.unaryPlus() = volumeUp()
operator fun MediaPlayer.unaryMinus() = volumeDown()

infix fun MediaPlayer.selectSource(name: String): List<EntityState> {
val response = callService(entityId, domain, "play_media", mapOf("source" to name))
return JSONArray(response).map { parseState(it as JSONObject) }
}
}
6 changes: 3 additions & 3 deletions version.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#Generated by the Semver Plugin for Gradle
#Sun Dec 17 12:03:32 PST 2023
#Tue Jun 11 16:00:29 PDT 2024
version.buildmeta=
version.major=0
version.minor=0
version.patch=4
version.patch=5
version.prerelease=
version.semver=0.0.4
version.semver=0.0.5

0 comments on commit 26590ed

Please sign in to comment.