diff --git a/docs/crackers/hassk/HAssKClient.html b/docs/crackers/hassk/HAssKClient.html index 8c41ded..f56fc17 100644 --- a/docs/crackers/hassk/HAssKClient.html +++ b/docs/crackers/hassk/HAssKClient.html @@ -343,7 +343,7 @@
turn(HAssKClient.Entity $self, Boolean on)
with(haCliewnt) {
+ Generic turn on/offwith(haClient) {
light("foo") turn on
group("bar") turn off
}
@@ -397,6 +397,14 @@ Method Summary
Create an entity in the "sensor" domain (do not prefix with "sensor.
+
+ final List<EntityState>
+
+ states(String domain)
+
+ Uses the state JSON API to get a list of entities and their states.
+
+
@@ -593,7 +601,7 @@ sendIt
turn
final List<EntityState> turn(HAssKClient.Entity $self, Boolean on)
- Generic turn on/off
with(haCliewnt) {
+ Generic turn on/off
with(haClient) {
light("foo") turn on
group("bar") turn off
}
@@ -717,7 +725,7 @@ switch
-
sensor
@@ -737,6 +745,21 @@ sensor
+
+
+
+
+ -
+
states
+ final List<EntityState> states(String domain)
+ Uses the state JSON API to get a list of entities and their states. It is filtered by the optional domain.
+
+
+
+
+
diff --git a/docs/index-files/index-7.html b/docs/index-files/index-7.html
index 2f7f849..dc089a4 100644
--- a/docs/index-files/index-7.html
+++ b/docs/index-files/index-7.html
@@ -179,6 +179,19 @@ S
Retrieves the state of the entity.
+
+
+
+
+ states(java.lang.String)
+
+ - function in crackers.hassk.HAssKClient
+
+
+
+ Uses the state JSON API to get a list of entities and their states.
+
+
diff --git a/docs/index-files/index-8.html b/docs/index-files/index-8.html
index 5d5e020..316dca0 100644
--- a/docs/index-files/index-8.html
+++ b/docs/index-files/index-8.html
@@ -124,7 +124,7 @@ T
- Generic turn on/offwith(haCliewnt) {
+ Generic turn on/offwith(haClient) {
light("foo") turn on
group("bar") turn off
}
diff --git a/docs/member-search-index.js b/docs/member-search-index.js
index 10b8608..b44c8e6 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":"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)","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"}]
diff --git a/src/main/kotlin/crackers/hassk/HAssKClient.kt b/src/main/kotlin/crackers/hassk/HAssKClient.kt
index 1a33e57..54b6b2a 100644
--- a/src/main/kotlin/crackers/hassk/HAssKClient.kt
+++ b/src/main/kotlin/crackers/hassk/HAssKClient.kt
@@ -64,7 +64,7 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123)
protected val client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.build()
- protected val logger = LoggerFactory.getLogger(this::class.java)
+ protected val logger = LoggerFactory.getLogger(javaClass.simpleName)
/**
* Set up generic service call that uses the entity ID as the payload.
@@ -79,7 +79,7 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123)
val request = startRequest(uri)
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build().also {
- logger.debug("Request: $it")
+ logger.debug("Request: {}", uri)
logger.debug("Payload: $payload")
}
return sendIt(request)
@@ -96,7 +96,7 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123)
.GET()
.build()
return sendIt(request).let {
- logger.debug("Sending $it")
+ logger.debug("Receiving: $it")
parseState(JSONObject(it))
}
}
@@ -141,20 +141,16 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123)
* Generic turn on/off
*
* ```kotlin
- * with(haCliewnt) {
+ * with(haClient) {
* light("foo") turn on
* group("bar") turn off
* }
* ```
*/
- infix fun Entity.turn(on: Boolean): List =
- callService(entityId, "homeassistant", "turn_${if (on) "on" else "off"}").let {
- JSONArray(it).map {
-// val entity = it as JSONObject
-// val isGroup = entity.getJSONObject("attributes").has("entity_id")
- parseState(it as JSONObject)
- }
- }
+ infix fun Entity.turn(on: Boolean): List {
+ val response = callService(entityId, "homeassistant", "turn_${if (on) "on" else "off"}")
+ return JSONArray(response).map { parseState(it as JSONObject) }
+ }
/**
* Retrieves the state of the entity.
@@ -201,6 +197,16 @@ open class HAssKClient(val token: String, haServer: String, haPort: Int = 8123)
*/
fun sensor(name: String) = Sensor(name)
+ /**
+ * 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 {
+ val response = sendIt(startRequest(URI.create("$serverUri/states")).build())
+ return JSONArray(response)
+ .map { parseState(it as JSONObject) }
+ .filter { domain == null || it.entityId.startsWith("$domain.") }
+ }
+
/**
* Basic "thing".
*