mirror of
https://github.com/home-assistant/frontend.git
synced 2025-04-24 13:27:22 +00:00
Add connection events to bus (#3117)
* Add connection events * Fix types * Fix order
This commit is contained in:
parent
c260591d4d
commit
d05b1ef9cc
10
src/external_app/external_events_forwarder.ts
Normal file
10
src/external_app/external_events_forwarder.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { ExternalMessaging } from "./external_messaging";
|
||||
|
||||
export const externalForwardConnectionEvents = (bus: ExternalMessaging) => {
|
||||
document.addEventListener("connection-status", (ev) =>
|
||||
bus.fireMessage({
|
||||
type: "connection-status",
|
||||
payload: { event: ev.detail },
|
||||
})
|
||||
);
|
||||
};
|
@ -1,3 +1,5 @@
|
||||
import { externalForwardConnectionEvents } from "./external_events_forwarder";
|
||||
|
||||
const CALLBACK_EXTERNAL_BUS = "externalBus";
|
||||
|
||||
interface CommandInFlight {
|
||||
@ -8,6 +10,7 @@ interface CommandInFlight {
|
||||
export interface InternalMessage {
|
||||
id?: number;
|
||||
type: string;
|
||||
payload?: unknown;
|
||||
}
|
||||
|
||||
interface ExternalError {
|
||||
@ -37,6 +40,7 @@ export class ExternalMessaging {
|
||||
public msgId = 0;
|
||||
|
||||
public attach() {
|
||||
externalForwardConnectionEvents(this);
|
||||
window[CALLBACK_EXTERNAL_BUS] = (msg) => this.receiveMessage(msg);
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@ import { fetchWithAuth } from "../../util/fetch-with-auth";
|
||||
import hassCallApi from "../../util/hass-call-api";
|
||||
import { subscribePanels } from "../../data/ws-panels";
|
||||
import { forwardHaptic } from "../../util/haptics";
|
||||
import { fireEvent } from "../../common/dom/fire_event";
|
||||
|
||||
export default (superClass) =>
|
||||
class extends EventsMixin(LocalizeMixin(superClass)) {
|
||||
@ -128,11 +129,16 @@ export default (superClass) =>
|
||||
|
||||
const conn = this.hass.connection;
|
||||
|
||||
fireEvent(document, "connection-status", "connected");
|
||||
|
||||
conn.addEventListener("ready", () => this.hassReconnected());
|
||||
conn.addEventListener("disconnected", () => this.hassDisconnected());
|
||||
// If we reconnect after losing connection and auth is no longer valid.
|
||||
conn.addEventListener("reconnect-error", (_conn, err) => {
|
||||
if (err === ERR_INVALID_AUTH) location.reload();
|
||||
if (err === ERR_INVALID_AUTH) {
|
||||
fireEvent(document, "connection-status", "auth-invalid");
|
||||
location.reload();
|
||||
}
|
||||
});
|
||||
|
||||
subscribeEntities(conn, (states) => this._updateHass({ states }));
|
||||
@ -144,10 +150,12 @@ export default (superClass) =>
|
||||
hassReconnected() {
|
||||
super.hassReconnected();
|
||||
this._updateHass({ connected: true });
|
||||
fireEvent(document, "connection-status", "connected");
|
||||
}
|
||||
|
||||
hassDisconnected() {
|
||||
super.hassDisconnected();
|
||||
this._updateHass({ connected: false });
|
||||
fireEvent(document, "connection-status", "disconnected");
|
||||
}
|
||||
};
|
||||
|
10
src/types.ts
10
src/types.ts
@ -10,6 +10,7 @@ import {
|
||||
} from "home-assistant-js-websocket";
|
||||
import { LocalizeFunc } from "./common/translations/localize";
|
||||
import { ExternalMessaging } from "./external_app/external_messaging";
|
||||
import { HASSDomEvent } from "./common/dom/fire_event";
|
||||
|
||||
declare global {
|
||||
var __DEV__: boolean;
|
||||
@ -17,9 +18,7 @@ declare global {
|
||||
var __BUILD__: "latest" | "es5";
|
||||
var __VERSION__: string;
|
||||
var __STATIC_PATH__: string;
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
// Custom panel entry point url
|
||||
customPanelJS: string;
|
||||
@ -39,9 +38,16 @@ declare global {
|
||||
value: unknown;
|
||||
};
|
||||
change: undefined;
|
||||
"connection-status": ConnectionStatus;
|
||||
}
|
||||
|
||||
interface GlobalEventHandlersEventMap {
|
||||
"connection-status": HASSDomEvent<ConnectionStatus>;
|
||||
}
|
||||
}
|
||||
|
||||
type ConnectionStatus = "connected" | "auth-invalid" | "disconnected";
|
||||
|
||||
export interface WebhookError {
|
||||
code: number;
|
||||
message: string;
|
||||
|
Loading…
x
Reference in New Issue
Block a user