Skip to content

Instantly share code, notes, and snippets.

@zachowj
Created May 21, 2019 20:07
Show Gist options
  • Save zachowj/3bad002cd0ba3b5cd61dbfd47aecb3b5 to your computer and use it in GitHub Desktop.
Save zachowj/3bad002cd0ba3b5cd61dbfd47aecb3b5 to your computer and use it in GitHub Desktop.
const WebSocket = require("ws");
const hajw = require("home-assistant-js-websocket");
global.WebSocket = WebSocket;
const server = new WebSocket.Server({
port: 8081
});
server.on("connection", function connection(ws) {
console.log("\n");
ws.on("message", function incoming(message) {
console.log(`[Server] Received: ${message}`);
const json = JSON.parse(message);
if (json.type == "get_states") {
console.log(
"[Server] Closing Connection to Client (After subscribe_events received)"
);
ws.close();
}
});
ws.send(
JSON.stringify({
type: "auth_ok",
ha_version: "0.90.0"
})
);
});
// Catch and log unhandled Promise Rejection
process.on("unhandledRejection", (reason, p) => {
console.log("[Client][UnhandledRejection]", p);
process.exit(1);
});
const authObj = {
access_token: "",
wsUrl: "http://127.0.0.1:8081",
expires: new Date().getTime() + 1e11,
clientId: "",
expires_in: new Date().getTime() + 1e11,
refresh_token: ""
};
async function connect() {
try {
const conn = await hajw.createConnection({
auth: authObj
});
hajw.subscribeEntities(conn, entities => console.log(entities));
} catch (e) {
console.log("[Client] Error: ", e);
}
}
connect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment