-

hassk 0.0.2 API

+

hassk 0.0.3 API

diff --git a/docs/member-search-index.js b/docs/member-search-index.js index b44c8e6..a7640f7 100644 --- a/docs/member-search-index.js +++ b/docs/member-search-index.js @@ -1 +1 @@ -var memberSearchIndex = [{"p":"crackers.hassk","c":"EntityState","l":"EntityState(String, String, ZonedDateTime, String)","url":"crackers/hassk/EntityState.html#EntityState(java.lang.String,java.lang.String,java.time.ZonedDateTime,java.lang.String)"}, {"p":"crackers.hassk","c":"EntityState","l":"getEntityId()","url":"crackers/hassk/EntityState.html#getEntityId()"}, {"p":"crackers.hassk","c":"EntityState","l":"getState()","url":"crackers/hassk/EntityState.html#getState()"}, {"p":"crackers.hassk","c":"EntityState","l":"getChanged()","url":"crackers/hassk/EntityState.html#getChanged()"}, {"p":"crackers.hassk","c":"EntityState","l":"getAttributes()","url":"crackers/hassk/EntityState.html#getAttributes()"}, {"p":"crackers.hassk","c":"HAssKClient.Entity","l":"getEntityId()","url":"crackers/hassk/HAssKClient.Entity.html#getEntityId()"}, {"p":"crackers.hassk","c":"HAssKClient.Light","l":"HAssKClient.Light(String, Boolean)","url":"crackers/hassk/HAssKClient.Light.html#HAssKClient.Light(java.lang.String,java.lang.Boolean)"}, {"p":"crackers.hassk","c":"HAssKClient.Light","l":"getEntityId()","url":"crackers/hassk/HAssKClient.Light.html#getEntityId()"}, {"p":"crackers.hassk","c":"HAssKClient.Light","l":"isGroup()","url":"crackers/hassk/HAssKClient.Light.html#isGroup()"}, {"p":"crackers.hassk","c":"HAssKClient.Scene","l":"HAssKClient.Scene(String)","url":"crackers/hassk/HAssKClient.Scene.html#HAssKClient.Scene(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient.Scene","l":"getEntityId()","url":"crackers/hassk/HAssKClient.Scene.html#getEntityId()"}, {"p":"crackers.hassk","c":"HAssKClient.Switch","l":"HAssKClient.Switch(String)","url":"crackers/hassk/HAssKClient.Switch.html#HAssKClient.Switch(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient.Switch","l":"getEntityId()","url":"crackers/hassk/HAssKClient.Switch.html#getEntityId()"}, {"p":"crackers.hassk","c":"HAssKClient.Sensor","l":"HAssKClient.Sensor(String)","url":"crackers/hassk/HAssKClient.Sensor.html#HAssKClient.Sensor(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient.Sensor","l":"getEntityId()","url":"crackers/hassk/HAssKClient.Sensor.html#getEntityId()"}, {"p":"crackers.hassk","c":"HAssKClient","l":"HAssKClient(String, String, Integer)","url":"crackers/hassk/HAssKClient.html#HAssKClient(java.lang.String,java.lang.String,java.lang.Integer)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"getServerUri()","url":"crackers/hassk/HAssKClient.html#getServerUri()"}, {"p":"crackers.hassk","c":"HAssKClient","l":"getToken()","url":"crackers/hassk/HAssKClient.html#getToken()"}, {"p":"crackers.hassk","c":"HAssKClient","l":"callService(String, String, String)","url":"crackers/hassk/HAssKClient.html#callService(java.lang.String,java.lang.String,java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"getState(String)","url":"crackers/hassk/HAssKClient.html#getState(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"startRequest(URI)","url":"crackers/hassk/HAssKClient.html#startRequest(java.net.URI)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"sendIt(HttpRequest)","url":"crackers/hassk/HAssKClient.html#sendIt(java.net.http.HttpRequest)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"turn(HAssKClient.Entity, Boolean)","url":"crackers/hassk/HAssKClient.html#turn(crackers.hassk.HAssKClient.Entity,java.lang.Boolean)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"state(HAssKClient.Entity)","url":"crackers/hassk/HAssKClient.html#state(crackers.hassk.HAssKClient.Entity)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"light(String)","url":"crackers/hassk/HAssKClient.html#light(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"group(String)","url":"crackers/hassk/HAssKClient.html#group(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"scene(String)","url":"crackers/hassk/HAssKClient.html#scene(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"switch(String)","url":"crackers/hassk/HAssKClient.html#switch(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"sensor(String)","url":"crackers/hassk/HAssKClient.html#sensor(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"states(String)","url":"crackers/hassk/HAssKClient.html#states(java.lang.String)"}, {"p":"crackers.hassk","c":"EntityState","l":"entityId","url":"crackers/hassk/EntityState.html#entityId"}, {"p":"crackers.hassk","c":"EntityState","l":"state","url":"crackers/hassk/EntityState.html#state"}, {"p":"crackers.hassk","c":"EntityState","l":"changed","url":"crackers/hassk/EntityState.html#changed"}, {"p":"crackers.hassk","c":"EntityState","l":"attributes","url":"crackers/hassk/EntityState.html#attributes"}, {"p":"crackers.hassk","c":"Constants","l":"on","url":"crackers/hassk/Constants.html#on"}, {"p":"crackers.hassk","c":"Constants","l":"off","url":"crackers/hassk/Constants.html#off"}, {"p":"crackers.hassk","c":"Constants","l":"INSTANCE","url":"crackers/hassk/Constants.html#INSTANCE"}, {"p":"crackers.hassk","c":"HAssKClient.Light","l":"entityId","url":"crackers/hassk/HAssKClient.Light.html#entityId"}, {"p":"crackers.hassk","c":"HAssKClient.Light","l":"isGroup","url":"crackers/hassk/HAssKClient.Light.html#isGroup()"}, {"p":"crackers.hassk","c":"HAssKClient.Scene","l":"entityId","url":"crackers/hassk/HAssKClient.Scene.html#entityId"}, {"p":"crackers.hassk","c":"HAssKClient.Switch","l":"entityId","url":"crackers/hassk/HAssKClient.Switch.html#entityId"}, {"p":"crackers.hassk","c":"HAssKClient.Sensor","l":"entityId","url":"crackers/hassk/HAssKClient.Sensor.html#entityId"}, {"p":"crackers.hassk","c":"HAssKClient","l":"serverUri","url":"crackers/hassk/HAssKClient.html#serverUri"}, {"p":"crackers.hassk","c":"HAssKClient","l":"token","url":"crackers/hassk/HAssKClient.html#token"}] +var memberSearchIndex = [{"p":"crackers.hassk","c":"EntityState","l":"EntityState(String, String, ZonedDateTime, String)","url":"crackers/hassk/EntityState.html#EntityState(java.lang.String,java.lang.String,java.time.ZonedDateTime,java.lang.String)"}, {"p":"crackers.hassk","c":"EntityState","l":"getEntityId()","url":"crackers/hassk/EntityState.html#getEntityId()"}, {"p":"crackers.hassk","c":"EntityState","l":"getState()","url":"crackers/hassk/EntityState.html#getState()"}, {"p":"crackers.hassk","c":"EntityState","l":"getChanged()","url":"crackers/hassk/EntityState.html#getChanged()"}, {"p":"crackers.hassk","c":"EntityState","l":"getAttributes()","url":"crackers/hassk/EntityState.html#getAttributes()"}, {"p":"crackers.hassk","c":"HAssKClient.Entity","l":"getEntityId()","url":"crackers/hassk/HAssKClient.Entity.html#getEntityId()"}, {"p":"crackers.hassk","c":"HAssKClient.Light","l":"HAssKClient.Light(String, Boolean)","url":"crackers/hassk/HAssKClient.Light.html#HAssKClient.Light(java.lang.String,java.lang.Boolean)"}, {"p":"crackers.hassk","c":"HAssKClient.Light","l":"getEntityId()","url":"crackers/hassk/HAssKClient.Light.html#getEntityId()"}, {"p":"crackers.hassk","c":"HAssKClient.Light","l":"isGroup()","url":"crackers/hassk/HAssKClient.Light.html#isGroup()"}, {"p":"crackers.hassk","c":"HAssKClient.Scene","l":"HAssKClient.Scene(String)","url":"crackers/hassk/HAssKClient.Scene.html#HAssKClient.Scene(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient.Scene","l":"getEntityId()","url":"crackers/hassk/HAssKClient.Scene.html#getEntityId()"}, {"p":"crackers.hassk","c":"HAssKClient.Switch","l":"HAssKClient.Switch(String)","url":"crackers/hassk/HAssKClient.Switch.html#HAssKClient.Switch(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient.Switch","l":"getEntityId()","url":"crackers/hassk/HAssKClient.Switch.html#getEntityId()"}, {"p":"crackers.hassk","c":"HAssKClient.Sensor","l":"HAssKClient.Sensor(String)","url":"crackers/hassk/HAssKClient.Sensor.html#HAssKClient.Sensor(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient.Sensor","l":"getEntityId()","url":"crackers/hassk/HAssKClient.Sensor.html#getEntityId()"}, {"p":"crackers.hassk","c":"HAssKClient","l":"HAssKClient(String, String, Integer)","url":"crackers/hassk/HAssKClient.html#HAssKClient(java.lang.String,java.lang.String,java.lang.Integer)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"getServerUri()","url":"crackers/hassk/HAssKClient.html#getServerUri()"}, {"p":"crackers.hassk","c":"HAssKClient","l":"getToken()","url":"crackers/hassk/HAssKClient.html#getToken()"}, {"p":"crackers.hassk","c":"HAssKClient","l":"callService(String, String, String, Map)","url":"crackers/hassk/HAssKClient.html#callService(java.lang.String,java.lang.String,java.lang.String,java.util.Map)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"getState(String)","url":"crackers/hassk/HAssKClient.html#getState(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"startRequest(URI)","url":"crackers/hassk/HAssKClient.html#startRequest(java.net.URI)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"sendIt(HttpRequest)","url":"crackers/hassk/HAssKClient.html#sendIt(java.net.http.HttpRequest)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"turn(HAssKClient.Entity, Boolean)","url":"crackers/hassk/HAssKClient.html#turn(crackers.hassk.HAssKClient.Entity,java.lang.Boolean)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"set(HAssKClient.Light, Integer)","url":"crackers/hassk/HAssKClient.html#set(crackers.hassk.HAssKClient.Light,java.lang.Integer)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"state(HAssKClient.Entity)","url":"crackers/hassk/HAssKClient.html#state(crackers.hassk.HAssKClient.Entity)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"light(String)","url":"crackers/hassk/HAssKClient.html#light(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"group(String)","url":"crackers/hassk/HAssKClient.html#group(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"scene(String)","url":"crackers/hassk/HAssKClient.html#scene(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"switch(String)","url":"crackers/hassk/HAssKClient.html#switch(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"sensor(String)","url":"crackers/hassk/HAssKClient.html#sensor(java.lang.String)"}, {"p":"crackers.hassk","c":"HAssKClient","l":"states(String)","url":"crackers/hassk/HAssKClient.html#states(java.lang.String)"}, {"p":"crackers.hassk","c":"EntityState","l":"entityId","url":"crackers/hassk/EntityState.html#entityId"}, {"p":"crackers.hassk","c":"EntityState","l":"state","url":"crackers/hassk/EntityState.html#state"}, {"p":"crackers.hassk","c":"EntityState","l":"changed","url":"crackers/hassk/EntityState.html#changed"}, {"p":"crackers.hassk","c":"EntityState","l":"attributes","url":"crackers/hassk/EntityState.html#attributes"}, {"p":"crackers.hassk","c":"Constants","l":"on","url":"crackers/hassk/Constants.html#on"}, {"p":"crackers.hassk","c":"Constants","l":"off","url":"crackers/hassk/Constants.html#off"}, {"p":"crackers.hassk","c":"Constants","l":"INSTANCE","url":"crackers/hassk/Constants.html#INSTANCE"}, {"p":"crackers.hassk","c":"HAssKClient.Light","l":"entityId","url":"crackers/hassk/HAssKClient.Light.html#entityId"}, {"p":"crackers.hassk","c":"HAssKClient.Light","l":"isGroup","url":"crackers/hassk/HAssKClient.Light.html#isGroup()"}, {"p":"crackers.hassk","c":"HAssKClient.Scene","l":"entityId","url":"crackers/hassk/HAssKClient.Scene.html#entityId"}, {"p":"crackers.hassk","c":"HAssKClient.Switch","l":"entityId","url":"crackers/hassk/HAssKClient.Switch.html#entityId"}, {"p":"crackers.hassk","c":"HAssKClient.Sensor","l":"entityId","url":"crackers/hassk/HAssKClient.Sensor.html#entityId"}, {"p":"crackers.hassk","c":"HAssKClient","l":"serverUri","url":"crackers/hassk/HAssKClient.html#serverUri"}, {"p":"crackers.hassk","c":"HAssKClient","l":"token","url":"crackers/hassk/HAssKClient.html#token"}] diff --git a/src/main/kotlin/crackers/hassk/HAssKClient.kt b/src/main/kotlin/crackers/hassk/HAssKClient.kt index 54b6b2a..182f19b 100644 --- a/src/main/kotlin/crackers/hassk/HAssKClient.kt +++ b/src/main/kotlin/crackers/hassk/HAssKClient.kt @@ -25,6 +25,7 @@ import java.net.http.HttpClient import java.net.http.HttpRequest import java.net.http.HttpResponse import java.time.ZonedDateTime +import kotlin.math.roundToInt /** * Data about an entity's current state. @@ -73,8 +74,16 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123) * @param serviceType which service * @param serviceCommand the command */ - fun callService(entityId: String, serviceType: String, serviceCommand: String): String { - val payload = """{"entity_id":"$entityId"}""".trimIndent() + fun callService( + entityId: String, + serviceType: String, + serviceCommand: String, + extraData: Map = emptyMap() + ): String { + val payload = JSONObject().apply { + put("entity_id", entityId) + extraData.forEach { (k, v) -> put(k, v) } + }.toString() val uri = URI.create("$serverUri/services/$serviceType/$serviceCommand") val request = startRequest(uri) .POST(HttpRequest.BodyPublishers.ofString(payload)) @@ -152,6 +161,26 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123) return JSONArray(response).map { parseState(it as JSONObject) } } + /** + * Set a light level for a light/group. + * @param level 0-100 + * + * ```kotlin + * with(haClient) { + * light("foo") set 50 + * group("bar") set 0 + * } + * ``` + */ + infix fun Light.set(level: Int): List { + if (level <= 0) return turn(Constants.off) + + val actual = (level * 255f / 100).roundToInt().coerceIn(1, 255) + val brightness = mapOf("brightness" to actual) + val response = callService(entityId, "homeassistant", "turn_on", brightness) + return JSONArray(response).map { parseState(it as JSONObject) } + } + /** * Retrieves the state of the entity. */ diff --git a/version.properties b/version.properties index fb33e1b..20e5e65 100644 --- a/version.properties +++ b/version.properties @@ -1,8 +1,8 @@ #Generated by the Semver Plugin for Gradle -#Mon Oct 23 22:08:12 PDT 2023 +#Sat Dec 09 13:42:53 PST 2023 version.buildmeta= version.major=0 version.minor=0 -version.patch=2 +version.patch=3 version.prerelease= -version.semver=0.0.2 +version.semver=0.0.3