Skip to content

Commit

Permalink
Get all states
Browse files Browse the repository at this point in the history
Simple domain filtering as well.
  • Loading branch information
EAGrahamJr committed Dec 9, 2023
1 parent 7d7da8c commit 7dd0c6a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
29 changes: 26 additions & 3 deletions docs/crackers/hassk/HAssKClient.html
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ <h3>Method Summary</h3>
</td>
<th class="colSecond" scope="row"><code><a href=HAssKClient.html#turn(crackers.hassk.HAssKClient.Entity,java.lang.Boolean)>turn</a>(<a href=HAssKClient.Entity.html>HAssKClient.Entity</a> $self, <a href=https://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html>Boolean</a> on) </code>
</th>
<td class="colLast">Generic turn on/off<pre><code>with(haCliewnt) {
<td class="colLast">Generic turn on/off<pre><code>with(haClient) {
light(&quot;foo&quot;) turn on
group(&quot;bar&quot;) turn off
}</code></pre></td>
Expand Down Expand Up @@ -397,6 +397,14 @@ <h3>Method Summary</h3>
<td class="colLast">Create an entity in the &quot;sensor&quot; domain (do not prefix with &quot;sensor.</td>
</tr>

<tr id="i13" class="rowColor">
<td class="colFirst"><code>final <a href=https://docs.oracle.com/javase/8/docs/api/java/util/List.html>List</a>&lt;<a href=EntityState.html>EntityState</a>&gt;</code>
</td>
<th class="colSecond" scope="row"><code><a href=HAssKClient.html#states(java.lang.String)>states</a>(<a href=https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>String</a> domain) </code>
</th>
<td class="colLast">Uses the state JSON API to get a list of entities and their states.</td>
</tr>

</tbody>
</table>
</div>
Expand Down Expand Up @@ -593,7 +601,7 @@ <h4>sendIt</h4>
<li class="blockList">
<h4>turn</h4>
<pre class="methodSignature"> final <a href=https://docs.oracle.com/javase/8/docs/api/java/util/List.html>List</a>&lt;<a href=EntityState.html>EntityState</a>&gt; <a href=HAssKClient.html#turn(crackers.hassk.HAssKClient.Entity,java.lang.Boolean)>turn</a>(<a href=HAssKClient.Entity.html>HAssKClient.Entity</a> $self, <a href=https://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html>Boolean</a> on)</pre>
<div class="block"><p>Generic turn on/off</p><pre><code>with(haCliewnt) {
<div class="block"><p>Generic turn on/off</p><pre><code>with(haClient) {
light(&quot;foo&quot;) turn on
group(&quot;bar&quot;) turn off
}</code></pre></div>
Expand Down Expand Up @@ -717,7 +725,7 @@ <h4>switch</h4>
<!-- -->
</a>
<ul class=
"blockListLast"
"blockList"
>
<li class="blockList">
<h4>sensor</h4>
Expand All @@ -737,6 +745,21 @@ <h4>sensor</h4>
</li>
</ul>

<a id="states(java.lang.String)">
<!-- -->
</a>
<ul class=
"blockListLast"
>
<li class="blockList">
<h4>states</h4>
<pre class="methodSignature"> final <a href=https://docs.oracle.com/javase/8/docs/api/java/util/List.html>List</a>&lt;<a href=EntityState.html>EntityState</a>&gt; <a href=HAssKClient.html#states(java.lang.String)>states</a>(<a href=https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>String</a> domain)</pre>
<div class="block"><p>Uses the state JSON API to get a list of entities and their states. It is filtered by the optional <a href=HAssKClient.html#states(java.lang.String)>domain</a>.</p></div>

<!-- TODO missing return annotation -->
</li>
</ul>

</li>
</ul>
</section>
Expand Down
13 changes: 13 additions & 0 deletions docs/index-files/index-7.html
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ <h2 class="title">S</h2>
<dd>Retrieves the state of the entity.</dd>


<dt>

<span class="memberNameLink">

<a href="../crackers/hassk/HAssKClient.html#states(java.lang.String)">states(java.lang.String)</a>

</span> - function in crackers.hassk.<a href=../crackers/hassk/HAssKClient.html>HAssKClient</a>


</dt>
<dd>Uses the state JSON API to get a list of entities and their states.</dd>


<dt>

<span class="memberNameLink">
Expand Down
2 changes: 1 addition & 1 deletion docs/index-files/index-8.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ <h2 class="title">T</h2>


</dt>
<dd>Generic turn on/off<pre><code>with(haCliewnt) {
<dd>Generic turn on/off<pre><code>with(haClient) {
light(&quot;foo&quot;) turn on
group(&quot;bar&quot;) turn off
}</code></pre></dd>
Expand Down
2 changes: 1 addition & 1 deletion docs/member-search-index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 18 additions & 12 deletions src/main/kotlin/crackers/hassk/HAssKClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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))
}
}
Expand Down Expand Up @@ -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<EntityState> =
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<EntityState> {
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.
Expand Down Expand Up @@ -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<EntityState> {
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".
*
Expand Down

0 comments on commit 7dd0c6a

Please sign in to comment.