Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const conn = this.hass!.connection;
broadcastConnectionStatus("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) {
broadcastConnectionStatus("auth-invalid");
location.reload();
}
});
subscribeEntities(conn, (states) => this._updateHass({ states }));
subscribeConfig(conn, (config) => this._updateHass({ config }));
subscribeServices(conn, (services) => this._updateHass({ services }));
subscribePanels(conn, (panels) => this._updateHass({ panels }));
}
window.hassConnection.then(({ conn }) => {
const noop = () => {
// do nothing
};
subscribeEntities(conn, noop);
subscribeConfig(conn, noop);
subscribeServices(conn, noop);
subscribePanels(conn, noop);
subscribeThemes(conn, noop);
subscribeUser(conn, noop);
if (location.pathname === "/" || location.pathname.startsWith("/lovelace/")) {
(window as WindowWithLovelaceProm).llConfProm = fetchConfig(conn, false);
}
});
return false;
}
// Client events
this.client.addEventListener('ready', this.onClientOpen.bind(this));
this.client.addEventListener(
'disconnected',
this.onClientClose.bind(this)
);
this.client.addEventListener(
'reconnect-error',
this.onClientError.bind(this)
);
// Home Assistant Events
homeassistant.subscribeEntities(this.client, ent =>
this.onClientStates(ent)
);
homeassistant.subscribeServices(this.client, ent =>
this.onClientServices(ent)
);
homeassistant.subscribeConfig(this.client, config =>
this.onClientConfigUpdate(config)
);
return true;
}
connection.then(({ conn }) => {
localStorage.removeItem('auth_triggered');
this.setState({ connected: true });
conn.removeEventListener('ready', this.eventHandler);
conn.addEventListener('ready', this.eventHandler);
subscribeConfig(conn, this.updateConfig);
subscribeEntities(conn, this.updateEntities);
getUser(conn).then(user => {
console.log('Logged into Home Assistant as', user.name);
sessionStorage.setItem('hass_id', user.id);
});
connection = conn;
});
})();
auth = await getAuth({
hassUrl: props.url,
saveTokens: saveTokens,
loadTokens: () => Promise.resolve(loadTokens())
});
connection = await createConnection({ auth });
} catch (err) {
throw err;
}
}
props.setConnected(true);
connection.removeEventListener('ready', eventHandler);
connection.addEventListener('ready', eventHandler);
props.setAuth(auth);
subscribeConfig(connection, updateConfig);
subscribeEntities(connection, updateEntites);
getUser(connection).then((user: HassUser) => {
console.log('Logged into Home Assistant as', user.name);
});
})();
}, [props, updateConfig, updateEntites]);
.then(function (conn) {
HAWS.subscribeEntities(conn);
HAWS.subscribeConfig(conn);
return conn;
});
};