Releases: home-assistant/home-assistant-js-websocket
Releases · home-assistant/home-assistant-js-websocket
2.1.0
3.0.0-rc3
3.0.0-rc2
3.0.0-rc1
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 newsubscribeServices
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.