Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

home-assistant-js-websocket

Package Overview
Dependencies
Maintainers
1
Versions
102
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

home-assistant-js-websocket - npm Package Compare versions

Comparing version 0.1.4 to 0.2.0

303

dist/haws.cjs.js

@@ -1,302 +0,1 @@

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function auth(authToken) {
return {
type: 'auth',
api_password: authToken,
};
}
function states() {
return {
type: 'get_states',
};
}
function config() {
return {
type: 'get_config',
};
}
function services() {
return {
type: 'get_services',
};
}
function panels() {
return {
type: 'get_panels',
};
}
function callService$1(domain, service, serviceData) {
var message = {
type: 'call_service',
domain: domain,
service: service,
};
if (serviceData) {
message.service_data = serviceData;
}
return message;
}
function subscribeEvents$1(eventType) {
var message = {
type: 'subscribe_events',
};
if (eventType) {
message.event_type = eventType;
}
return message;
}
function unsubscribeEvents(subscription) {
return {
type: 'unsubscribe_events',
subscription: subscription,
};
}
function ping$1() {
return {
type: 'ping',
};
}
var ERR_CANNOT_CONNECT = 1;
var ERR_INVALID_AUTH = 2;
function extractResult(message) {
return message.result;
}
var Connection = function Connection(url, options) {
this.url = url;
this.options = options || {};
this.commandId = 1;
this.commands = {};
this.connectionTries = 0;
this.eventListeners = {};
this.closeRequested = false;
};
Connection.prototype.addEventListener = function addEventListener (eventType, callback) {
var listeners = this.eventListeners[eventType];
if (!listeners) {
listeners = this.eventListeners[eventType] = [];
}
listeners.push(callback);
};
Connection.prototype.fireEvent = function fireEvent (eventType) {
var this$1 = this;
(this.eventListeners[eventType] || []).forEach(function (callback) { return callback(this$1); });
};
Connection.prototype.connect = function connect () {
var this$1 = this;
return new Promise(function (resolve, reject) {
// Used for resubscribing in the future
var oldCommands = this$1.commands;
Object.keys(oldCommands).forEach(function (id) {
var info = oldCommands[id];
// Reject stuff still waiting for an answer
if (info.reject) {
info.reject();
}
});
// If invalid auth, we will not try to reconnect.
var invalidAuth = false;
this$1.connectionTries += 1;
this$1.socket = new WebSocket(this$1.url);
this$1.socket.addEventListener('open', function () {
this$1.connectionTries = 0;
});
this$1.socket.addEventListener('message', function (event) {
var message = JSON.parse(event.data);
{
/* eslint-disable no-console */
console.log('Received', message);
/* eslint-enable no-console */
}
switch (message.type) {
case 'event':
this$1.commands[message.id].eventCallback(message.event);
break;
case 'result':
if (message.success) {
this$1.commands[message.id].resolve(message);
} else {
this$1.commands[message.id].reject(message.error);
}
delete this$1.commands[message.id];
break;
case 'pong':
break;
case 'auth_required':
this$1.sendMessage(auth(this$1.options.authToken));
break;
case 'auth_invalid':
reject({ code: ERR_INVALID_AUTH });
invalidAuth = true;
break;
case 'auth_ok':
resolve(this$1);
this$1.fireEvent('ready');
// Re-subscribe to events and update old location of unsub method
// so old unsub method keeps working.
this$1.commandId = 1;
this$1.commands = {};
Object.keys(oldCommands).forEach(function (id) {
var info = oldCommands[id];
if (info.eventType) {
this$1.subscribeEvents(info.eventCallback, info.eventType)
.then(function (unsub) { info.unsubscribe = unsub; });
}
});
break;
default:
{
/* eslint-disable no-console */
console.warn('Unhandled message', message);
/* eslint-enable no-console */
}
}
});
this$1.socket.addEventListener('close', function () {
if (invalidAuth || this$1.closeRequested) {
// When we have invalid auth, let's not reconnect or we get banned.
return;
} else if (this$1.connectionTries === 0) {
// We were connected at some point.
this$1.fireEvent('disconnected');
} else {
// We never were connected
reject(ERR_CANNOT_CONNECT);
}
// Try again
var waitTime = Math.min(this$1.connectionTries, 5) * 1000;
setTimeout(function () { return this$1.connect(); }, waitTime);
});
});
};
Connection.prototype.close = function close () {
this.closeRequested = true;
this.socket.close();
};
Connection.prototype.getStates = function getStates () {
return this.sendMessagePromise(states()).then(extractResult);
};
Connection.prototype.getServices = function getServices () {
return this.sendMessagePromise(services()).then(extractResult);
};
Connection.prototype.getPanels = function getPanels () {
return this.sendMessagePromise(panels()).then(extractResult);
};
Connection.prototype.getConfig = function getConfig () {
return this.sendMessagePromise(config()).then(extractResult);
};
Connection.prototype.callService = function callService (domain, service, serviceData) {
return this.sendMessagePromise(callService$1(domain, service, serviceData));
};
// eventCallback will be called when a new event fires
// Returned promise resolves to an unsubscribe function.
Connection.prototype.subscribeEvents = function subscribeEvents (eventCallback, eventType) {
var this$1 = this;
return this.sendMessagePromise(subscribeEvents$1(eventType)).then(
function (resultMessage) {
// We store unsubscribe on info object. That way we can overwrite it in case
// we get disconnected and we have to subscribe again.
var info = {
eventCallback: eventCallback,
eventType: eventType,
unsubscribe: function () { return this$1.sendMessagePromise(unsubscribeEvents(resultMessage.id))
.then(function () { delete this$1.commands[resultMessage.id]; }); },
};
this$1.commands[resultMessage.id] = info;
return function () { return info.unsubscribe(); };
/* eslint-disable comma-dangle */ /* comma crashes Buble */
}
/* eslint-enable comma-dangle */
);
};
Connection.prototype.ping = function ping () {
return this.sendMessagePromise(ping$1());
};
Connection.prototype.sendMessage = function sendMessage (message) {
{
/* eslint-disable no-console */
console.log('Sending', message);
/* eslint-enable no-console */
}
this.socket.send(JSON.stringify(message));
};
Connection.prototype.sendMessagePromise = function sendMessagePromise (message) {
var this$1 = this;
return new Promise(function (resolve, reject) {
this$1.commandId += 1;
var commandId = this$1.commandId;
/* eslint-disable no-param-reassign */
message.id = commandId;
/* eslint-enable no-param-reassign */
this$1.commands[commandId] = { resolve: resolve, reject: reject };
this$1.sendMessage(message);
});
};
function createConnection(url, options) {
var conn = new Connection(url, options);
return conn.connect();
}
exports.ERR_CANNOT_CONNECT = ERR_CANNOT_CONNECT;
exports.ERR_INVALID_AUTH = ERR_INVALID_AUTH;
exports.createConnection = createConnection;
exports['default'] = createConnection;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjpudWxsLCJzb3VyY2VzIjpbIi4uL2xpYi9tZXNzYWdlcy5qcyIsIi4uL2xpYi9pbmRleC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZnVuY3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SAwO1xuICAgICAgfSk7XG5cbiAgICAgIHRoaXMuc29ja2V0LmFkZEV2ZW50TGlzdGVuZXIoJ21lc3NhZ2UnLCAoZXZlbnQpID0+IHtcbiAgICAgICAgY29uc3QgbWVzc2FnZSA9IEpTT04ucGFyc2UoZXZlbnQuZGF0YSk7XG5cbiAgICAgICAgaWYgKF9fREVWX18pIHtcbiAgICAgICAgICAvKiBlc2xpbnQtZGlzYWJsZSBuby1jb25zb2xlICovXG4gICAgICAgICAgY29uc29sZS5sb2coJ1JlY2VpdmVkJywgbWVzc2FnZSk7XG4gICAgICAgICAgLyogZXNsaW50LWVuYWJsZSBuby1jb25zb2xlICovXG4gICAgICAgIH1cblxuICAgICAgICBzd2l0Y2ggKG1lc3NhZ2UudHlwZSkge1xuICAgICAgICAgIGNhc2UgJ2V2ZW50JzpcbiAgICAgICAgICAgIHRoaXMuY29tbWFuZHNbbWVzc2FnZS5pZF0uZXZlbnRDYWxsYmFjayhtZXNzYWdlLmV2ZW50KTtcbiAgICAgICAgICAgIGJyZWFrO1xuXG4gICAgICAgICAgY2FzZSAncmVzdWx0JzpcbiAgICAgICAgICAgIGlmIChtZXNzYWdlLnN1Y2Nlc3MpIHtcbiAgICAgICAgICAgICAgdGhpcy5jb21tYW5kc1ttZXNzYWdlLmlkXS5yZXNvbHZlKG1lc3NhZ2UpO1xuICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgdGhpcy5jb21tYW5kc1ttZXNzYWdlLmlkXS5yZWplY3QobWVzc2FnZS5lcnJvcik7XG4gICAgICAgICAgICB9XG4gICAgICAgICAgICBkZWxldGUgdGhpcy5jb21tYW5kc1ttZXNzYWdlLmlkXTtcbiAgICAgICAgICAgIGJyZWFrO1xuXG4gICAgICAgICAgY2FzZSAncG9uZyc6XG4gICAgICAgICAgICBicmVhaztcblxuICAgICAgICAgIGNhc2UgJ2F1dGhfcmVxdWlyZWQnOlxuICAgICAgICAgICAgdGhpcy5zZW5kTWVzc2FnZShtZXNzYWdlcy5hdXRoKHRoaXMub3B0aW9ucy5hdXRoVG9rZW4pKTtcbiAgICAgICAgICAgIGJyZWFrO1xuXG4gICAgICAgICAgY2FzZSAnYXV0aF9pbnZhbGlkJzpcbiAgICAgICAgICAgIHJlamVjdCh7IGNvZGU6IEVSUl9JTlZBTElEX0FVVEggfSk7XG4gICAgICAgICAgICBpbnZhbGlkQXV0aCA9IHRydWU7XG4gICAgICAgICAgICBicmVhaztcblxuICAgICAgICAgIGNhc2UgJ2F1dGhfb2snOlxuICAgICAgICAgICAgcmVzb2x2ZSh0aGlzKTtcbiAgICAgICAgICAgIHRoaXMuZmlyZUV2ZW50KCdyZWFkeScpO1xuXG4gICAgICAgICAgICAvLyBSZS1zdWJzY3JpYmUgdG8gZXZlbnRzIGFuZCB1cGRhdGUgb2xkIGxvY2F0aW9uIG9mIHVuc3ViIG1ldGhvZFxuICAgICAgICAgICAgLy8gc28gb2xkIHVuc3ViIG1ldGhvZCBrZWVwcyB3b3JraW5nLlxuICAgICAgICAgICAgdGhpcy5jb21tYW5kSWQgPSAxO1xuICAgICAgICAgICAgdGhpcy5jb21tYW5kcyA9IHt9O1xuXG4gICAgICAgICAgICBPYmplY3Qua2V5cyhvbGRDb21tYW5kcykuZm9yRWFjaCgoaWQpID0+IHtcbiAgICAgICAgICAgICAgY29uc3QgaW5mbyA9IG9sZENvbW1hbmRzW2lkXTtcblxuICAgICAgICAgICAgICBpZiAoaW5mby5ldmVudFR5cGUpIHtcbiAgICAgICAgICAgICAgICB0aGlzLnN1YnNjcmliZUV2ZW50cyhpbmZvLmV2ZW50Q2FsbGJhY2ssIGluZm8uZXZlbnRUeXBlKVxuICAgICAgICAgICAgICAgICAgLnRoZW4oKHVuc3ViKSA9PiB7IGluZm8udW5zdWJzY3JpYmUgPSB1bnN1YjsgfSk7XG4gICAgICAgICAgICAgIH1cbiAgICAgICAgICAgIH0pO1xuICAgICAgICAgICAgYnJlYWs7XG5cbiAgICAgICAgICBkZWZhdWx0O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VMsQ0FBQzs7SUFFekIsTUFBTSxDQUFDLFFBQVEsQ0FBQyxTQUFTLENBQUMsR0FBRyxFQUFFLFNBQUEsT0FBTyxFQUFFLFFBQUEsTUFBTSxFQUFFLENBQUM7SUFDakQsTUFBTSxDQUFDLFdBQVcsQ0FBQyxPQUFPLENBQUMsQ0FBQztHQUMzQixDQUFDLENBQUM7Q0FDSixDQUFBOztBQUdILEFBQU8sU0FBUyxnQkFBZ0IsQ0FBQyxHQUFHLEVBQUUsT0FBTyxFQUFFO0VBQzdDSCxJQUFNLElBQUksR0FBRyxJQUFJLFVBQVUsQ0FBQyxHQUFHLEVBQUUsT0FBTyxDQUFDLENBQUM7RUFDMUMsT0FBTyxJQUFJLENBQUMsT0FBTyxFQUFFLENBQUM7Q0FDdkIsQUFFRCxBQUFnQzs7Ozs7In0=
"use strict";function auth(e){return{type:"auth",api_password:e}}function states(){return{type:"get_states"}}function config(){return{type:"get_config"}}function services(){return{type:"get_services"}}function panels(){return{type:"get_panels"}}function callService$1(e,t,n){var s={type:"call_service",domain:e,service:t};return n&&(s.service_data=n),s}function subscribeEvents$1(e){var t={type:"subscribe_events"};return e&&(t.event_type=e),t}function unsubscribeEvents(e){return{type:"unsubscribe_events",subscription:e}}function ping$1(){return{type:"ping"}}function extractResult(e){return e.result}function createConnection(e,t){var n=new Connection(e,t);return n.connect()}function processEvent(e,t){var n=t.data.new_state;n?e.updateState(n):e.removeState(t.data.entity_id)}function createEntityStore(e){return new Promise(function(t,n){var s=new EntityStore,i=e.subscribeEvents(processEvent.bind(null,s),"state_changed").then(function(e){s.unsubscribe=e}),r=e.getStates().then(function(e){return s.setStates(e)});Promise.all([i,r]).then(function(){return t(s)},function(){return n()})})}Object.defineProperty(exports,"__esModule",{value:!0});var ERR_CANNOT_CONNECT=1,ERR_INVALID_AUTH=2,Connection=function(e,t){this.url=e,this.options=t||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};Connection.prototype.addEventListener=function(e,t){var n=this.eventListeners[e];n||(n=this.eventListeners[e]=[]),n.push(t)},Connection.prototype.fireEvent=function(e){var t=this;(this.eventListeners[e]||[]).forEach(function(e){return e(t)})},Connection.prototype.connect=function(){var e=this;return new Promise(function(t,n){var s=e.commands;Object.keys(s).forEach(function(e){var t=s[e];t.reject&&t.reject()});var i=!1;e.connectionTries+=1,e.socket=new WebSocket(e.url),e.socket.addEventListener("open",function(){e.connectionTries=0}),e.socket.addEventListener("message",function(r){var o=JSON.parse(r.data);switch(o.type){case"event":e.commands[o.id].eventCallback(o.event);break;case"result":o.success?e.commands[o.id].resolve(o):e.commands[o.id].reject(o.error),delete e.commands[o.id];break;case"pong":break;case"auth_required":e.sendMessage(auth(e.options.authToken));break;case"auth_invalid":n({code:ERR_INVALID_AUTH}),i=!0;break;case"auth_ok":t(e),e.fireEvent("ready"),e.commandId=1,e.commands={},Object.keys(s).forEach(function(t){var n=s[t];n.eventType&&e.subscribeEvents(n.eventCallback,n.eventType).then(function(e){n.unsubscribe=e})})}}),e.socket.addEventListener("close",function(){if(!i&&!e.closeRequested){0===e.connectionTries?e.fireEvent("disconnected"):n(ERR_CANNOT_CONNECT);var t=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},t)}})})},Connection.prototype.close=function(){this.closeRequested=!0,this.socket.close()},Connection.prototype.getStates=function(){return this.sendMessagePromise(states()).then(extractResult)},Connection.prototype.getServices=function(){return this.sendMessagePromise(services()).then(extractResult)},Connection.prototype.getPanels=function(){return this.sendMessagePromise(panels()).then(extractResult)},Connection.prototype.getConfig=function(){return this.sendMessagePromise(config()).then(extractResult)},Connection.prototype.callService=function(e,t,n){return this.sendMessagePromise(callService$1(e,t,n))},Connection.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(subscribeEvents$1(t)).then(function(s){var i={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(unsubscribeEvents(s.id)).then(function(){delete n.commands[s.id]})}};return n.commands[s.id]=i,function(){return i.unsubscribe()}})},Connection.prototype.ping=function(){return this.sendMessagePromise(ping$1())},Connection.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},Connection.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,s){t.commandId+=1;var i=t.commandId;e.id=i,t.commands[i]={resolve:n,reject:s},t.sendMessage(e)})};var EntityStore=function(){this.listeners={},this.entities={}};EntityStore.prototype.addEventListener=function(e,t){var n=this.listeners[e];n||(n=this.listeners[e]=[]),n.push(t)},EntityStore.prototype.fireEvent=function(e){var t=this;(this.listeners[e]||[]).forEach(function(e){return e(t.entities)})},EntityStore.prototype.setStates=function(e){for(var t={},n=0;n<e.length;n++){var s=e[n];t[s.entity_id]=s}this.entities=t,this.fireEvent("change")},EntityStore.prototype.updateState=function(e){var t=Object.assign({},this.entities);t[e.entity_id]=e,this.entities=t,this.fireEvent("change")},EntityStore.prototype.removeState=function(e){var t=Object.assign({},this.entities);delete t[e],this.entities=t,this.fireEvent("change")},EntityStore.prototype.unsubscribe=function(){},exports.createConnection=createConnection,exports.createEntityStore=createEntityStore,exports.ERR_CANNOT_CONNECT=ERR_CANNOT_CONNECT,exports.ERR_INVALID_AUTH=ERR_INVALID_AUTH;

@@ -1,295 +0,1 @@

function auth(authToken) {
return {
type: 'auth',
api_password: authToken,
};
}
function states() {
return {
type: 'get_states',
};
}
function config() {
return {
type: 'get_config',
};
}
function services() {
return {
type: 'get_services',
};
}
function panels() {
return {
type: 'get_panels',
};
}
function callService$1(domain, service, serviceData) {
var message = {
type: 'call_service',
domain: domain,
service: service,
};
if (serviceData) {
message.service_data = serviceData;
}
return message;
}
function subscribeEvents$1(eventType) {
var message = {
type: 'subscribe_events',
};
if (eventType) {
message.event_type = eventType;
}
return message;
}
function unsubscribeEvents(subscription) {
return {
type: 'unsubscribe_events',
subscription: subscription,
};
}
function ping$1() {
return {
type: 'ping',
};
}
var ERR_CANNOT_CONNECT = 1;
var ERR_INVALID_AUTH = 2;
function extractResult(message) {
return message.result;
}
var Connection = function Connection(url, options) {
this.url = url;
this.options = options || {};
this.commandId = 1;
this.commands = {};
this.connectionTries = 0;
this.eventListeners = {};
this.closeRequested = false;
};
Connection.prototype.addEventListener = function addEventListener (eventType, callback) {
var listeners = this.eventListeners[eventType];
if (!listeners) {
listeners = this.eventListeners[eventType] = [];
}
listeners.push(callback);
};
Connection.prototype.fireEvent = function fireEvent (eventType) {
var this$1 = this;
(this.eventListeners[eventType] || []).forEach(function (callback) { return callback(this$1); });
};
Connection.prototype.connect = function connect () {
var this$1 = this;
return new Promise(function (resolve, reject) {
// Used for resubscribing in the future
var oldCommands = this$1.commands;
Object.keys(oldCommands).forEach(function (id) {
var info = oldCommands[id];
// Reject stuff still waiting for an answer
if (info.reject) {
info.reject();
}
});
// If invalid auth, we will not try to reconnect.
var invalidAuth = false;
this$1.connectionTries += 1;
this$1.socket = new WebSocket(this$1.url);
this$1.socket.addEventListener('open', function () {
this$1.connectionTries = 0;
});
this$1.socket.addEventListener('message', function (event) {
var message = JSON.parse(event.data);
{
/* eslint-disable no-console */
console.log('Received', message);
/* eslint-enable no-console */
}
switch (message.type) {
case 'event':
this$1.commands[message.id].eventCallback(message.event);
break;
case 'result':
if (message.success) {
this$1.commands[message.id].resolve(message);
} else {
this$1.commands[message.id].reject(message.error);
}
delete this$1.commands[message.id];
break;
case 'pong':
break;
case 'auth_required':
this$1.sendMessage(auth(this$1.options.authToken));
break;
case 'auth_invalid':
reject({ code: ERR_INVALID_AUTH });
invalidAuth = true;
break;
case 'auth_ok':
resolve(this$1);
this$1.fireEvent('ready');
// Re-subscribe to events and update old location of unsub method
// so old unsub method keeps working.
this$1.commandId = 1;
this$1.commands = {};
Object.keys(oldCommands).forEach(function (id) {
var info = oldCommands[id];
if (info.eventType) {
this$1.subscribeEvents(info.eventCallback, info.eventType)
.then(function (unsub) { info.unsubscribe = unsub; });
}
});
break;
default:
{
/* eslint-disable no-console */
console.warn('Unhandled message', message);
/* eslint-enable no-console */
}
}
});
this$1.socket.addEventListener('close', function () {
if (invalidAuth || this$1.closeRequested) {
// When we have invalid auth, let's not reconnect or we get banned.
return;
} else if (this$1.connectionTries === 0) {
// We were connected at some point.
this$1.fireEvent('disconnected');
} else {
// We never were connected
reject(ERR_CANNOT_CONNECT);
}
// Try again
var waitTime = Math.min(this$1.connectionTries, 5) * 1000;
setTimeout(function () { return this$1.connect(); }, waitTime);
});
});
};
Connection.prototype.close = function close () {
this.closeRequested = true;
this.socket.close();
};
Connection.prototype.getStates = function getStates () {
return this.sendMessagePromise(states()).then(extractResult);
};
Connection.prototype.getServices = function getServices () {
return this.sendMessagePromise(services()).then(extractResult);
};
Connection.prototype.getPanels = function getPanels () {
return this.sendMessagePromise(panels()).then(extractResult);
};
Connection.prototype.getConfig = function getConfig () {
return this.sendMessagePromise(config()).then(extractResult);
};
Connection.prototype.callService = function callService (domain, service, serviceData) {
return this.sendMessagePromise(callService$1(domain, service, serviceData));
};
// eventCallback will be called when a new event fires
// Returned promise resolves to an unsubscribe function.
Connection.prototype.subscribeEvents = function subscribeEvents (eventCallback, eventType) {
var this$1 = this;
return this.sendMessagePromise(subscribeEvents$1(eventType)).then(
function (resultMessage) {
// We store unsubscribe on info object. That way we can overwrite it in case
// we get disconnected and we have to subscribe again.
var info = {
eventCallback: eventCallback,
eventType: eventType,
unsubscribe: function () { return this$1.sendMessagePromise(unsubscribeEvents(resultMessage.id))
.then(function () { delete this$1.commands[resultMessage.id]; }); },
};
this$1.commands[resultMessage.id] = info;
return function () { return info.unsubscribe(); };
/* eslint-disable comma-dangle */ /* comma crashes Buble */
}
/* eslint-enable comma-dangle */
);
};
Connection.prototype.ping = function ping () {
return this.sendMessagePromise(ping$1());
};
Connection.prototype.sendMessage = function sendMessage (message) {
{
/* eslint-disable no-console */
console.log('Sending', message);
/* eslint-enable no-console */
}
this.socket.send(JSON.stringify(message));
};
Connection.prototype.sendMessagePromise = function sendMessagePromise (message) {
var this$1 = this;
return new Promise(function (resolve, reject) {
this$1.commandId += 1;
var commandId = this$1.commandId;
/* eslint-disable no-param-reassign */
message.id = commandId;
/* eslint-enable no-param-reassign */
this$1.commands[commandId] = { resolve: resolve, reject: reject };
this$1.sendMessage(message);
});
};
function createConnection(url, options) {
var conn = new Connection(url, options);
return conn.connect();
}
export { ERR_CANNOT_CONNECT, ERR_INVALID_AUTH, createConnection };export default createConnection;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjpudWxsLCJzb3VyY2VzIjpbIi4uL2xpYi9tZXNzYWdlcy5qcyIsIi4uL2xpYi9pbmRleC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZnVuY3Rpb24gYXV0aChhdXRoVG9rZW4pIHtcbiAgcmV0dXJuIHtcbiAgICB0eXBlOiAnYXV0aCcsXG4gICAgYXBpX3Bhc3N3b3JkOiBhdXRoVG9rZW4sXG4gIH07XG59XG5cbmV4cG9ydCBmdW5jdGlvbiBzdGF0ZXMoKSB7XG4gIHJldHVybiB7XG4gICAgdHlwZTogJ2dldF9zdGF0ZXMnLFxuICB9O1xufVxuXG5leHBvcnQgZnVuY3Rpb24gY29uZmlnKCkge1xuICByZXR1cm4ge1xuICAgIHR5cGU6ICdnZXRfY29uZmlnJyxcbiAgfTtcbn1cblxuZXhwb3J0IGZ1bmN0aW9uIHNlcnZpY2VzKCkge1xuICByZXR1cm4ge1xuICAgIHR5cGU6ICdnZXRfc2VydmljZXMnLFxuICB9O1xufVxuXG5leHBvcnQgZnVuY3Rpb24gcGFuZWxzKCkge1xuICByZXR1cm4ge1xuICAgIHR5cGU6ICdnZXRfcGFuZWxzJyxcbiAgfTtcbn1cblxuZXhwb3J0IGZ1bmN0aW9uIGNhbGxTZXJ2aWNlKGRvbWFpbiwgc2VydmljZSwgc2VydmljZURhdGEpIHtcbiAgY29uc3QgbWVzc2FnZSA9IHtcbiAgICB0eXBlOiAnY2FsbF9zZXJ2aWNlJyxcbiAgICBkb21haW4sXG4gICAgc2VydmljZSxcbi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1lc3NhZ2UpO1xuICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgdGhpcy5jb21tYW5kc1ttZXNzYWdlLmlkXS5yZWplY3QobWVzc2FnZS5lcnJvcik7XG4gICAgICAgICAgICB9XG4gICAgICAgICAgICBkZWxldGUgdGhpcy5jb21tYW5kc1ttZXNzYWdlLmlkXTtcbiAgICAgICAgICAgIGJyZWFrO1xuXG4gICAgICAgICAgY2FzZSAncG9uZyc6XG4gICAgICAgICAgICBicmVhaztcblxuICAgICAgICAgIGNhc2UgJ2F1dGhfcmVxdWlyZWQnOlxuICAgICAgICAgICAgdGhpcy5zZW5kTWVzc2FnZShtZXNzYWdlcy5hdXRoKHRoaXMub3B0aW9ucy5hdXRoVG9rZW4pKTtcbiAgICAgICAgICAgIGJyZWFrO1xuXG4gICAgICAgICAgY2FzZSAnYXV0aF9pbnZhbGlkJzpcbiAgICAgICAgICAgIHJlamVjdCh7IGNvZGU6IEVSUl9JTlZBTElEX0FVVEggfSk7XG4gICAgICAgICAgICBpbnZhbGlkQXV0aCA9IHRydWU7XG4gICAgICAgICAgICBicmVhaztcblxuICAgICAgICAgIGNhc2UgJ2F1dGhfb2snOlxuICAgICAgICAgICAgcmVzb2x2ZSh0aGlzKTtcbiAgICAgICAgICAgIHRoaXMuZmlyZUV2ZW50KCdyZWFkeScpO1xuXG4gICAgICAgICAgICAvLyBSZS1zdWJzY3JpYmUgdG8gZXZlbnRzIGFuZCB1cGRhdGUgb2xkIGxvY2F0aW9uIG9mIHVuc3ViIG1ldGhvZFxuICAgICAgICAgICAgLy8gc28gb2xkIHVuc3ViIG1ldGhvZCBrZWVwcyB3b3JraW5nLlxuICAgICAgICAgICAgdGhpcy5jb21tYW5kSWQgPSAxO1xuICAgICAgICAgICAgdGhpcy5jb21tYW5kcyA9IHt9O1xuXG4gICAgICAgICAgICBPYmplY3Qua2V5cyhvbGRDb21tYW5kcykuZm9yRWFjaCgoaWQpID0+IHtcbiAgICAgICAgICAgICAgY29uc3QgaW5mbyA9IG9sZENvbW1hbmRzW2lkXTtcblxuICAgICAgICAgICAgICBpZiAoaW5mby5ldmVudFR5cGUpIHtcbiAgICAgICAgICAgICAgICB0aGlzLnN1YnNjcmliZUV2ZW50cyhpbmZvLmV2ZW50Q2FsbGJhY2ssIGluZm8uZXZlbnRUeXBlKVxuICAgICAgICAgICAgICAgICAgLnRoZW4oKHVuc3ViKSA9PiB7IGluZm8udW5zdWJzY3JpYmUgPSB1bnN1YjsgfSk7XG4gICAgICAgICAgICAgIH1cbiAgICAgICAgICAgIH0pO1xuICAgICAgICAgICAgYnJlYWs7XG5cbiAgICAgICAgICBkZWZhdWx0OlxuICAgICAgICAgICAgaWYgKF9fREVWX18pIHtcbiAgICAgICAgICAgICAgLyogZXNsaW50LWRpc2FibGUgbm8tY29uc29sZSAqL1xuICAgICAgICAgICAgICBjb25zb2xlLndhcm4oJ1VuaGFuZGxlZCBtZXNzYWdlJywgbWVzc2FnZSk7XG4gICAgICAgICAgICAgIC8qIGVzbGludC1lbmFibGUgbm8tY29uc29sZSAqL1xuICAgICAgICAgICAgfVxuICAgICAgICB9XG4gICAgICB9KTtcblxuICAgICAgdGhpcy5zb2NrZXQuYWRkRXZlbnRMaXN0ZW5lcignY2xvc2UnLCAoKSA9PiB7XG4gICAgICAgIGlmIChpbnZhbGlkQXV0aCB8fCB0aGlzLmNsb3NlUmVxdWVzdGVkKSB7XG4gICAgICAgICAgLy8gV2hlbiB3ZSBoYXZlIGludmFsaWQgYXV0aCwgbGV0J3Mgbm90IHJlY29ubmVjdCBvciB3ZSBnZXQgYmFubmVkLlxuICAgICAgICAgIHJldHVybjtcbiAgICAgICAgfSBlbHNlIGlmICh0aGlzLmNvbm5lY3Rpb25UcmllcyA9PT0gMCkge1xuICAgICAgICAgIC8vIFdlIHdlcmUgY29ubmVjdGVkIGF0IHNvbWUgcG9pbnQuXG4gICAgICAgICAgdGhpcy5maXJlRXZlbnQoJ2Rpc2Nvbm5lY3RlZCcpO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UFDLElBQUksQ0FBQyxJQUFJLENBQUMsU0FBUyxDQUFDLE9BQU8sQ0FBQyxDQUFDLENBQUM7Q0FDM0MsQ0FBQTs7QUFFSCxxQkFBRSxrQkFBa0IsZ0NBQUMsT0FBTyxFQUFFOzs7RUFDNUIsT0FBUyxJQUFJLE9BQU8sQ0FBQyxVQUFDLE9BQU8sRUFBRSxNQUFNLEVBQUU7SUFDckMsTUFBTSxDQUFDLFNBQVMsSUFBSSxDQUFDLENBQUM7SUFDdEIsSUFBUSxTQUFTLEdBQUdULE1BQUksQ0FBQyxTQUFTLENBQUM7O0lBRW5DLE9BQVMsQ0FBQyxFQUFFLEdBQUcsU0FBUyxDQUFDOztJQUV6QixNQUFNLENBQUMsUUFBUSxDQUFDLFNBQVMsQ0FBQyxHQUFHLEVBQUUsU0FBQSxPQUFPLEVBQUUsUUFBQSxNQUFNLEVBQUUsQ0FBQztJQUNqRCxNQUFNLENBQUMsV0FBVyxDQUFDLE9BQU8sQ0FBQyxDQUFDO0dBQzNCLENBQUMsQ0FBQztDQUNKLENBQUE7O0FBR0gsQUFBTyxTQUFTLGdCQUFnQixDQUFDLEdBQUcsRUFBRSxPQUFPLEVBQUU7RUFDN0NILElBQU0sSUFBSSxHQUFHLElBQUksVUFBVSxDQUFDLEdBQUcsRUFBRSxPQUFPLENBQUMsQ0FBQztFQUMxQyxPQUFPLElBQUksQ0FBQyxPQUFPLEVBQUUsQ0FBQztDQUN2QixBQUVELEFBQWdDOzsifQ==
function auth(e){return{type:"auth",api_password:e}}function states(){return{type:"get_states"}}function config(){return{type:"get_config"}}function services(){return{type:"get_services"}}function panels(){return{type:"get_panels"}}function callService$1(e,t,n){var s={type:"call_service",domain:e,service:t};return n&&(s.service_data=n),s}function subscribeEvents$1(e){var t={type:"subscribe_events"};return e&&(t.event_type=e),t}function unsubscribeEvents(e){return{type:"unsubscribe_events",subscription:e}}function ping$1(){return{type:"ping"}}function extractResult(e){return e.result}function createConnection(e,t){var n=new Connection(e,t);return n.connect()}function processEvent(e,t){var n=t.data.new_state;n?e.updateState(n):e.removeState(t.data.entity_id)}function createEntityStore(e){return new Promise(function(t,n){var s=new EntityStore,i=e.subscribeEvents(processEvent.bind(null,s),"state_changed").then(function(e){s.unsubscribe=e}),r=e.getStates().then(function(e){return s.setStates(e)});Promise.all([i,r]).then(function(){return t(s)},function(){return n()})})}var ERR_CANNOT_CONNECT=1,ERR_INVALID_AUTH=2,Connection=function(e,t){this.url=e,this.options=t||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};Connection.prototype.addEventListener=function(e,t){var n=this.eventListeners[e];n||(n=this.eventListeners[e]=[]),n.push(t)},Connection.prototype.fireEvent=function(e){var t=this;(this.eventListeners[e]||[]).forEach(function(e){return e(t)})},Connection.prototype.connect=function(){var e=this;return new Promise(function(t,n){var s=e.commands;Object.keys(s).forEach(function(e){var t=s[e];t.reject&&t.reject()});var i=!1;e.connectionTries+=1,e.socket=new WebSocket(e.url),e.socket.addEventListener("open",function(){e.connectionTries=0}),e.socket.addEventListener("message",function(r){var o=JSON.parse(r.data);switch(o.type){case"event":e.commands[o.id].eventCallback(o.event);break;case"result":o.success?e.commands[o.id].resolve(o):e.commands[o.id].reject(o.error),delete e.commands[o.id];break;case"pong":break;case"auth_required":e.sendMessage(auth(e.options.authToken));break;case"auth_invalid":n({code:ERR_INVALID_AUTH}),i=!0;break;case"auth_ok":t(e),e.fireEvent("ready"),e.commandId=1,e.commands={},Object.keys(s).forEach(function(t){var n=s[t];n.eventType&&e.subscribeEvents(n.eventCallback,n.eventType).then(function(e){n.unsubscribe=e})})}}),e.socket.addEventListener("close",function(){if(!i&&!e.closeRequested){0===e.connectionTries?e.fireEvent("disconnected"):n(ERR_CANNOT_CONNECT);var t=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},t)}})})},Connection.prototype.close=function(){this.closeRequested=!0,this.socket.close()},Connection.prototype.getStates=function(){return this.sendMessagePromise(states()).then(extractResult)},Connection.prototype.getServices=function(){return this.sendMessagePromise(services()).then(extractResult)},Connection.prototype.getPanels=function(){return this.sendMessagePromise(panels()).then(extractResult)},Connection.prototype.getConfig=function(){return this.sendMessagePromise(config()).then(extractResult)},Connection.prototype.callService=function(e,t,n){return this.sendMessagePromise(callService$1(e,t,n))},Connection.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(subscribeEvents$1(t)).then(function(s){var i={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(unsubscribeEvents(s.id)).then(function(){delete n.commands[s.id]})}};return n.commands[s.id]=i,function(){return i.unsubscribe()}})},Connection.prototype.ping=function(){return this.sendMessagePromise(ping$1())},Connection.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},Connection.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,s){t.commandId+=1;var i=t.commandId;e.id=i,t.commands[i]={resolve:n,reject:s},t.sendMessage(e)})};var EntityStore=function(){this.listeners={},this.entities={}};EntityStore.prototype.addEventListener=function(e,t){var n=this.listeners[e];n||(n=this.listeners[e]=[]),n.push(t)},EntityStore.prototype.fireEvent=function(e){var t=this;(this.listeners[e]||[]).forEach(function(e){return e(t.entities)})},EntityStore.prototype.setStates=function(e){for(var t={},n=0;n<e.length;n++){var s=e[n];t[s.entity_id]=s}this.entities=t,this.fireEvent("change")},EntityStore.prototype.updateState=function(e){var t=Object.assign({},this.entities);t[e.entity_id]=e,this.entities=t,this.fireEvent("change")},EntityStore.prototype.removeState=function(e){var t=Object.assign({},this.entities);delete t[e],this.entities=t,this.fireEvent("change")},EntityStore.prototype.unsubscribe=function(){};export{createConnection:createConnection,createEntityStore:createEntityStore,ERR_CANNOT_CONNECT:ERR_CANNOT_CONNECT,ERR_INVALID_AUTH:ERR_INVALID_AUTH};

@@ -1,308 +0,1 @@

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.HAWS = global.HAWS || {})));
}(this, (function (exports) { 'use strict';
function auth(authToken) {
return {
type: 'auth',
api_password: authToken,
};
}
function states() {
return {
type: 'get_states',
};
}
function config() {
return {
type: 'get_config',
};
}
function services() {
return {
type: 'get_services',
};
}
function panels() {
return {
type: 'get_panels',
};
}
function callService$1(domain, service, serviceData) {
var message = {
type: 'call_service',
domain: domain,
service: service,
};
if (serviceData) {
message.service_data = serviceData;
}
return message;
}
function subscribeEvents$1(eventType) {
var message = {
type: 'subscribe_events',
};
if (eventType) {
message.event_type = eventType;
}
return message;
}
function unsubscribeEvents(subscription) {
return {
type: 'unsubscribe_events',
subscription: subscription,
};
}
function ping$1() {
return {
type: 'ping',
};
}
var ERR_CANNOT_CONNECT = 1;
var ERR_INVALID_AUTH = 2;
function extractResult(message) {
return message.result;
}
var Connection = function Connection(url, options) {
this.url = url;
this.options = options || {};
this.commandId = 1;
this.commands = {};
this.connectionTries = 0;
this.eventListeners = {};
this.closeRequested = false;
};
Connection.prototype.addEventListener = function addEventListener (eventType, callback) {
var listeners = this.eventListeners[eventType];
if (!listeners) {
listeners = this.eventListeners[eventType] = [];
}
listeners.push(callback);
};
Connection.prototype.fireEvent = function fireEvent (eventType) {
var this$1 = this;
(this.eventListeners[eventType] || []).forEach(function (callback) { return callback(this$1); });
};
Connection.prototype.connect = function connect () {
var this$1 = this;
return new Promise(function (resolve, reject) {
// Used for resubscribing in the future
var oldCommands = this$1.commands;
Object.keys(oldCommands).forEach(function (id) {
var info = oldCommands[id];
// Reject stuff still waiting for an answer
if (info.reject) {
info.reject();
}
});
// If invalid auth, we will not try to reconnect.
var invalidAuth = false;
this$1.connectionTries += 1;
this$1.socket = new WebSocket(this$1.url);
this$1.socket.addEventListener('open', function () {
this$1.connectionTries = 0;
});
this$1.socket.addEventListener('message', function (event) {
var message = JSON.parse(event.data);
{
/* eslint-disable no-console */
console.log('Received', message);
/* eslint-enable no-console */
}
switch (message.type) {
case 'event':
this$1.commands[message.id].eventCallback(message.event);
break;
case 'result':
if (message.success) {
this$1.commands[message.id].resolve(message);
} else {
this$1.commands[message.id].reject(message.error);
}
delete this$1.commands[message.id];
break;
case 'pong':
break;
case 'auth_required':
this$1.sendMessage(auth(this$1.options.authToken));
break;
case 'auth_invalid':
reject({ code: ERR_INVALID_AUTH });
invalidAuth = true;
break;
case 'auth_ok':
resolve(this$1);
this$1.fireEvent('ready');
// Re-subscribe to events and update old location of unsub method
// so old unsub method keeps working.
this$1.commandId = 1;
this$1.commands = {};
Object.keys(oldCommands).forEach(function (id) {
var info = oldCommands[id];
if (info.eventType) {
this$1.subscribeEvents(info.eventCallback, info.eventType)
.then(function (unsub) { info.unsubscribe = unsub; });
}
});
break;
default:
{
/* eslint-disable no-console */
console.warn('Unhandled message', message);
/* eslint-enable no-console */
}
}
});
this$1.socket.addEventListener('close', function () {
if (invalidAuth || this$1.closeRequested) {
// When we have invalid auth, let's not reconnect or we get banned.
return;
} else if (this$1.connectionTries === 0) {
// We were connected at some point.
this$1.fireEvent('disconnected');
} else {
// We never were connected
reject(ERR_CANNOT_CONNECT);
}
// Try again
var waitTime = Math.min(this$1.connectionTries, 5) * 1000;
setTimeout(function () { return this$1.connect(); }, waitTime);
});
});
};
Connection.prototype.close = function close () {
this.closeRequested = true;
this.socket.close();
};
Connection.prototype.getStates = function getStates () {
return this.sendMessagePromise(states()).then(extractResult);
};
Connection.prototype.getServices = function getServices () {
return this.sendMessagePromise(services()).then(extractResult);
};
Connection.prototype.getPanels = function getPanels () {
return this.sendMessagePromise(panels()).then(extractResult);
};
Connection.prototype.getConfig = function getConfig () {
return this.sendMessagePromise(config()).then(extractResult);
};
Connection.prototype.callService = function callService (domain, service, serviceData) {
return this.sendMessagePromise(callService$1(domain, service, serviceData));
};
// eventCallback will be called when a new event fires
// Returned promise resolves to an unsubscribe function.
Connection.prototype.subscribeEvents = function subscribeEvents (eventCallback, eventType) {
var this$1 = this;
return this.sendMessagePromise(subscribeEvents$1(eventType)).then(
function (resultMessage) {
// We store unsubscribe on info object. That way we can overwrite it in case
// we get disconnected and we have to subscribe again.
var info = {
eventCallback: eventCallback,
eventType: eventType,
unsubscribe: function () { return this$1.sendMessagePromise(unsubscribeEvents(resultMessage.id))
.then(function () { delete this$1.commands[resultMessage.id]; }); },
};
this$1.commands[resultMessage.id] = info;
return function () { return info.unsubscribe(); };
/* eslint-disable comma-dangle */ /* comma crashes Buble */
}
/* eslint-enable comma-dangle */
);
};
Connection.prototype.ping = function ping () {
return this.sendMessagePromise(ping$1());
};
Connection.prototype.sendMessage = function sendMessage (message) {
{
/* eslint-disable no-console */
console.log('Sending', message);
/* eslint-enable no-console */
}
this.socket.send(JSON.stringify(message));
};
Connection.prototype.sendMessagePromise = function sendMessagePromise (message) {
var this$1 = this;
return new Promise(function (resolve, reject) {
this$1.commandId += 1;
var commandId = this$1.commandId;
/* eslint-disable no-param-reassign */
message.id = commandId;
/* eslint-enable no-param-reassign */
this$1.commands[commandId] = { resolve: resolve, reject: reject };
this$1.sendMessage(message);
});
};
function createConnection(url, options) {
var conn = new Connection(url, options);
return conn.connect();
}
exports.ERR_CANNOT_CONNECT = ERR_CANNOT_CONNECT;
exports.ERR_INVALID_AUTH = ERR_INVALID_AUTH;
exports.createConnection = createConnection;
exports['default'] = createConnection;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjpudWxsLCJzb3VyY2VzIjpbIi4uL2xpYi9tZXNzYWdlcy5qcyIsIi4uL2xpYi9pbmRleC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgZnVuY3Rpb24gYXV0aChhdXRoVG9rZW4pIHtcbiAgcmV0dXJuIHtcbiAgICB0eXBlOiAnYXV0aCcsXG4gICAgYXBpX3Bhc3N3b3JkOiBhdXRoVG9rZW4sXG4gIH07XG59XG5cbmV4cG9ydCBmdW5jdGlvbiBzdGF0ZXMoKSB7XG4gIHJldHVybiB7XG4gICAgdHlwZTogJ2dldF9zd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ICAgICAgICAvKiBlc2xpbnQtZGlzYWJsZSBuby1jb25zb2xlICovXG4gICAgICAgICAgY29uc29sZS5sb2coJ1JlY2VpdmVkJywgbWVzc2FnZSk7XG4gICAgICAgICAgLyogZXNsaW50LWVuYWJsZSBuby1jb25zb2xlICovXG4gICAgICAgIH1cblxuICAgICAgICBzd2l0Y2ggKG1lc3NhZ2UudHlwZSkge1xuICAgICAgICAgIGNhc2UgJ2V2ZW50JzpcbiAgICAgICAgICAgIHRoaXMuY29tbWFuZHNbbWVzc2FnZS5pZF0uZXZlbnRDYWxsYmFjayhtZXNzYWdlLmV2ZW50KTtcbiAgICAgICAgICAgIGJyZWFrO1xuXG4gICAgICAgICAgY2FzZSAncmVzdWx0JzpcbiAgICAgICAgICAgIGlmIChtZXNzYWdlLnN1Y2Nlc3MpIHtcbiAgICAgICAgICAgICAgdGhpcy5jb21tYW5kc1ttZXNzYWdlLmlkXS5yZXNvbHZlKG1lc3NhZ2UpO1xuICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgdGhpcy5jb21tYW5kc1ttZXNzYWdlLmlkXS5yZWplY3QobWVzc2FnZS5lcnJvcik7XG4gICAgICAgICAgICB9XG4gICAgICAgICAgICBkZWxldGUgdGhpcy5jb21tYW5kc1ttZXNzYWdlLmlkXTtcbiAgICAgICAgICAgIGJyZWFrO1xuXG4gICAgICAgICAgY2FzZSAncG9uZyc6XG4gICAgICAgICAgICBicmVhaztcblxuICAgICAgICAgIGNhc2UgJ2F1dGhfcmVxdWlyZWQnOlxuICAgICAgICAgICAgdGhpcy5zZW5kTWVzc2FnZShtZXNzYWdlcy5hdXRoKHRoaXMub3B0aW9ucy5hdXRoVG9rZW4pKTtcbiAgICAgICAgICAgIGJyZWFrO1xuXG4gICAgICAgICAgY2FzZSAnYXV0aF9pbnZhbGlkJzpcbiAgICAgICAgICAgIHJlamVjdCh7IGNvZGU6IEVSUl9JTlZBTElEX0FVVEggfSk7XG4gICAgICAgICAgICBpbnZhbGlkQXV0aCA9IHRydWU7XG4gICAgICAgICAgICBicmVhaztcblxuICAgICAgICAgIGNhc2UgJ2F1dGhfb2snOlxuICAgICAgICAgICAgcmVzb2x2ZSh0aGlzKTtcbiAgICAgICAgICAgIHRoaXMuZmlyZUV2ZW50KCdyZWFkeScpO1xuXG4gICAgICAgICAgICAvLyBSZS1zdWJzY3JpYmUgdG8gZXZlbnRzIGFuZCB1cGRhdGUgb2xkIGxvY2F0aW9uIG9mIHVuc3ViIG1ldGhvZFxuICAgICAgICAgICAgLy8gc28gb2xkIHVuc3ViIG1ldGhvZCBrZWVwcyB3b3JraW5nLlxuICAgICAgICAgICAgdGhpcy5jb21tYW5kSWQgPSAxO1xuICAgICAgICAgICAgdGhpcy5jb21tYW5kcyA9IHt9O1xuXG4gICAgICAgICAgICBPYmplY3Qua2V5cyhvbGRDb21tYW5kcykuZm9yRWFjaCgoaWQpID0+IHtcbiAgICAgICAgICAgICAgY29uc3QgaW5mbyA9IG9sZENvbW1hbmRzW2lkXTtcblxuICAgICAgICAgICAgICBpZiAoaW5mby5ldmVudFR5cGUpIHtcbiAgICAgICAgICAgICAgICB0aGlzLnN1YnNjcmliZUV2ZW50cyhpbmZvLmV2ZW50Q2FsbGJhY2ssIGluZm8uZXZlbnRUeXBlKVxuICAgICAgICAgICAgICAgICAgLnRoZW4oKHVuc3ViKSA9PiB7IGluZm8udW5zdWJzY3JpYmUgPSB1bnN1YjsgfSk7XG4gICAgICAgICAgICAgIH1cbiAgICAgICAgICAgIH0pO1xuICAgICAgICAgICAgYnJlYWs7XG5cbiAgICAgICAgICBkZWZhdWx0OlxuICAgICAgICAgICAgaWYgKF9fREVWX18pIHtcbiAgICAgICAgICAgICAgLyogZXNsaW50LWRpc2FibGUgbm8tY29uc29sZSAqL1xuICAgICAgICAgICAgICBjb25zb2xlLndhcm4oJ1VuaGFuZGxlZCBtZXNzYWdlJywgbWVzc2FnZSk7XG4gICAgICAgICAgICAgIC8qIGVzbGludC1lbmFibGUgb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Q0FBQztDQUNKLENBQUE7O0FBR0gsQUFBTyxTQUFTLGdCQUFnQixDQUFDLEdBQUcsRUFBRSxPQUFPLEVBQUU7RUFDN0NILElBQU0sSUFBSSxHQUFHLElBQUksVUFBVSxDQUFDLEdBQUcsRUFBRSxPQUFPLENBQUMsQ0FBQztFQUMxQyxPQUFPLElBQUksQ0FBQyxPQUFPLEVBQUUsQ0FBQztDQUN2QixBQUVELEFBQWdDOzs7Ozs7Ozs7In0=
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.HAWS=e.HAWS||{})}(this,function(e){"use strict";function t(e){return{type:"auth",api_password:e}}function n(){return{type:"get_states"}}function s(){return{type:"get_config"}}function i(){return{type:"get_services"}}function r(){return{type:"get_panels"}}function o(e,t,n){var s={type:"call_service",domain:e,service:t};return n&&(s.service_data=n),s}function c(e){var t={type:"subscribe_events"};return e&&(t.event_type=e),t}function u(e){return{type:"unsubscribe_events",subscription:e}}function a(){return{type:"ping"}}function d(e){return e.result}function f(e,t){var n=new y(e,t);return n.connect()}function p(e,t){var n=t.data.new_state;n?e.updateState(n):e.removeState(t.data.entity_id)}function v(e){return new Promise(function(t,n){var s=new l,i=e.subscribeEvents(p.bind(null,s),"state_changed").then(function(e){s.unsubscribe=e}),r=e.getStates().then(function(e){return s.setStates(e)});Promise.all([i,r]).then(function(){return t(s)},function(){return n()})})}var h=1,m=2,y=function(e,t){this.url=e,this.options=t||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};y.prototype.addEventListener=function(e,t){var n=this.eventListeners[e];n||(n=this.eventListeners[e]=[]),n.push(t)},y.prototype.fireEvent=function(e){var t=this;(this.eventListeners[e]||[]).forEach(function(e){return e(t)})},y.prototype.connect=function(){var e=this;return new Promise(function(n,s){var i=e.commands;Object.keys(i).forEach(function(e){var t=i[e];t.reject&&t.reject()});var r=!1;e.connectionTries+=1,e.socket=new WebSocket(e.url),e.socket.addEventListener("open",function(){e.connectionTries=0}),e.socket.addEventListener("message",function(o){var c=JSON.parse(o.data);switch(c.type){case"event":e.commands[c.id].eventCallback(c.event);break;case"result":c.success?e.commands[c.id].resolve(c):e.commands[c.id].reject(c.error),delete e.commands[c.id];break;case"pong":break;case"auth_required":e.sendMessage(t(e.options.authToken));break;case"auth_invalid":s({code:m}),r=!0;break;case"auth_ok":n(e),e.fireEvent("ready"),e.commandId=1,e.commands={},Object.keys(i).forEach(function(t){var n=i[t];n.eventType&&e.subscribeEvents(n.eventCallback,n.eventType).then(function(e){n.unsubscribe=e})})}}),e.socket.addEventListener("close",function(){if(!r&&!e.closeRequested){0===e.connectionTries?e.fireEvent("disconnected"):s(h);var t=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},t)}})})},y.prototype.close=function(){this.closeRequested=!0,this.socket.close()},y.prototype.getStates=function(){return this.sendMessagePromise(n()).then(d)},y.prototype.getServices=function(){return this.sendMessagePromise(i()).then(d)},y.prototype.getPanels=function(){return this.sendMessagePromise(r()).then(d)},y.prototype.getConfig=function(){return this.sendMessagePromise(s()).then(d)},y.prototype.callService=function(e,t,n){return this.sendMessagePromise(o(e,t,n))},y.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(c(t)).then(function(s){var i={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(u(s.id)).then(function(){delete n.commands[s.id]})}};return n.commands[s.id]=i,function(){return i.unsubscribe()}})},y.prototype.ping=function(){return this.sendMessagePromise(a())},y.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},y.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,s){t.commandId+=1;var i=t.commandId;e.id=i,t.commands[i]={resolve:n,reject:s},t.sendMessage(e)})};var l=function(){this.listeners={},this.entities={}};l.prototype.addEventListener=function(e,t){var n=this.listeners[e];n||(n=this.listeners[e]=[]),n.push(t)},l.prototype.fireEvent=function(e){var t=this;(this.listeners[e]||[]).forEach(function(e){return e(t.entities)})},l.prototype.setStates=function(e){for(var t={},n=0;n<e.length;n++){var s=e[n];t[s.entity_id]=s}this.entities=t,this.fireEvent("change")},l.prototype.updateState=function(e){var t=Object.assign({},this.entities);t[e.entity_id]=e,this.entities=t,this.fireEvent("change")},l.prototype.removeState=function(e){var t=Object.assign({},this.entities);delete t[e],this.entities=t,this.fireEvent("change")},l.prototype.unsubscribe=function(){},e.createConnection=f,e.createEntityStore=v,e.ERR_CANNOT_CONNECT=h,e.ERR_INVALID_AUTH=m,Object.defineProperty(e,"__esModule",{value:!0})});

2

package.json
{
"name": "home-assistant-js-websocket",
"version": "0.1.4",
"version": "0.2.0",
"description": "Home Assistant websocket client",

@@ -5,0 +5,0 @@ "main": "dist/haws.cjs.js",

@@ -6,3 +6,3 @@ # :aerial_tramway: JavaScript websocket client for Home Assistant

```javascript
import createConnection from 'home-assistant-js-websocket';
import { createConnection } from 'home-assistant-js-websocket';

@@ -24,2 +24,4 @@ function stateChanged(event) {

### Initializing connection
Connections to the websocket API are initiated by calling `createConnection(url[, options])`. `createConnection` will return a promise that will resolve to either a `Connection` object or rejects with an error code.

@@ -50,3 +52,3 @@

## Automatic reconnecting
#### Automatic reconnecting

@@ -70,2 +72,19 @@ The connection object will automatically try to reconnect to the server when the connection gets lost. On reconnect, it will automatically resubscribe the event listeners.

### Entities
An Entity Store is available that at all times will represent the latest information about the available entities. It is possible to listen for `change` events on the store to be notified when it gets updated.
```javascript
import { createEntityStore } from 'home-assistant-js-websocket';
// conn is connection from earlier.
createEntityStore(conn).then((entityStore) => {
console.log('Current entities', entityStore.entities);
entityStore.addEventListener('change', entities => console.log('Store updated!', entities));
});
```
## Connection API Reference

@@ -98,1 +117,5 @@

Returns a promise that will resolve to a function that will cancel the subscription once called.
#### `conn.addEventListener(eventType, listener)
Listen for events on the connection. [See docs.](#automatic-reconnecting)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc