Split js parts

This commit is contained in:
2022-04-29 19:34:21 +00:00
parent edd03225d2
commit 69e9642b4b
14 changed files with 42 additions and 8 deletions

42
js/plugin/connection.ts Normal file
View File

@@ -0,0 +1,42 @@
import { deviceID } from "card-tools/src/deviceID";
import { hass, provideHass } from "card-tools/src/hass";
export class BrowserModConnection {
hass;
connection;
async connect() {
const isCast = document.querySelector("hc-main") !== null;
if (!isCast) {
while (!window.hassConnection)
await new Promise((resolve) => window.setTimeout(resolve, 100));
this.connection = (await window.hassConnection).conn;
} else {
this.connection = hass().connection;
}
this.connection.subscribeMessage((msg) => this.msg_callback(msg), {
type: "browser_mod/connect",
deviceID: deviceID,
});
provideHass(this);
}
get connected() {
return this.connection !== undefined;
}
msg_callback(message) {
console.log(message);
}
sendUpdate(data) {
if (!this.connected) return;
this.connection.sendMessage({
type: "browser_mod/update",
deviceID,
data,
});
}
}