home-assistant-js-websocket

JavaScript websocket client for Home Assistant

OTHER License

Downloads
14.7K
Stars
271
Committers
24

Bot releases are visible (Hide)

home-assistant-js-websocket - 3.2.0

Published by release-drafter[bot] almost 6 years ago

Now possible to get collection objects, instead of just subscribing to them.

// Will only initialize one collection per connection.
getCollection<State>(
  conn: Connection,
  key: string,
  fetchCollection: (conn: Connection) => Promise<State>,
  subscribeUpdates: (
    conn: Connection,
    store: Store<State>
  ) => Promise<UnsubscribeFunc>,
): Collection<State>

// Returns object with following type
class Collection<State> {
  state: State;
  async refresh(): Promise<void>;
  subscribe(subscriber: (state: State) => void): UnsubscribeFunc;
}

You can also import the service, config and entities collections:

import { entitiesColl, servicesColl, configColl } from 'home-assistant-js-websocket';

Commits

  • Add base attributes type to base entity (#67) @balloob
  • Convert tests to TS (#69) @balloob
  • Update deps (#70) @balloob
  • Allow refreshing collections (#68) @balloob
  • Expose token tools (#73) @balloob
  • Don't always catch exceptions on refresh (#74) @balloob
home-assistant-js-websocket - 3.1.6

Published by release-drafter[bot] almost 6 years ago

What's Changed

  • Don't reject subEvents on close (#66) @balloob
home-assistant-js-websocket - 3.1.5

Published by release-drafter[bot] almost 6 years ago

What's Changed

  • Add more types (#61) @balloob
  • Handle collection refresh hitting closed socket (#63) @balloob
  • Better recover subscribeEvents (#62) @balloob
  • Add generic types (#64) @balloob
home-assistant-js-websocket - 3.1.4

Published by release-drafter[bot] about 6 years ago

What's Changed

  • Fix re-subscribing to all events after reconnect (#58) @balloob
home-assistant-js-websocket - 3.1.3

Published by release-drafter[bot] about 6 years ago

What's Changed

  • Make type of event type in subscribeEvents optional (#56) @balloob
home-assistant-js-websocket - 3.1.2

Published by release-drafter[bot] about 6 years ago

What's Changed

  • fetch call add credentials:'same-origin' to avoid omit cookie (#54) @yulongying
home-assistant-js-websocket - 3.1.1

Published by release-drafter[bot] about 6 years ago

What's Changed

  • Add option to revoke token (#53) @balloob
  • Send auth as soon as possible (#52) @balloob

3.1.0 was skipped because I accidentally pushed without updating dist files

home-assistant-js-websocket - 3.0.0

Published by release-drafter[bot] about 6 years ago

Major rewrite, cleanup and typescriptation of this lib. See release notes of the RCs for the ins and outs.

home-assistant-js-websocket - 3.0.0-rc5

Published by balloob about 6 years ago

  • Export more types (#48) @balloob
  • Extract commands + new getUser command exported (#49) @balloob

Breaking change

The following methods have been removed from the connection object and can now be imported individually: conn.getStates(), conn.getConfig(), conn.getServices(), conn.callService().

Replace calls with:

import { getServices } from 'home-assistant-js-websocket';
const services = await getServices(conn);
home-assistant-js-websocket - 3.0.0-rc4

Published by balloob about 6 years ago

What's Changed

  • Strip trailing slash from hassUrl (#39) @balloob
  • Don't lose refresh token when refreshing access token (#40) @balloob
  • Handle refreshing when server unavailable (#41) @balloob
  • Make clientID and redirectUri configurable (#44) @balloob
  • Renames (#47) @balloob
  • Remove extra 10s check on token expiration (#46) @balloob
home-assistant-js-websocket - 2.1.0

Published by balloob about 6 years ago

  • Check access token expires before we use (#21 - @awarecan)
  • Breaking Change Remove getting panels (#19 - @balloob)
home-assistant-js-websocket - 3.0.0-rc3

Published by release-drafter[bot] about 6 years ago

What's Changed

  • Expose createCollection (#34) @balloob
  • Fix UMD bundle to export to HAWS on window @balloob
home-assistant-js-websocket - 3.0.0-rc2

Published by release-drafter[bot] about 6 years ago

What's Changed

  • Fix expiration check (#31) @balloob
  • Enable strict mode (#32) @balloob
home-assistant-js-websocket - 3.0.0-rc1

Published by release-drafter[bot] about 6 years ago

Major upgrade of the lib.

The library will now only work with the new OAuth system in Home Assistant. It supports a full OAuth2 authentication flow with any Home Assistant instance out of the box.

The following code is all that is needed to ask the user for their Home Assistant instance url, navigate the user to their instance to authenticate your app, fetch the tokens once returned and connect to the WebSocket API and start logging the entities whenever they change.

import {
  getAuth,
  createConnection,
  subscribeEntities,
  ERR_HASS_HOST_REQUIRED
} from "home-assistant-js-websocket";

async function connect() {
  let auth;
  try {
    // If we came back from authentication flow, retrieve tokens and url
    auth = await getAuth();
  } catch (err) {
    if (err === ERR_HASS_HOST_REQUIRED) {
      // No host configured, ask user for host and initiate authentication flow
      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, ent => console.log(ent));
}

connect();

Other changes

  • Bring your own WebSocket with the new createSocket connection option. Defined as: (auth, options) => promise<WebSocket>
  • Convert to TypeScript
  • Clean up of the subscription methods
  • Use prettier
  • Bundling using microbundle
  • async / await ! (but converted to promises in the output)

Breaking changes

  • Requires Home Assistant 0.76 or later with the new auth system enabled
  • connection.sendMessagePromise will now resolve to the result instead of returning the WebSocket API wrapper.
    const response = conn.sendMessagePromise({ type: 'ping' });
    // < 3.0.0
    response.then(message => console.log(message.result))
    // 3.0.0
    response.then(result => console.log(result))
    
  • Drop support for passing in access token or API password
  • The config returned by subscribeConfig no longer contains the services. To subscribe to services use the new subscribeServices method.
  • Subscribe methods now return the unsubscribe function immediately instead of returning a promise that resolves to the unsubscribe function once the initial data load has been done.
home-assistant-js-websocket - 2.0.1

Published by balloob over 6 years ago

Mark package as side effect free to help with Webpack treeshaking (docs)

home-assistant-js-websocket - 2.0.0

Published by balloob over 6 years ago

Breaking change:

  • All group and view extraction helpers have been removed from this package (#16)
home-assistant-js-websocket - 1.1.4

Published by balloob over 6 years ago

  • Don't spam the console with errors when trying to reconnect (@andrey-git - #13)

(the 1.1.3 release should be ignored due to a package publish fail)

home-assistant-js-websocket - 1.1.2

Published by balloob about 7 years ago

  • Remove sorting groups from splitbyGroups (@abmantis - #9)
home-assistant-js-websocket - 1.1.0

Published by balloob over 7 years ago

  • subscribeConfig will now process service_removed events (@pvizeli)
home-assistant-js-websocket - 1.0.1

Published by balloob over 7 years ago

Fixes a bug that when extracting the entities of a view, we would incorrectly ignore the hidden attribute on group entities.