Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation of home-assistant-js-websocket #37

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from

Conversation

robvanoostenrijk
Copy link
Collaborator

@robvanoostenrijk robvanoostenrijk commented Feb 9, 2025

Future extensibility of this plugin depends on a solid integration with the Home Assistant WebSocket API.

Home Assistant publishes their own WebSocket client communication module here:
https://github.com/home-assistant/home-assistant-js-websocket/tree/master

It comes as a typescript package without any dependencies. The only issue with regards to NodeJS is that it require a browser Websocket implementation.

However, home-assistant-js-websocket allows overriding the socket creation function, giving this opportunity to supply an alternative class using NodeJS native ws package.

Simple example usage:

// NodeJS implementation of WebSocket for home-assistant-js-websocket
import { createSocket } from './socket.js'

import {
    createConnection,
    subscribeEntities,
    createLongLivedTokenAuth,
  } from "home-assistant-js-websocket";
  import * as messages from 'home-assistant-js-websocket/dist/messages.js';

  (async () => {
    try
    {
        const auth = createLongLivedTokenAuth(
           "http://ha.internal",
           "ey........sjjo",
        );

        const connOptions = Object.assign({ setupRetry: 0, auth: auth, createSocket });
        const conn = await createConnection(connOptions);
      
        const config = await conn.sendMessagePromise(messages.config());
        console.log(config);

        // Custom message
        const voice_entities = await conn.sendMessagePromise({ "type": "homeassistant/expose_entity/list"});
        console.log(voice_entities);

        conn.subscribeEvents((event) => console.log(event), "state_changed");
        subscribeEntities(conn, (entities) => console.log(entities));
    }
    catch (e) {
        console.log(e);
        console.trace();
    }

  })();

This pull request refactors homeAssistant.ts to use the home-assistant-js-websocket implementation.
As-is its starting up and operational with matterbridge, however I'd need your help to ensure everything is covered.

@robvanoostenrijk
Copy link
Collaborator Author

Based on your feedback, significantly reduced library integration and aligned with the original design direction

@robvanoostenrijk
Copy link
Collaborator Author

Ended up removing the entire library and rather inherit the Connection and Socket classes instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant