Open
Description
I am doing the authentication myself with a long lived access token (also used as bearer token for the REST API).
function createSocket(): Promise < HaWebSocket > {
return new Promise((resolve, reject) => {
const ws = new WebSocket(this.host.replace(/^http/, "ws") + "/api/websocket");
ws.on('error', (buffer) => {
console.error(`Error connecting to Home Assistant: ${buffer.toString()}`);
reject()
});
ws.on('message', (buffer) => {
const data = JSON.parse(buffer);
if (data.type === "auth_ok") {
ws.haVersion = data.ha_version;
resolve(ws);
} else if (data.type === "auth_required") {
ws.send(JSON.stringify({
type: "auth",
access_token: this.token
}));
} else if (data.type === "auth_invalid") {
console.error(`Error authenticating to Home Assistant: ${data.message}`);
reject()
}
});
})
}
this.wsConnection = await createConnection({
createSocket: createSocket.bind(this)
});
The line ws.haVersion = data.ha_version;
is very important before resolving the promise, otherwise there will be bugs down the line.
You should mention in your docs that you createSocket needs to return Promise < HaWebSocket >
, i.e. an object with interface WebSocket
extended by the haVersion
field as described in HaWebSocket
Metadata
Metadata
Assignees
Labels
No labels