New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.4.0 to 0.4.1

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,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;
"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 error(e,n){return{type:"result",success:!1,error:{code:e,message:n}}}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 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,a=Object.assign({},o.services[i]||{},(t={},t[c]={description:"",fields:{}},t));o=Object.assign({},o,{services:Object.assign({},o.services,(s={},s[i]=a,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],a=e[4];o={core:s,panels:r,services:i},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 r(e){var t=e.data,s=t.entity_id,r=t.new_state;i=r?updateState(i,r):removeState(i,s),n(i)}var i=null,o=e.subscribeEvents(r,"state_changed"),c=e.getStates().then(function(e){i=getEntities(e),n(i)});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,ERR_CONNECTION_LOST=3,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(error(ERR_CONNECTION_LOST,"Connection lost"))});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(i){var o=JSON.parse(i.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(ERR_INVALID_AUTH),r=!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(!r&&!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 r={eventCallback:e,eventType:n,unsubscribe:function(){return t.sendMessagePromise(unsubscribeEvents(s.id)).then(function(){delete t.commands[s.id]})}};return t.commands[s.id]=r,function(){return r.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 r=n.commandId;e.id=r,n.commands[r]={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 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};
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 error(e,n){return{type:"result",success:!1,error:{code:e,message:n}}}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,ERR_CONNECTION_LOST=3,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(error(ERR_CONNECTION_LOST,"Connection lost"))});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(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,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})});
!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,n){return{type:"result",success:!1,error:{code:e,message:n}}}function f(e){return e.result}function v(e,n){var t=new k(e,n);return t.connect()}function p(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 m(e){for(var n={},t=0;t<e.length;t++){var s=e[t];n[s.entity_id]=s}return n}function h(e,n){var t=Object.assign({},e);return t[n.entity_id]=n,t}function l(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?h(i,r):l(i,s),n(i)}var i=null,o=e.subscribeEvents(r,"state_changed"),c=e.getStates().then(function(e){i=m(e),n(i)});Promise.all([o,c]).then(function(e){var n=e[0];return t(n)},function(){return s()})})}var g=1,y=2,_=3,k=function(e,n){this.url=e,this.options=n||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};k.prototype.addEventListener=function(e,n){var t=this.eventListeners[e];t||(t=this.eventListeners[e]=[]),t.push(n)},k.prototype.fireEvent=function(e){var n=this;(this.eventListeners[e]||[]).forEach(function(e){return e(n)})},k.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(d(_,"Connection lost"))});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(y),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(g);var n=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},n)}})})},k.prototype.close=function(){this.closeRequested=!0,this.socket.close()},k.prototype.getStates=function(){return this.sendMessagePromise(t()).then(f)},k.prototype.getServices=function(){return this.sendMessagePromise(r()).then(f)},k.prototype.getPanels=function(){return this.sendMessagePromise(i()).then(f)},k.prototype.getConfig=function(){return this.sendMessagePromise(s()).then(f)},k.prototype.callService=function(e,n,t){return this.sendMessagePromise(o(e,n,t))},k.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()}})},k.prototype.ping=function(){return this.sendMessagePromise(a())},k.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},k.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=g,e.ERR_INVALID_AUTH=y,e.createConnection=v,e.subscribeConfig=p,e.subscribeEntities=b,Object.defineProperty(e,"__esModule",{value:!0})});
{
"name": "home-assistant-js-websocket",
"version": "0.4.0",
"version": "0.4.1",
"description": "Home Assistant websocket client",

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

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