The HomeAssistantAPI encapsulates the communication to the HomeAssistant instance.
Function | Description |
setup(url, port, token, force) |
Setup HomeAssistant configuration |
isValid() |
Return if HomeAssistantAPI has a valid configuration |
changeUrl(url) |
Change URL |
changePort(port) |
Change port number |
changeToken(token) |
Change access token |
changeForce(force) |
Change force update flag |
It uses the Fetch API. The asynchronous answer will be packed as JSON in a socket message.
Function | Response |
fetchApiStatus() |
Ok:
{
"key": "api",
"value": "ok",
"name": "location_name"
}
Error:
{
"key": "api",
"value": "error_message"
}
|
changeEntity(entity, state) |
{
"key": "change",
"id": "entity_id"
"state": "entity_state"
}
|
fetchEntity(entity) |
{
"key": "add",
"id": "entity_id",
"name": "entity_name"
"state": "entity_state"
}
|
flowchart TD;
Companion -- function --> HomeAssistantAPI;
HomeAssistantAPI -- request --> HomeAssistant;
HomeAssistant -- respond --> HomeAssistantAPI;
HomeAssistantAPI -- message --> App;
Companion
import { HomeAssistantAPI } from "./HomeAssistantAPI";
var HA = new HomeAssistantAPI();
HA.setup("127.0.0.1", "8123", "my_secret_access_token", false);
HA.fetchApiStatus();
HA.fetchEntity("switch.myswitch");
App
import * as messaging from "messaging";
messaging.peerSocket.onmessage = (evt) => {
if (evt.data.key === "api") {
if (evt.data.value === "ok") {
console.log("HomeAssistant available");
}
else {
console.log("HomeAssistant not available");
console.log(`Error: ${evt.data.value}`);
}
}
else if (evt.data.key === "add") {
console.log(`New entry: ${evt.data.name} = ${evt.data.state}`);
}
}