How to use the home-assistant-js-websocket.createConnection function in home-assistant-js-websocket

To help you get started, we’ve selected a few home-assistant-js-websocket examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zachowj / node-red-contrib-home-assistant-websocket / lib / ha-websocket.js View on Github external
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)
github home-assistant / home-assistant-polymer / src / onboarding / ha-onboarding.ts View on Github external
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));
  }
}
github stanford-oval / thingpedia-common-devices / io.home-assistant / index.js View on Github external
async _doStart() {
        try {
            this._connection = await HomeAssistant.createConnection({
                createSocket: this._createSocket.bind(this),
                setupRetry: 10,
            });
            await this._subdevices.start();
        } catch(e) {
            console.error(e);
        }
    }
github home-assistant / home-assistant-polymer / cast / src / receiver / layout / hc-main.ts View on Github external
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();
  }
github home-assistant / home-assistant-polymer / js / core.js View on Github external
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;
    });
};