/node-homeassistant

Node.js wrapper for the home-assistant websocket api

Primary LanguageJavaScriptMIT LicenseMIT

Node.js api for home-assistant

A simple package to access & controll home-assistant from node.js using the websocket api.

Installation

$ npm install node-homeassistant

Usage

Create a new Homeassistant object:

const Homeassistant = require('node-homeassistant')

let ha = new Homeassistant({
  host: '192.168.1.166',
  protocol: 'ws', // "ws" (default) or "wss" for SSL
  retryTimeout: 1000, // in ms, default is 5000
  retryCount: 3, // default is 10, values < 0 mean unlimited
  password: 'http_api_password', // api_password is getting depricated by home assistant
  token: 'access_token' // for now both tokens and api_passwords are suported
  port: 8123
})

ha.connect().then(() => {
  // do stuff
})

Access & subscribe to states:

console.log(ha.state('sun.sun'))


ha.on('state:media_player.spotify', data => console.log)

Call services:

ha.call({
     domain: 'light',
     service: 'turn_on'
   })

You can subscribe to the 'connection' event to get information about the websocket connection.

ha.on('connection', info => {
  console.log('connection state is', info)
})

See the example folders for a working demo.

License

MIT