homeassistant-ws
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -20,4 +20,4 @@ "use strict"; | ||
token: null, | ||
messageSerializer: fullMessage => JSON.stringify(fullMessage), | ||
messageParser: fullMessage => JSON.parse(fullMessage.data), | ||
messageSerializer: outgoingMessage => JSON.stringify(outgoingMessage), | ||
messageParser: incomingMessage => JSON.parse(incomingMessage.data), | ||
@@ -164,3 +164,3 @@ // A method that returns a websocket instance. Can be overriden to use a custom behavior: | ||
client.ws.onerror = err => reject(new Error(err)); | ||
client.ws.onerror = err => reject(err); | ||
@@ -167,0 +167,0 @@ client.emitter.on('auth_ok', () => resolve(resolveWith)); |
{ | ||
"name": "homeassistant-ws", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"license": "MIT", | ||
"description": "Client for Homeassistant's websocket API", | ||
"keywords": ["homeassistant", "websocket", "hass", "ha", "smart", "home", "api", "hassio"], | ||
"keywords": [ | ||
"homeassistant", | ||
"websocket", | ||
"hass", | ||
"ha", | ||
"smart", | ||
"home", | ||
"api", | ||
"hassio" | ||
], | ||
"homepage": "https://github.com/filp/homeassistant-ws", | ||
"main": "dist/index.js", | ||
"types": "types.d.ts", | ||
"bugs": { | ||
@@ -15,5 +25,5 @@ "url": "https://github.com/filp/homeassistant-ws/issues" | ||
"url": "https://github.com/filp/homeassistant-ws.git" | ||
}, | ||
}, | ||
"scripts": { | ||
"start": "babel-node src/test.js", | ||
"start": "babel-node src/index.js", | ||
"build": "babel src/ --out-dir=dist/", | ||
@@ -20,0 +30,0 @@ "prepublish": "npm run build" |
# homeassistant-ws | ||
[![npm](https://img.shields.io/npm/v/homeassistant-ws?color=%23ff11dd&style=flat-square)](https://www.npmjs.com/package/homeassistant-ws) | ||
[![GitHub](https://img.shields.io/github/license/filp/homeassistant-ws?style=flat-square)](https://github.com/filp/homeassistant-ws/blob/master/LICENSE.md) | ||
Minimalist client library for [Homeassistant's Websocket API](https://developers.home-assistant.io/docs/external_api_websocket). Works in node, and also in the browser. | ||
@@ -22,3 +25,3 @@ | ||
// Assuming hass running in `localhost`, under the default `8321` port: | ||
const client = hass({ | ||
const client = await hass({ | ||
token: 'my-secret-token' | ||
@@ -30,1 +33,70 @@ }) | ||
Tokens are available from your profile page under the Homeassistant UI. For documentation on the authentication API, see [the official HA documentation](https://developers.home-assistant.io/docs/auth_api/). | ||
## Configuration options | ||
The following properties (shown with their defaults) can be passed to the constructor. All are optional. | ||
```js | ||
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](https://developers.home-assistant.io/docs/external_api_websocket) | ||
```js | ||
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' }) | ||
// Register event handlers. Return a promise since we need to call the HA API to | ||
// subscribe to events the first time. | ||
await client.onAnyEvent((event) => console.log(event)) | ||
await client.onStateChanged((event) => console.log(event)) | ||
await client.onEvent('event_name', (event) => console.log(event)) | ||
// Tell HA to stop sending events of the following type: | ||
await client.unsubscribeFromEvent('event_name') | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12729
7
199
101