mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-15 05:16:34 +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";
|
const CALLBACK_EXTERNAL_BUS = "externalBus";
|
||||||
|
|
||||||
interface CommandInFlight {
|
interface CommandInFlight {
|
||||||
@ -8,6 +10,7 @@ interface CommandInFlight {
|
|||||||
export interface InternalMessage {
|
export interface InternalMessage {
|
||||||
id?: number;
|
id?: number;
|
||||||
type: string;
|
type: string;
|
||||||
|
payload?: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ExternalError {
|
interface ExternalError {
|
||||||
@ -37,6 +40,7 @@ export class ExternalMessaging {
|
|||||||
public msgId = 0;
|
public msgId = 0;
|
||||||
|
|
||||||
public attach() {
|
public attach() {
|
||||||
|
externalForwardConnectionEvents(this);
|
||||||
window[CALLBACK_EXTERNAL_BUS] = (msg) => this.receiveMessage(msg);
|
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 hassCallApi from "../../util/hass-call-api";
|
||||||
import { subscribePanels } from "../../data/ws-panels";
|
import { subscribePanels } from "../../data/ws-panels";
|
||||||
import { forwardHaptic } from "../../util/haptics";
|
import { forwardHaptic } from "../../util/haptics";
|
||||||
|
import { fireEvent } from "../../common/dom/fire_event";
|
||||||
|
|
||||||
export default (superClass) =>
|
export default (superClass) =>
|
||||||
class extends EventsMixin(LocalizeMixin(superClass)) {
|
class extends EventsMixin(LocalizeMixin(superClass)) {
|
||||||
@ -128,11 +129,16 @@ export default (superClass) =>
|
|||||||
|
|
||||||
const conn = this.hass.connection;
|
const conn = this.hass.connection;
|
||||||
|
|
||||||
|
fireEvent(document, "connection-status", "connected");
|
||||||
|
|
||||||
conn.addEventListener("ready", () => this.hassReconnected());
|
conn.addEventListener("ready", () => this.hassReconnected());
|
||||||
conn.addEventListener("disconnected", () => this.hassDisconnected());
|
conn.addEventListener("disconnected", () => this.hassDisconnected());
|
||||||
// If we reconnect after losing connection and auth is no longer valid.
|
// If we reconnect after losing connection and auth is no longer valid.
|
||||||
conn.addEventListener("reconnect-error", (_conn, err) => {
|
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 }));
|
subscribeEntities(conn, (states) => this._updateHass({ states }));
|
||||||
@ -144,10 +150,12 @@ export default (superClass) =>
|
|||||||
hassReconnected() {
|
hassReconnected() {
|
||||||
super.hassReconnected();
|
super.hassReconnected();
|
||||||
this._updateHass({ connected: true });
|
this._updateHass({ connected: true });
|
||||||
|
fireEvent(document, "connection-status", "connected");
|
||||||
}
|
}
|
||||||
|
|
||||||
hassDisconnected() {
|
hassDisconnected() {
|
||||||
super.hassDisconnected();
|
super.hassDisconnected();
|
||||||
this._updateHass({ connected: false });
|
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";
|
} from "home-assistant-js-websocket";
|
||||||
import { LocalizeFunc } from "./common/translations/localize";
|
import { LocalizeFunc } from "./common/translations/localize";
|
||||||
import { ExternalMessaging } from "./external_app/external_messaging";
|
import { ExternalMessaging } from "./external_app/external_messaging";
|
||||||
|
import { HASSDomEvent } from "./common/dom/fire_event";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
var __DEV__: boolean;
|
var __DEV__: boolean;
|
||||||
@ -17,9 +18,7 @@ declare global {
|
|||||||
var __BUILD__: "latest" | "es5";
|
var __BUILD__: "latest" | "es5";
|
||||||
var __VERSION__: string;
|
var __VERSION__: string;
|
||||||
var __STATIC_PATH__: string;
|
var __STATIC_PATH__: string;
|
||||||
}
|
|
||||||
|
|
||||||
declare global {
|
|
||||||
interface Window {
|
interface Window {
|
||||||
// Custom panel entry point url
|
// Custom panel entry point url
|
||||||
customPanelJS: string;
|
customPanelJS: string;
|
||||||
@ -39,9 +38,16 @@ declare global {
|
|||||||
value: unknown;
|
value: unknown;
|
||||||
};
|
};
|
||||||
change: undefined;
|
change: undefined;
|
||||||
|
"connection-status": ConnectionStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface GlobalEventHandlersEventMap {
|
||||||
|
"connection-status": HASSDomEvent<ConnectionStatus>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ConnectionStatus = "connected" | "auth-invalid" | "disconnected";
|
||||||
|
|
||||||
export interface WebhookError {
|
export interface WebhookError {
|
||||||
code: number;
|
code: number;
|
||||||
message: string;
|
message: string;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user