2018-05-18 19:25:01 +02:00
|
|
|
import {
|
2018-08-31 09:45:58 +02:00
|
|
|
getAuth,
|
2018-05-18 19:25:01 +02:00
|
|
|
createConnection,
|
|
|
|
subscribeConfig,
|
|
|
|
subscribeEntities,
|
2018-08-31 09:45:58 +02:00
|
|
|
subscribeServices,
|
2018-09-07 19:40:56 +02:00
|
|
|
ERR_INVALID_AUTH,
|
2018-11-04 10:01:33 +01:00
|
|
|
Auth,
|
|
|
|
Connection,
|
2018-10-11 12:22:11 +02:00
|
|
|
} from "home-assistant-js-websocket";
|
2016-05-29 01:03:51 +02:00
|
|
|
|
2018-11-02 12:26:03 +01:00
|
|
|
import { loadTokens, saveTokens } from "../common/auth/token_storage";
|
|
|
|
import { subscribePanels } from "../data/ws-panels";
|
|
|
|
import { subscribeThemes } from "../data/ws-themes";
|
|
|
|
import { subscribeUser } from "../data/ws-user";
|
2018-11-04 10:01:33 +01:00
|
|
|
import { HomeAssistant } from "../types";
|
2019-05-08 07:27:10 +02:00
|
|
|
import { hassUrl } from "../data/auth";
|
2019-06-14 22:30:20 +02:00
|
|
|
import { fetchConfig, WindowWithLovelaceProm } from "../data/lovelace";
|
2018-11-04 10:01:33 +01:00
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
hassConnection: Promise<{ auth: Auth; conn: Connection }>;
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 20:25:36 +02:00
|
|
|
|
2019-11-28 00:44:59 +01:00
|
|
|
const isExternal =
|
|
|
|
window.externalApp ||
|
|
|
|
window.webkit?.messageHandlers?.getExternalAuth ||
|
|
|
|
location.search.includes("external_auth=1");
|
2018-09-03 18:00:39 +02:00
|
|
|
|
2018-09-19 15:15:16 +02:00
|
|
|
const authProm = isExternal
|
2018-10-11 12:22:11 +02:00
|
|
|
? () =>
|
2019-11-27 22:51:03 +01:00
|
|
|
import(
|
|
|
|
/* webpackChunkName: "external_auth" */ "../external_app/external_auth"
|
|
|
|
).then(({ createExternalAuth }) => createExternalAuth(hassUrl))
|
2018-10-11 12:22:11 +02:00
|
|
|
: () =>
|
|
|
|
getAuth({
|
|
|
|
hassUrl,
|
|
|
|
saveTokens,
|
|
|
|
loadTokens: () => Promise.resolve(loadTokens()),
|
|
|
|
});
|
2018-05-10 20:25:36 +02:00
|
|
|
|
2018-09-07 19:40:56 +02:00
|
|
|
const connProm = async (auth) => {
|
|
|
|
try {
|
|
|
|
const conn = await createConnection({ auth });
|
|
|
|
|
|
|
|
// Clear url if we have been able to establish a connection
|
2018-10-11 12:22:11 +02:00
|
|
|
if (location.search.includes("auth_callback=1")) {
|
2018-11-04 10:01:33 +01:00
|
|
|
history.replaceState(null, "", location.pathname);
|
2018-09-07 19:40:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return { auth, conn };
|
|
|
|
} catch (err) {
|
|
|
|
if (err !== ERR_INVALID_AUTH) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
// We can get invalid auth if auth tokens were stored that are no longer valid
|
|
|
|
// Clear stored tokens.
|
2018-11-04 10:01:33 +01:00
|
|
|
if (!isExternal) {
|
|
|
|
saveTokens(null);
|
|
|
|
}
|
2018-09-07 19:40:56 +02:00
|
|
|
auth = await authProm();
|
|
|
|
const conn = await createConnection({ auth });
|
|
|
|
return { auth, conn };
|
2018-05-10 20:25:36 +02:00
|
|
|
}
|
2018-09-07 19:40:56 +02:00
|
|
|
};
|
|
|
|
|
2019-06-14 22:30:20 +02:00
|
|
|
if (__DEV__) {
|
|
|
|
performance.mark("hass-start");
|
|
|
|
}
|
2018-09-07 19:40:56 +02:00
|
|
|
window.hassConnection = authProm().then(connProm);
|
2018-05-10 20:25:36 +02:00
|
|
|
|
2018-08-31 09:45:58 +02:00
|
|
|
// Start fetching some of the data that we will need.
|
2018-09-07 19:40:56 +02:00
|
|
|
window.hassConnection.then(({ conn }) => {
|
2018-11-04 10:01:33 +01:00
|
|
|
const noop = () => {
|
|
|
|
// do nothing
|
|
|
|
};
|
2018-08-31 09:45:58 +02:00
|
|
|
subscribeEntities(conn, noop);
|
|
|
|
subscribeConfig(conn, noop);
|
|
|
|
subscribeServices(conn, noop);
|
|
|
|
subscribePanels(conn, noop);
|
|
|
|
subscribeThemes(conn, noop);
|
|
|
|
subscribeUser(conn, noop);
|
2019-06-14 22:30:20 +02:00
|
|
|
|
|
|
|
if (location.pathname === "/" || location.pathname.startsWith("/lovelace/")) {
|
|
|
|
(window as WindowWithLovelaceProm).llConfProm = fetchConfig(conn, false);
|
|
|
|
}
|
2018-08-31 09:45:58 +02:00
|
|
|
});
|
2018-01-27 20:50:13 +01:00
|
|
|
|
2018-10-11 12:22:11 +02:00
|
|
|
window.addEventListener("error", (e) => {
|
2018-11-04 10:01:33 +01:00
|
|
|
const homeAssistant = document.querySelector("home-assistant") as any;
|
|
|
|
if (
|
|
|
|
homeAssistant &&
|
|
|
|
homeAssistant.hass &&
|
|
|
|
(homeAssistant.hass as HomeAssistant).callService
|
|
|
|
) {
|
2018-10-11 12:22:11 +02:00
|
|
|
homeAssistant.hass.callService("system_log", "write", {
|
|
|
|
logger: `frontend.${
|
|
|
|
__DEV__ ? "js_dev" : "js"
|
|
|
|
}.${__BUILD__}.${__VERSION__.replace(".", "")}`,
|
2018-01-27 20:50:13 +01:00
|
|
|
message: `${e.filename}:${e.lineno}:${e.colno} ${e.message}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|