Giter Club home page Giter Club logo

homeassistant-ws's Introduction

homeassistant-ws

npm GitHub

Minimalist client library for Homeassistant's Websocket API. Works in node, and also in the browser.


Installation:

Using npm:

$ npm i --save homeassistant-ws

Import it in your project:

import hass from 'homeassistant-ws';

async function main() {
  // Assuming hass running in `localhost`, under the default `8321` port:
  const client = await hass({
    token: 'my-secret-token',
  });
}

Tokens are available from your profile page under the Homeassistant UI. For documentation on the authentication API, see the official HA documentation.

Configuration options

The following properties (shown with their defaults) can be passed to the constructor. All are optional.

hass({
  protocol: 'ws',
  host: 'localhost',
  port: 8123,
  path: '/api/websocket',

  // Must be set if HA expects authentication:
  token: null,

  // Used to serialize outgoing messages:
  messageSerializer: (outgoingMessage) => JSON.stringify(outgoingMessage),

  // Used to parse incoming messages. Receives the entire Websocket message object:
  messageParser: (incomingMessage) => JSON.parse(incomingMessage.data),

  // Should return a WebSocket instance
  ws: (opts) => {
    return new WebSocket(
      `${opts.protocol}://${opts.host}:${opts.port}${opts.path}`
    );
  },
});

Example

The following example includes all available methods. For more details on available Homeassistant event types, states, etc. see the official Websocket API

import hass from 'hass';

async function main() {
  // Establishes a connection, and authenticates if necessary:
  const client = await hass({ token: 'my-token' });

  // Get a list of all available states, panels or services:
  await client.getStates();
  await client.getServices();
  await client.getPanels();

  // Get hass configuration:
  await client.getConfig();

  // Get a Buffer containing the current thumbnail for the given media player
  await client.getMediaPlayerThumbnail('media_player.my_player');
  // { content_type: 'image/jpeg', content: Buffer<...>}

  // Get a Buffer containing a thumbnail for the given camera
  await client.getCameraThumbnail('camera.front_yard');
  // { content_type: 'image/jpeg', content: Buffer<...>}

  // Call a service, by its domain and name. The third argument is optional.
  await client.callService('lights', 'turn_on', {
    entity_id: 'light.my_light',
  });

  // Listen for all HASS events - the 'message' event is a homeassistant-ws event triggered for
  // all messages received through the websocket connection with HASS:
  //
  // See https://developers.home-assistant.io/docs/api/websocket/ for details on HASS events:
  client.on('message', (rawMessageData) => {
    console.log(rawMessageData);
  });

  // Listen only for state changes:
  client.on('state_changed', (stateChangedEvent) => {
    console.log(stateChangedEvent.data.new_state.state);
  });
}

homeassistant-ws's People

Contributors

filp avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.