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.3.0 to 0.4.0

2

dist/haws.cjs.js

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

"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 getEntities(e){for(var t={},n=0;n<e.length;n++){var s=e[n];t[s.entity_id]=s}return t}function updateState(e,t){var n=Object.assign({},e);return n[t.entity_id]=t,n}function removeState(e,t){var n=Object.assign({},e);return delete n[t],n}function subscribeEntities(e,t){return new Promise(function(n,s){function i(e){var n=e.data,s=n.entity_id,i=n.new_state;r=i?updateState(r,i):removeState(r,s),t(r)}var r=null,o=e.subscribeEvents(i,"state_changed"),c=e.getStates().then(function(e){r=getEntities(e),t(r)});Promise.all([o,c]).then(function(e){var t=e[0];return n(t)},function(){return s()})})}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)})},exports.createConnection=createConnection,exports.subscribeEntities=subscribeEntities,exports.ERR_CANNOT_CONNECT=ERR_CANNOT_CONNECT,exports.ERR_INVALID_AUTH=ERR_INVALID_AUTH;
"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,n,t){var s={type:"call_service",domain:e,service:n};return t&&(s.service_data=t),s}function subscribeEvents$1(e){var n={type:"subscribe_events"};return e&&(n.event_type=e),n}function unsubscribeEvents(e){return{type:"unsubscribe_events",subscription:e}}function ping$1(){return{type:"ping"}}function extractResult(e){return e.result}function createConnection(e,n){var t=new Connection(e,n);return t.connect()}function subscribeConfig(e,n){return new Promise(function(t,s){function i(e){var t=Object.assign({},o.core,{components:o.core.components.concat(e.data.component)});o=Object.assign({},o,{core:t}),n(o)}function r(e){var t,s,i=e.data,r=i.domain,c=i.service,a=Object.assign({},o.services[r]||{},(t={},t[c]={description:"",fields:{}},t));o=Object.assign({},o,{services:Object.assign({},o.services,(s={},s[r]=a,s))}),n(o)}var o=null;Promise.all([e.getConfig(),e.getPanels(),e.getServices(),e.subscribeEvents(i,"component_loaded"),e.subscribeEvents(r,"service_registered")]).then(function(e){var s=e[0],i=e[1],r=e[2],c=e[3],a=e[4];o={core:s,panels:i,services:r},n(o),t(function(){c(),a()})},function(){return s()})})}function getEntities(e){for(var n={},t=0;t<e.length;t++){var s=e[t];n[s.entity_id]=s}return n}function updateState(e,n){var t=Object.assign({},e);return t[n.entity_id]=n,t}function removeState(e,n){var t=Object.assign({},e);return delete t[n],t}function subscribeEntities(e,n){return new Promise(function(t,s){function i(e){var t=e.data,s=t.entity_id,i=t.new_state;r=i?updateState(r,i):removeState(r,s),n(r)}var r=null,o=e.subscribeEvents(i,"state_changed"),c=e.getStates().then(function(e){r=getEntities(e),n(r)});Promise.all([o,c]).then(function(e){var n=e[0];return t(n)},function(){return s()})})}Object.defineProperty(exports,"__esModule",{value:!0});var ERR_CANNOT_CONNECT=1,ERR_INVALID_AUTH=2,Connection=function(e,n){this.url=e,this.options=n||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};Connection.prototype.addEventListener=function(e,n){var t=this.eventListeners[e];t||(t=this.eventListeners[e]=[]),t.push(n)},Connection.prototype.fireEvent=function(e){var n=this;(this.eventListeners[e]||[]).forEach(function(e){return e(n)})},Connection.prototype.connect=function(){var e=this;return new Promise(function(n,t){var s=e.commands;Object.keys(s).forEach(function(e){var n=s[e];n.reject&&n.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":t({code:ERR_INVALID_AUTH}),i=!0;break;case"auth_ok":n(e),e.fireEvent("ready"),e.commandId=1,e.commands={},Object.keys(s).forEach(function(n){var t=s[n];t.eventType&&e.subscribeEvents(t.eventCallback,t.eventType).then(function(e){t.unsubscribe=e})})}}),e.socket.addEventListener("close",function(){if(!i&&!e.closeRequested){0===e.connectionTries?e.fireEvent("disconnected"):t(ERR_CANNOT_CONNECT);var n=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},n)}})})},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,n,t){return this.sendMessagePromise(callService$1(e,n,t))},Connection.prototype.subscribeEvents=function(e,n){var t=this;return this.sendMessagePromise(subscribeEvents$1(n)).then(function(s){var i={eventCallback:e,eventType:n,unsubscribe:function(){return t.sendMessagePromise(unsubscribeEvents(s.id)).then(function(){delete t.commands[s.id]})}};return t.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 n=this;return new Promise(function(t,s){n.commandId+=1;var i=n.commandId;e.id=i,n.commands[i]={resolve:t,reject:s},n.sendMessage(e)})},exports.ERR_CANNOT_CONNECT=ERR_CANNOT_CONNECT,exports.ERR_INVALID_AUTH=ERR_INVALID_AUTH,exports.createConnection=createConnection,exports.subscribeConfig=subscribeConfig,exports.subscribeEntities=subscribeEntities;

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

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,n,t){var s={type:"call_service",domain:e,service:n};return t&&(s.service_data=t),s}function subscribeEvents$1(e){var n={type:"subscribe_events"};return e&&(n.event_type=e),n}function unsubscribeEvents(e){return{type:"unsubscribe_events",subscription:e}}function ping$1(){return{type:"ping"}}function extractResult(e){return e.result}function createConnection(e,n){var t=new Connection(e,n);return t.connect()}function getEntities(e){for(var n={},t=0;t<e.length;t++){var s=e[t];n[s.entity_id]=s}return n}function updateState(e,n){var t=Object.assign({},e);return t[n.entity_id]=n,t}function removeState(e,n){var t=Object.assign({},e);return delete t[n],t}function subscribeEntities(e,n){return new Promise(function(t,s){function i(e){var t=e.data,s=t.entity_id,i=t.new_state;r=i?updateState(r,i):removeState(r,s),n(r)}var r=null,o=e.subscribeEvents(i,"state_changed"),c=e.getStates().then(function(e){r=getEntities(e),n(r)});Promise.all([o,c]).then(function(e){var n=e[0];return t(n)},function(){return s()})})}var ERR_CANNOT_CONNECT=1,ERR_INVALID_AUTH=2,Connection=function(e,n){this.url=e,this.options=n||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};Connection.prototype.addEventListener=function(e,n){var t=this.eventListeners[e];t||(t=this.eventListeners[e]=[]),t.push(n)},Connection.prototype.fireEvent=function(e){var n=this;(this.eventListeners[e]||[]).forEach(function(e){return e(n)})},Connection.prototype.connect=function(){var e=this;return new Promise(function(n,t){var s=e.commands;Object.keys(s).forEach(function(e){var n=s[e];n.reject&&n.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":t({code:ERR_INVALID_AUTH}),i=!0;break;case"auth_ok":n(e),e.fireEvent("ready"),e.commandId=1,e.commands={},Object.keys(s).forEach(function(n){var t=s[n];t.eventType&&e.subscribeEvents(t.eventCallback,t.eventType).then(function(e){t.unsubscribe=e})})}}),e.socket.addEventListener("close",function(){if(!i&&!e.closeRequested){0===e.connectionTries?e.fireEvent("disconnected"):t(ERR_CANNOT_CONNECT);var n=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},n)}})})},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,n,t){return this.sendMessagePromise(callService$1(e,n,t))},Connection.prototype.subscribeEvents=function(e,n){var t=this;return this.sendMessagePromise(subscribeEvents$1(n)).then(function(s){var i={eventCallback:e,eventType:n,unsubscribe:function(){return t.sendMessagePromise(unsubscribeEvents(s.id)).then(function(){delete t.commands[s.id]})}};return t.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 n=this;return new Promise(function(t,s){n.commandId+=1;var i=n.commandId;e.id=i,n.commands[i]={resolve:t,reject:s},n.sendMessage(e)})};export{createConnection:createConnection,subscribeEntities:subscribeEntities,ERR_CANNOT_CONNECT:ERR_CANNOT_CONNECT,ERR_INVALID_AUTH:ERR_INVALID_AUTH};
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,n,t){var s={type:"call_service",domain:e,service:n};return t&&(s.service_data=t),s}function subscribeEvents$1(e){var n={type:"subscribe_events"};return e&&(n.event_type=e),n}function unsubscribeEvents(e){return{type:"unsubscribe_events",subscription:e}}function ping$1(){return{type:"ping"}}function extractResult(e){return e.result}function createConnection(e,n){var t=new Connection(e,n);return t.connect()}function subscribeConfig(e,n){return new Promise(function(t,s){function i(e){var t=Object.assign({},o.core,{components:o.core.components.concat(e.data.component)});o=Object.assign({},o,{core:t}),n(o)}function r(e){var t,s,i=e.data,r=i.domain,c=i.service,a=Object.assign({},o.services[r]||{},(t={},t[c]={description:"",fields:{}},t));o=Object.assign({},o,{services:Object.assign({},o.services,(s={},s[r]=a,s))}),n(o)}var o=null;Promise.all([e.getConfig(),e.getPanels(),e.getServices(),e.subscribeEvents(i,"component_loaded"),e.subscribeEvents(r,"service_registered")]).then(function(e){var s=e[0],i=e[1],r=e[2],c=e[3],a=e[4];o={core:s,panels:i,services:r},n(o),t(function(){c(),a()})},function(){return s()})})}function getEntities(e){for(var n={},t=0;t<e.length;t++){var s=e[t];n[s.entity_id]=s}return n}function updateState(e,n){var t=Object.assign({},e);return t[n.entity_id]=n,t}function removeState(e,n){var t=Object.assign({},e);return delete t[n],t}function subscribeEntities(e,n){return new Promise(function(t,s){function i(e){var t=e.data,s=t.entity_id,i=t.new_state;r=i?updateState(r,i):removeState(r,s),n(r)}var r=null,o=e.subscribeEvents(i,"state_changed"),c=e.getStates().then(function(e){r=getEntities(e),n(r)});Promise.all([o,c]).then(function(e){var n=e[0];return t(n)},function(){return s()})})}var ERR_CANNOT_CONNECT=1,ERR_INVALID_AUTH=2,Connection=function(e,n){this.url=e,this.options=n||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};Connection.prototype.addEventListener=function(e,n){var t=this.eventListeners[e];t||(t=this.eventListeners[e]=[]),t.push(n)},Connection.prototype.fireEvent=function(e){var n=this;(this.eventListeners[e]||[]).forEach(function(e){return e(n)})},Connection.prototype.connect=function(){var e=this;return new Promise(function(n,t){var s=e.commands;Object.keys(s).forEach(function(e){var n=s[e];n.reject&&n.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":t({code:ERR_INVALID_AUTH}),i=!0;break;case"auth_ok":n(e),e.fireEvent("ready"),e.commandId=1,e.commands={},Object.keys(s).forEach(function(n){var t=s[n];t.eventType&&e.subscribeEvents(t.eventCallback,t.eventType).then(function(e){t.unsubscribe=e})})}}),e.socket.addEventListener("close",function(){if(!i&&!e.closeRequested){0===e.connectionTries?e.fireEvent("disconnected"):t(ERR_CANNOT_CONNECT);var n=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},n)}})})},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,n,t){return this.sendMessagePromise(callService$1(e,n,t))},Connection.prototype.subscribeEvents=function(e,n){var t=this;return this.sendMessagePromise(subscribeEvents$1(n)).then(function(s){var i={eventCallback:e,eventType:n,unsubscribe:function(){return t.sendMessagePromise(unsubscribeEvents(s.id)).then(function(){delete t.commands[s.id]})}};return t.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 n=this;return new Promise(function(t,s){n.commandId+=1;var i=n.commandId;e.id=i,n.commands[i]={resolve:t,reject:s},n.sendMessage(e)})};export{ERR_CANNOT_CONNECT:ERR_CANNOT_CONNECT,ERR_INVALID_AUTH:ERR_INVALID_AUTH,createConnection:createConnection,subscribeConfig:subscribeConfig,subscribeEntities:subscribeEntities};

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

!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 r(){return{type:"get_services"}}function i(){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 b(e,t);return n.connect()}function p(e){for(var t={},n=0;n<e.length;n++){var s=e[n];t[s.entity_id]=s}return t}function v(e,t){var n=Object.assign({},e);return n[t.entity_id]=t,n}function h(e,t){var n=Object.assign({},e);return delete n[t],n}function m(e,t){return new Promise(function(n,s){function r(e){var n=e.data,s=n.entity_id,r=n.new_state;i=r?v(i,r):h(i,s),t(i)}var i=null,o=e.subscribeEvents(r,"state_changed"),c=e.getStates().then(function(e){i=p(e),t(i)});Promise.all([o,c]).then(function(e){var t=e[0];return n(t)},function(){return s()})})}var y=1,l=2,b=function(e,t){this.url=e,this.options=t||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};b.prototype.addEventListener=function(e,t){var n=this.eventListeners[e];n||(n=this.eventListeners[e]=[]),n.push(t)},b.prototype.fireEvent=function(e){var t=this;(this.eventListeners[e]||[]).forEach(function(e){return e(t)})},b.prototype.connect=function(){var e=this;return new Promise(function(n,s){var r=e.commands;Object.keys(r).forEach(function(e){var t=r[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(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:l}),i=!0;break;case"auth_ok":n(e),e.fireEvent("ready"),e.commandId=1,e.commands={},Object.keys(r).forEach(function(t){var n=r[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"):s(y);var t=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},t)}})})},b.prototype.close=function(){this.closeRequested=!0,this.socket.close()},b.prototype.getStates=function(){return this.sendMessagePromise(n()).then(d)},b.prototype.getServices=function(){return this.sendMessagePromise(r()).then(d)},b.prototype.getPanels=function(){return this.sendMessagePromise(i()).then(d)},b.prototype.getConfig=function(){return this.sendMessagePromise(s()).then(d)},b.prototype.callService=function(e,t,n){return this.sendMessagePromise(o(e,t,n))},b.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(c(t)).then(function(s){var r={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(u(s.id)).then(function(){delete n.commands[s.id]})}};return n.commands[s.id]=r,function(){return r.unsubscribe()}})},b.prototype.ping=function(){return this.sendMessagePromise(a())},b.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},b.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,s){t.commandId+=1;var r=t.commandId;e.id=r,t.commands[r]={resolve:n,reject:s},t.sendMessage(e)})},e.createConnection=f,e.subscribeEntities=m,e.ERR_CANNOT_CONNECT=y,e.ERR_INVALID_AUTH=l,Object.defineProperty(e,"__esModule",{value:!0})});
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(e.HAWS=e.HAWS||{})}(this,function(e){"use strict";function n(e){return{type:"auth",api_password:e}}function t(){return{type:"get_states"}}function s(){return{type:"get_config"}}function r(){return{type:"get_services"}}function i(){return{type:"get_panels"}}function o(e,n,t){var s={type:"call_service",domain:e,service:n};return t&&(s.service_data=t),s}function c(e){var n={type:"subscribe_events"};return e&&(n.event_type=e),n}function u(e){return{type:"unsubscribe_events",subscription:e}}function a(){return{type:"ping"}}function d(e){return e.result}function f(e,n){var t=new y(e,n);return t.connect()}function v(e,n){return new Promise(function(t,s){function r(e){var t=Object.assign({},o.core,{components:o.core.components.concat(e.data.component)});o=Object.assign({},o,{core:t}),n(o)}function i(e){var t,s,r=e.data,i=r.domain,c=r.service,u=Object.assign({},o.services[i]||{},(t={},t[c]={description:"",fields:{}},t));o=Object.assign({},o,{services:Object.assign({},o.services,(s={},s[i]=u,s))}),n(o)}var o=null;Promise.all([e.getConfig(),e.getPanels(),e.getServices(),e.subscribeEvents(r,"component_loaded"),e.subscribeEvents(i,"service_registered")]).then(function(e){var s=e[0],r=e[1],i=e[2],c=e[3],u=e[4];o={core:s,panels:r,services:i},n(o),t(function(){c(),u()})},function(){return s()})})}function p(e){for(var n={},t=0;t<e.length;t++){var s=e[t];n[s.entity_id]=s}return n}function m(e,n){var t=Object.assign({},e);return t[n.entity_id]=n,t}function h(e,n){var t=Object.assign({},e);return delete t[n],t}function b(e,n){return new Promise(function(t,s){function r(e){var t=e.data,s=t.entity_id,r=t.new_state;i=r?m(i,r):h(i,s),n(i)}var i=null,o=e.subscribeEvents(r,"state_changed"),c=e.getStates().then(function(e){i=p(e),n(i)});Promise.all([o,c]).then(function(e){var n=e[0];return t(n)},function(){return s()})})}var l=1,g=2,y=function(e,n){this.url=e,this.options=n||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};y.prototype.addEventListener=function(e,n){var t=this.eventListeners[e];t||(t=this.eventListeners[e]=[]),t.push(n)},y.prototype.fireEvent=function(e){var n=this;(this.eventListeners[e]||[]).forEach(function(e){return e(n)})},y.prototype.connect=function(){var e=this;return new Promise(function(t,s){var r=e.commands;Object.keys(r).forEach(function(e){var n=r[e];n.reject&&n.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(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(n(e.options.authToken));break;case"auth_invalid":s({code:g}),i=!0;break;case"auth_ok":t(e),e.fireEvent("ready"),e.commandId=1,e.commands={},Object.keys(r).forEach(function(n){var t=r[n];t.eventType&&e.subscribeEvents(t.eventCallback,t.eventType).then(function(e){t.unsubscribe=e})})}}),e.socket.addEventListener("close",function(){if(!i&&!e.closeRequested){0===e.connectionTries?e.fireEvent("disconnected"):s(l);var n=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},n)}})})},y.prototype.close=function(){this.closeRequested=!0,this.socket.close()},y.prototype.getStates=function(){return this.sendMessagePromise(t()).then(d)},y.prototype.getServices=function(){return this.sendMessagePromise(r()).then(d)},y.prototype.getPanels=function(){return this.sendMessagePromise(i()).then(d)},y.prototype.getConfig=function(){return this.sendMessagePromise(s()).then(d)},y.prototype.callService=function(e,n,t){return this.sendMessagePromise(o(e,n,t))},y.prototype.subscribeEvents=function(e,n){var t=this;return this.sendMessagePromise(c(n)).then(function(s){var r={eventCallback:e,eventType:n,unsubscribe:function(){return t.sendMessagePromise(u(s.id)).then(function(){delete t.commands[s.id]})}};return t.commands[s.id]=r,function(){return r.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 n=this;return new Promise(function(t,s){n.commandId+=1;var r=n.commandId;e.id=r,n.commands[r]={resolve:t,reject:s},n.sendMessage(e)})},e.ERR_CANNOT_CONNECT=l,e.ERR_INVALID_AUTH=g,e.createConnection=f,e.subscribeConfig=v,e.subscribeEntities=b,Object.defineProperty(e,"__esModule",{value:!0})});
{
"name": "home-assistant-js-websocket",
"version": "0.3.0",
"version": "0.4.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, subscribeEntities } from 'home-assistant-js-websocket';

@@ -16,3 +16,3 @@ function stateChanged(event) {

console.log('Connection established!');
conn.subscribeEvents(stateChanged, 'state_changed');
subscribeEntities(conn, entities => console.log('New entities!', entities));
},

@@ -75,4 +75,6 @@ err => console.error('Connection failed with code', err)

You can subscribe to the entities of Home Assistant. Your callback will be called when the entities are first loaded and on every change after that. The function `subscribeEntities` will return a promise that resolves to an unsubscribe function.
You can subscribe to the entities of Home Assistant. Your callback will be called when the entities are first loaded and on every change after that.
The function `subscribeEntities` will return a promise that resolves to an unsubscribe function.
```javascript

@@ -86,2 +88,16 @@ import { subscribeEntities } from 'home-assistant-js-websocket';

### Config
You can subscribe to the config of Home Assistant. Config can change when either a component gets loaded or a new service gets registered.
The function `subscribeConfig` will return a promise that resolves to an unsubscribe function.
```javascript
import { subscribeConfig } from 'home-assistant-js-websocket';
// conn is the connection from earlier.
subscribeConfig(conn, config => console.log('New config!', config));
```
## Connection API Reference

@@ -88,0 +104,0 @@

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