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-05-18 19:25:01 +02:00
|
|
|
} from 'home-assistant-js-websocket';
|
2016-05-29 01:03:51 +02:00
|
|
|
|
2018-08-31 09:45:58 +02:00
|
|
|
import { loadTokens, saveTokens } from '../common/auth/token_storage.js';
|
|
|
|
import { subscribePanels } from '../data/ws-panels.js';
|
|
|
|
import { subscribeThemes } from '../data/ws-themes.js';
|
|
|
|
import { subscribeUser } from '../data/ws-user.js';
|
2018-05-10 20:25:36 +02:00
|
|
|
|
2018-09-03 18:00:39 +02:00
|
|
|
const hassUrl = `${location.protocol}//${location.host}`;
|
2018-09-07 19:40:56 +02:00
|
|
|
const isExternal = 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
|
|
|
|
? () => import('../common/auth/external_auth.js')
|
|
|
|
.then(mod => new mod.default(hassUrl))
|
|
|
|
: () => getAuth({
|
2018-09-03 18:00:39 +02:00
|
|
|
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
|
|
|
|
if (location.search.includes('auth_callback=1')) {
|
|
|
|
history.replaceState(null, null, location.pathname);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
if (!isExternal) saveTokens(null);
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
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-08-31 09:45:58 +02:00
|
|
|
const noop = () => {};
|
|
|
|
subscribeEntities(conn, noop);
|
|
|
|
subscribeConfig(conn, noop);
|
|
|
|
subscribeServices(conn, noop);
|
|
|
|
subscribePanels(conn, noop);
|
|
|
|
subscribeThemes(conn, noop);
|
|
|
|
subscribeUser(conn, noop);
|
|
|
|
});
|
2018-01-27 20:50:13 +01:00
|
|
|
|
|
|
|
window.addEventListener('error', (e) => {
|
|
|
|
const homeAssistant = document.querySelector('home-assistant');
|
|
|
|
if (homeAssistant && homeAssistant.hass && homeAssistant.hass.callService) {
|
|
|
|
homeAssistant.hass.callService('system_log', 'write', {
|
2018-05-16 13:54:29 +02:00
|
|
|
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}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|