Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async startListening(eventOpts = {}) {
this.config.websocket.includeRegex =
eventOpts.includeRegex || this.config.websocket.includeRegex;
this.config.websocket.excludeRegex =
eventOpts.excludeRegex || this.config.websocket.excludeRegex;
try {
this.client = await homeassistant.createConnection({
self: this,
createSocket: this.createSocket
});
this.onClientOpen();
this.emit('ha_client:connected');
} catch (e) {
this.connectionState = HaWebsocket.DISCONNECTED;
this.emit('ha_client:close');
return false;
}
// Client events
this.client.addEventListener('ready', this.onClientOpen.bind(this));
this.client.addEventListener(
'disconnected',
this.onClientClose.bind(this)
private async _connectHass(auth: Auth) {
const conn = await createConnection({ auth });
// Make sure config and user info is loaded before we initialize.
// It is needed for the core config step.
await Promise.all([
subscribeOne(conn, subscribeConfig),
subscribeOne(conn, subscribeUser),
]);
this.initializeHass(auth, conn);
// Load config strings for integrations
(this as any)._loadFragmentTranslations(this.hass!.language, "config");
// Make sure hass is initialized + the config/user callbacks have called.
await new Promise((resolve) => setTimeout(resolve, 0));
}
}
async _doStart() {
try {
this._connection = await HomeAssistant.createConnection({
createSocket: this._createSocket.bind(this),
setupRetry: 10,
});
await this._subdevices.start();
} catch(e) {
console.error(e);
}
}
loadTokens: async () => ({
hassUrl: msg.hassUrl,
clientId: msg.clientId,
refresh_token: msg.refreshToken,
access_token: "",
expires: 0,
expires_in: 0,
}),
});
} catch (err) {
this._error = this._getErrorMessage(err);
return;
}
let connection;
try {
connection = await createConnection({ auth });
} catch (err) {
this._error = this._getErrorMessage(err);
return;
}
if (this.hass) {
this.hass.connection.close();
}
this.initializeHass(auth, connection);
this._error = undefined;
this._sendStatus();
}
const init = window.createHassConnection = function (password) {
const proto = window.location.protocol === 'https:' ? 'wss' : 'ws';
const url = `${proto}://${window.location.host}/api/websocket?${window.HASS_BUILD}`;
const options = {
setupRetry: 10,
};
if (password !== undefined) {
options.authToken = password;
}
return HAWS.createConnection(url, options)
.then(function (conn) {
HAWS.subscribeEntities(conn);
HAWS.subscribeConfig(conn);
return conn;
});
};