Skip to content

Next gen#22

Merged
balloob merged 1 commit intomasterfrom
next-gen
Aug 6, 2018
Merged

Next gen#22
balloob merged 1 commit intomasterfrom
next-gen

Conversation

@balloob
Copy link
Copy Markdown
Member

@balloob balloob commented Jul 17, 2018

This is a preview of the next gen of this lib. Goal is that anyone can include this tiny lib and start building a UI for Home Assistant.

Changes:

  • Move bundler to microbundle
  • Drop support for API password
  • Add support for authorizing with an instance, fetching and refreshing tokens
  • Allow to pass in optional methods for saving/restoring tokens

to do:

  • break up subscribe config
  • typescript ?

Size:

Wrote 3.17 kB: haws.es.js
Wrote 3.16 kB: haws.js
Wrote 3.23 kB: haws.umd.js

Note: currently depends on a pending PR for Home Assistant that adds CORS to the /auth/token endpoint.

Example usage of this lib (also included as example.html to run locally):

This piece of code will:

  • Prompt user for the url of their Home Assistant instance
  • Redirect user to authorize url of instance and let user log in
  • Once logged in, will redirect user back to this page where tokens will be fetched and connection established
<html>
  <body>
    <script type='module'>
      import {
        getAuth,
        createConnection,
        subscribeEntities,
        ERR_HASS_HOST_REQUIRED,
      } from './dist/haws.es.js';

      (async () => {
        let auth;
        try {
          auth = await getAuth();
        } catch (err) {
          if (err === ERR_HASS_HOST_REQUIRED) {
            const hassUrl = prompt("What host to connect to?", "http://localhost:8123");
            auth = await getAuth({ hassUrl });
          } else {
            alert(`Unknown error: ${err}`);
            return;
          }
        }
        const connection = await createConnection(auth);
        subscribeEntities(connection, entities => console.log(entities));
      })();
    </script>
  </body>
</html>

@balloob balloob merged commit f33a59f into master Aug 6, 2018
@balloob balloob deleted the next-gen branch August 6, 2018 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants