Skip to content

Commit

Permalink
Add media controls.
Browse files Browse the repository at this point in the history
Since we're controlling everything, control everything.
  • Loading branch information
EAGrahamJr committed Jun 11, 2024
1 parent 2056646 commit 522b626
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ buildscript {

plugins {
kotlin("jvm") version "1.9.0"
`java-library`
idea
id("org.jmailen.kotlinter") version "3.12.0"
id("library-publish") version "1.0.1"
Expand All @@ -21,7 +22,7 @@ repositories {
}

dependencies {
api("org.json:json:20230227")
api("org.json:json:20231013")
api("org.slf4j:slf4j-api:2.0.0")

testImplementation("io.kotest:kotest-runner-junit5:5.5.4")
Expand Down
66 changes: 62 additions & 4 deletions src/main/kotlin/crackers/hassk/HAssKClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123)
*/
fun switch(name: String) = Switch(name)

/**
* Create an entity in the "media_player" domain ( do **not** prefix with "media_player.")
*/
fun media(name: String) = MediaPlayer(name)

/**
* Create an entity in the "sensor" domain (do **not** prefix with "sensor.")
*
Expand All @@ -243,6 +248,7 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123)
*/
interface Entity {
val entityId: String
val domain: String
}

/**
Expand All @@ -253,27 +259,79 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123)
* @property isGroup group of lights (or not)
*/
class Light(name: String, val isGroup: Boolean = false) : Entity {
override val entityId = "light.$name"
override val domain = "light"
override val entityId = "$domain.$name"
}

/**
* A pre-determined group of entities at certain states.
*/
class Scene(name: String) : Entity {
override val entityId = "scene.$name"
override val domain = "scene"
override val entityId = "$domain.$name"
}

/**
* A switch-type entity.
*/
class Switch(name: String) : Entity {
override val entityId = "switch.$name"
override val domain = "switch"
override val entityId = "$domain.$name"
}

/**
* A sensor entity. **NOTE** Sensors are "read-only" and will not generally respond to commands.
*/
class Sensor(name: String) : Entity {
override val entityId = "sensor.$name"
override val domain = "sensor"
override val entityId = "$domain.$name"
}

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

fun MediaPlayer.pause(): List<EntityState> {
val response = callService(entityId, domain, "media_pause")
return JSONArray(response).map { parseState(it as JSONObject) }
}

fun MediaPlayer.play(): List<EntityState> {
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) }
}

fun MediaPlayer.next(): List<EntityState> {
val response = callService(entityId, domain, "media_next_track")
return JSONArray(response).map { parseState(it as JSONObject) }
}

fun MediaPlayer.previous(): List<EntityState> {
val response = callService(entityId, domain, "media_previous_track")
return JSONArray(response).map { parseState(it as JSONObject) }
}

fun MediaPlayer.volumeUp(): List<EntityState> {
val response = callService(entityId, domain, "volume_up")
return JSONArray(response).map { parseState(it as JSONObject) }
}

fun MediaPlayer.volumeDown(): List<EntityState> {
val response = callService(entityId, "homeassistant", "volume_down")
return JSONArray(response).map { parseState(it as JSONObject) }
}

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
#Sat Dec 09 13:42:53 PST 2023
#Sun Dec 17 12:03:32 PST 2023
version.buildmeta=
version.major=0
version.minor=0
version.patch=3
version.patch=4
version.prerelease=
version.semver=0.0.3
version.semver=0.0.4

0 comments on commit 522b626

Please sign in to comment.