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.5.0 to 0.6.0

LICENSE.md

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 r={type:"call_service",domain:e,service:t};return n&&(r.service_data=n),r}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 error(e,t){return{type:"result",success:!1,error:{code:e,message:t}}}function extractResult(e){return e.result}function createConnection(e,t){var n=new Connection(e,t);return n.connect()}function subscribeConfig(e,t){return new Promise(function(n,r){function s(e){var n=Object.assign({},o.core,{components:o.core.components.concat(e.data.component)});o=Object.assign({},o,{core:n}),t(o)}function i(e){var n,r,s=e.data,i=s.domain,c=s.service,u=Object.assign({},o.services[i]||{},(n={},n[c]={description:"",fields:{}},n));o=Object.assign({},o,{services:Object.assign({},o.services,(r={},r[i]=u,r))}),t(o)}var o=null;Promise.all([e.getConfig(),e.getPanels(),e.getServices(),e.subscribeEvents(s,"component_loaded"),e.subscribeEvents(i,"service_registered")]).then(function(e){var r=e[0],s=e[1],i=e[2],c=e[3],u=e[4];o={core:r,panels:s,services:i},t(o),n(function(){c(),u()})},function(){return r()})})}function getEntities(e){for(var t={},n=0;n<e.length;n++){var r=e[n];t[r.entity_id]=r}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,r){function s(e){var n=e.data,r=n.entity_id,s=n.new_state;i=s?updateState(i,s):removeState(i,r),t(i)}var i=null,o=e.subscribeEvents(s,"state_changed"),c=e.getStates().then(function(e){i=getEntities(e),t(i)});Promise.all([o,c]).then(function(e){var t=e[0];return n(t)},function(){return r()})})}function extractDomain(e){return e.substr(0,e.indexOf("."))}function extractObjectId(e){return e.substr(e.indexOf(".")+1)}function getGroupEntities(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var r=e[t];r&&(n[r.entity_id]=r)}),n}function splitByGroups(e){var t=[],n={};return Object.keys(e).forEach(function(r){var s=e[r];"group"===extractDomain(r)?t.push(s):n[r]=s}),t.sort(function(e,t){return e.attributes.order-t.attributes.order}),t.forEach(function(e){return e.attributes.entity_id.forEach(function(e){delete n[e]})}),{groups:t,ungrouped:n}}function getViewEntities(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var r=e[t];r&&!r.attributes.hidden&&(n[r.entity_id]=r,"group"===extractDomain(r.entity_id)&&Object.assign(n,getGroupEntities(e,r)))}),n}function extractViews(e){var t=[];return Object.keys(e).forEach(function(n){var r=e[n];r.attributes.view&&t.push(r)}),t.sort(function(e,t){return e.entity_id===DEFAULT_VIEW_ENTITY_ID?-1:t.entity_id===DEFAULT_VIEW_ENTITY_ID?1:e.attributes.order-t.attributes.order}),t}Object.defineProperty(exports,"__esModule",{value:!0});var ERR_CANNOT_CONNECT=1,ERR_INVALID_AUTH=2,ERR_CONNECTION_LOST=3,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 r=e.commands;Object.keys(r).forEach(function(e){var t=r[e];t.reject&&t.reject(error(ERR_CONNECTION_LOST,"Connection lost"))});var s=!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":n(ERR_INVALID_AUTH),s=!0;break;case"auth_ok":t(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(!s&&!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(r){var s={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(unsubscribeEvents(r.id)).then(function(){delete n.commands[r.id]})}};return n.commands[r.id]=s,function(){return s.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,r){t.commandId+=1;var s=t.commandId;e.id=s,t.commands[s]={resolve:n,reject:r},t.sendMessage(e)})};var DEFAULT_VIEW_ENTITY_ID="group.default_view";exports.ERR_CANNOT_CONNECT=ERR_CANNOT_CONNECT,exports.ERR_INVALID_AUTH=ERR_INVALID_AUTH,exports.createConnection=createConnection,exports.subscribeConfig=subscribeConfig,exports.subscribeEntities=subscribeEntities,exports.getGroupEntities=getGroupEntities,exports.splitByGroups=splitByGroups,exports.getViewEntities=getViewEntities,exports.extractViews=extractViews,exports.extractDomain=extractDomain,exports.extractObjectId=extractObjectId;
"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(e,t,n){var r={type:"call_service",domain:e,service:t};return n&&(r.service_data=n),r}function subscribeEvents(e){var t={type:"subscribe_events"};return e&&(t.event_type=e),t}function unsubscribeEvents(e){return{type:"unsubscribe_events",subscription:e}}function ping(){return{type:"ping"}}function error(e,t){return{type:"result",success:!1,error:{code:e,message:t}}}function extractResult(e){return e.result}function createConnection(e,t){var n=new Connection(e,t);return n.connect()}function subscribeConfig(e,t){return new Promise(function(n,r){function s(e){var n=Object.assign({},c.core,{components:c.core.components.concat(e.data.component)});c=Object.assign({},c,{core:n}),t(c)}function i(e){var n,r=e.data,s=r.domain,i=r.service,o=Object.assign({},c.services[s]||{},(n={},n[i]={description:"",fields:{}},n));c=Object.assign({},c,{services:Object.assign({},c.services,(u={},u[s]=o,u))});var u;t(c)}function o(){return Promise.all([e.getConfig(),e.getPanels(),e.getServices()]).then(function(e){var n=e[0],r=e[1],s=e[2];c={core:n,panels:r,services:s},t(c)})}var c=null;Promise.all([e.subscribeEvents(s,"component_loaded"),e.subscribeEvents(i,"service_registered"),o()]).then(function(t){var r=t[0],s=t[1];e.addEventListener("ready",o),n(function(){removeEventListener("ready",o),r(),s()})},function(){return r()})})}function getEntities(e){for(var t={},n=0;n<e.length;n++){var r=e[n];t[r.entity_id]=r}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,r){function s(e){var n=e.data,r=n.entity_id,s=n.new_state;o=s?updateState(o,s):removeState(o,r),t(o)}function i(){return e.getStates().then(function(e){o=getEntities(e),t(o)})}var o=null;Promise.all([e.subscribeEvents(s,"state_changed"),i()]).then(function(t){var r=t[0];e.addEventListener("ready",i),n(function(){e.removeEventListener("ready",i),r()})},function(){return r()})})}function extractDomain(e){return e.substr(0,e.indexOf("."))}function extractObjectId(e){return e.substr(e.indexOf(".")+1)}function getGroupEntities(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var r=e[t];r&&(n[r.entity_id]=r)}),n}function splitByGroups(e){var t=[],n={};return Object.keys(e).forEach(function(r){var s=e[r];"group"===extractDomain(r)?t.push(s):n[r]=s}),t.sort(function(e,t){return e.attributes.order-t.attributes.order}),t.forEach(function(e){return e.attributes.entity_id.forEach(function(e){delete n[e]})}),{groups:t,ungrouped:n}}function getViewEntities(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var r=e[t];r&&!r.attributes.hidden&&(n[r.entity_id]=r,"group"===extractDomain(r.entity_id)&&Object.assign(n,getGroupEntities(e,r)))}),n}function extractViews(e){var t=[];return Object.keys(e).forEach(function(n){var r=e[n];r.attributes.view&&t.push(r)}),t.sort(function(e,t){return e.entity_id===DEFAULT_VIEW_ENTITY_ID?-1:t.entity_id===DEFAULT_VIEW_ENTITY_ID?1:e.attributes.order-t.attributes.order}),t}Object.defineProperty(exports,"__esModule",{value:!0});var ERR_CANNOT_CONNECT=1,ERR_INVALID_AUTH=2,ERR_CONNECTION_LOST=3,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.removeEventListener=function(e,t){var n=this.eventListeners[e];if(n){var r=n.indexOf(t);r!==-1&&n.splice(r,1)}},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 r=e.commands;Object.keys(r).forEach(function(e){var t=r[e];t.reject&&t.reject(error(ERR_CONNECTION_LOST,"Connection lost"))});var s=!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":n(ERR_INVALID_AUTH),s=!0;break;case"auth_ok":t(e),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.fireEvent("ready")}}),e.socket.addEventListener("close",function(){if(!s&&!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(e,t,n))},Connection.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(subscribeEvents(t)).then(function(r){var s={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(unsubscribeEvents(r.id)).then(function(){delete n.commands[r.id]})}};return n.commands[r.id]=s,function(){return s.unsubscribe()}})},Connection.prototype.ping=function(){return this.sendMessagePromise(ping())},Connection.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},Connection.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,r){t.commandId+=1;var s=t.commandId;e.id=s,t.commands[s]={resolve:n,reject:r},t.sendMessage(e)})};var DEFAULT_VIEW_ENTITY_ID="group.default_view";exports.ERR_CANNOT_CONNECT=ERR_CANNOT_CONNECT,exports.ERR_INVALID_AUTH=ERR_INVALID_AUTH,exports.createConnection=createConnection,exports.subscribeConfig=subscribeConfig,exports.subscribeEntities=subscribeEntities,exports.getGroupEntities=getGroupEntities,exports.splitByGroups=splitByGroups,exports.getViewEntities=getViewEntities,exports.extractViews=extractViews,exports.extractDomain=extractDomain,exports.extractObjectId=extractObjectId;

@@ -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,t,n){var r={type:"call_service",domain:e,service:t};return n&&(r.service_data=n),r}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 error(e,t){return{type:"result",success:!1,error:{code:e,message:t}}}function extractResult(e){return e.result}function createConnection(e,t){var n=new Connection(e,t);return n.connect()}function subscribeConfig(e,t){return new Promise(function(n,r){function i(e){var n=Object.assign({},o.core,{components:o.core.components.concat(e.data.component)});o=Object.assign({},o,{core:n}),t(o)}function s(e){var n,r,i=e.data,s=i.domain,c=i.service,u=Object.assign({},o.services[s]||{},(n={},n[c]={description:"",fields:{}},n));o=Object.assign({},o,{services:Object.assign({},o.services,(r={},r[s]=u,r))}),t(o)}var o=null;Promise.all([e.getConfig(),e.getPanels(),e.getServices(),e.subscribeEvents(i,"component_loaded"),e.subscribeEvents(s,"service_registered")]).then(function(e){var r=e[0],i=e[1],s=e[2],c=e[3],u=e[4];o={core:r,panels:i,services:s},t(o),n(function(){c(),u()})},function(){return r()})})}function getEntities(e){for(var t={},n=0;n<e.length;n++){var r=e[n];t[r.entity_id]=r}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,r){function i(e){var n=e.data,r=n.entity_id,i=n.new_state;s=i?updateState(s,i):removeState(s,r),t(s)}var s=null,o=e.subscribeEvents(i,"state_changed"),c=e.getStates().then(function(e){s=getEntities(e),t(s)});Promise.all([o,c]).then(function(e){var t=e[0];return n(t)},function(){return r()})})}function extractDomain(e){return e.substr(0,e.indexOf("."))}function extractObjectId(e){return e.substr(e.indexOf(".")+1)}function getGroupEntities(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var r=e[t];r&&(n[r.entity_id]=r)}),n}function splitByGroups(e){var t=[],n={};return Object.keys(e).forEach(function(r){var i=e[r];"group"===extractDomain(r)?t.push(i):n[r]=i}),t.sort(function(e,t){return e.attributes.order-t.attributes.order}),t.forEach(function(e){return e.attributes.entity_id.forEach(function(e){delete n[e]})}),{groups:t,ungrouped:n}}function getViewEntities(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var r=e[t];r&&!r.attributes.hidden&&(n[r.entity_id]=r,"group"===extractDomain(r.entity_id)&&Object.assign(n,getGroupEntities(e,r)))}),n}function extractViews(e){var t=[];return Object.keys(e).forEach(function(n){var r=e[n];r.attributes.view&&t.push(r)}),t.sort(function(e,t){return e.entity_id===DEFAULT_VIEW_ENTITY_ID?-1:t.entity_id===DEFAULT_VIEW_ENTITY_ID?1:e.attributes.order-t.attributes.order}),t}var ERR_CANNOT_CONNECT=1,ERR_INVALID_AUTH=2,ERR_CONNECTION_LOST=3,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 r=e.commands;Object.keys(r).forEach(function(e){var t=r[e];t.reject&&t.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(s){var o=JSON.parse(s.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(ERR_INVALID_AUTH),i=!0;break;case"auth_ok":t(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"):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(r){var i={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(unsubscribeEvents(r.id)).then(function(){delete n.commands[r.id]})}};return n.commands[r.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,r){t.commandId+=1;var i=t.commandId;e.id=i,t.commands[i]={resolve:n,reject:r},t.sendMessage(e)})};var DEFAULT_VIEW_ENTITY_ID="group.default_view";export{ERR_CANNOT_CONNECT,ERR_INVALID_AUTH,createConnection,subscribeConfig,subscribeEntities,getGroupEntities,splitByGroups,getViewEntities,extractViews,extractDomain,extractObjectId};
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(e,t,n){var r={type:"call_service",domain:e,service:t};return n&&(r.service_data=n),r}function subscribeEvents(e){var t={type:"subscribe_events"};return e&&(t.event_type=e),t}function unsubscribeEvents(e){return{type:"unsubscribe_events",subscription:e}}function ping(){return{type:"ping"}}function error(e,t){return{type:"result",success:!1,error:{code:e,message:t}}}function extractResult(e){return e.result}function createConnection(e,t){var n=new Connection(e,t);return n.connect()}function subscribeConfig(e,t){return new Promise(function(n,r){function i(e){var n=Object.assign({},c.core,{components:c.core.components.concat(e.data.component)});c=Object.assign({},c,{core:n}),t(c)}function s(e){var n,r=e.data,i=r.domain,s=r.service,o=Object.assign({},c.services[i]||{},(n={},n[s]={description:"",fields:{}},n));c=Object.assign({},c,{services:Object.assign({},c.services,(u={},u[i]=o,u))});var u;t(c)}function o(){return Promise.all([e.getConfig(),e.getPanels(),e.getServices()]).then(function(e){var n=e[0],r=e[1],i=e[2];c={core:n,panels:r,services:i},t(c)})}var c=null;Promise.all([e.subscribeEvents(i,"component_loaded"),e.subscribeEvents(s,"service_registered"),o()]).then(function(t){var r=t[0],i=t[1];e.addEventListener("ready",o),n(function(){removeEventListener("ready",o),r(),i()})},function(){return r()})})}function getEntities(e){for(var t={},n=0;n<e.length;n++){var r=e[n];t[r.entity_id]=r}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,r){function i(e){var n=e.data,r=n.entity_id,i=n.new_state;o=i?updateState(o,i):removeState(o,r),t(o)}function s(){return e.getStates().then(function(e){o=getEntities(e),t(o)})}var o=null;Promise.all([e.subscribeEvents(i,"state_changed"),s()]).then(function(t){var r=t[0];e.addEventListener("ready",s),n(function(){e.removeEventListener("ready",s),r()})},function(){return r()})})}function extractDomain(e){return e.substr(0,e.indexOf("."))}function extractObjectId(e){return e.substr(e.indexOf(".")+1)}function getGroupEntities(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var r=e[t];r&&(n[r.entity_id]=r)}),n}function splitByGroups(e){var t=[],n={};return Object.keys(e).forEach(function(r){var i=e[r];"group"===extractDomain(r)?t.push(i):n[r]=i}),t.sort(function(e,t){return e.attributes.order-t.attributes.order}),t.forEach(function(e){return e.attributes.entity_id.forEach(function(e){delete n[e]})}),{groups:t,ungrouped:n}}function getViewEntities(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var r=e[t];r&&!r.attributes.hidden&&(n[r.entity_id]=r,"group"===extractDomain(r.entity_id)&&Object.assign(n,getGroupEntities(e,r)))}),n}function extractViews(e){var t=[];return Object.keys(e).forEach(function(n){var r=e[n];r.attributes.view&&t.push(r)}),t.sort(function(e,t){return e.entity_id===DEFAULT_VIEW_ENTITY_ID?-1:t.entity_id===DEFAULT_VIEW_ENTITY_ID?1:e.attributes.order-t.attributes.order}),t}var ERR_CANNOT_CONNECT=1,ERR_INVALID_AUTH=2,ERR_CONNECTION_LOST=3,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.removeEventListener=function(e,t){var n=this.eventListeners[e];if(n){var r=n.indexOf(t);r!==-1&&n.splice(r,1)}},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 r=e.commands;Object.keys(r).forEach(function(e){var t=r[e];t.reject&&t.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(s){var o=JSON.parse(s.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(ERR_INVALID_AUTH),i=!0;break;case"auth_ok":t(e),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.fireEvent("ready")}}),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(e,t,n))},Connection.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(subscribeEvents(t)).then(function(r){var i={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(unsubscribeEvents(r.id)).then(function(){delete n.commands[r.id]})}};return n.commands[r.id]=i,function(){return i.unsubscribe()}})},Connection.prototype.ping=function(){return this.sendMessagePromise(ping())},Connection.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},Connection.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,r){t.commandId+=1;var i=t.commandId;e.id=i,t.commands[i]={resolve:n,reject:r},t.sendMessage(e)})};var DEFAULT_VIEW_ENTITY_ID="group.default_view";export{ERR_CANNOT_CONNECT,ERR_INVALID_AUTH,createConnection,subscribeConfig,subscribeEntities,getGroupEntities,splitByGroups,getViewEntities,extractViews,extractDomain,extractObjectId};

@@ -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 r(){return{type:"get_config"}}function s(){return{type:"get_services"}}function i(){return{type:"get_panels"}}function o(e,t,n){var r={type:"call_service",domain:e,service:t};return n&&(r.service_data=n),r}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,t){return{type:"result",success:!1,error:{code:e,message:t}}}function f(e){return e.result}function v(e,t){var n=new w(e,t);return n.connect()}function p(e,t){return new Promise(function(n,r){function s(e){var n=Object.assign({},o.core,{components:o.core.components.concat(e.data.component)});o=Object.assign({},o,{core:n}),t(o)}function i(e){var n,r,s=e.data,i=s.domain,c=s.service,u=Object.assign({},o.services[i]||{},(n={},n[c]={description:"",fields:{}},n));o=Object.assign({},o,{services:Object.assign({},o.services,(r={},r[i]=u,r))}),t(o)}var o=null;Promise.all([e.getConfig(),e.getPanels(),e.getServices(),e.subscribeEvents(s,"component_loaded"),e.subscribeEvents(i,"service_registered")]).then(function(e){var r=e[0],s=e[1],i=e[2],c=e[3],u=e[4];o={core:r,panels:s,services:i},t(o),n(function(){c(),u()})},function(){return r()})})}function b(e){for(var t={},n=0;n<e.length;n++){var r=e[n];t[r.entity_id]=r}return t}function h(e,t){var n=Object.assign({},e);return n[t.entity_id]=t,n}function m(e,t){var n=Object.assign({},e);return delete n[t],n}function g(e,t){return new Promise(function(n,r){function s(e){var n=e.data,r=n.entity_id,s=n.new_state;i=s?h(i,s):m(i,r),t(i)}var i=null,o=e.subscribeEvents(s,"state_changed"),c=e.getStates().then(function(e){i=b(e),t(i)});Promise.all([o,c]).then(function(e){var t=e[0];return n(t)},function(){return r()})})}function y(e){return e.substr(0,e.indexOf("."))}function l(e){return e.substr(e.indexOf(".")+1)}function _(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var r=e[t];r&&(n[r.entity_id]=r)}),n}function E(e){var t=[],n={};return Object.keys(e).forEach(function(r){var s=e[r];"group"===y(r)?t.push(s):n[r]=s}),t.sort(function(e,t){return e.attributes.order-t.attributes.order}),t.forEach(function(e){return e.attributes.entity_id.forEach(function(e){delete n[e]})}),{groups:t,ungrouped:n}}function k(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var r=e[t];r&&!r.attributes.hidden&&(n[r.entity_id]=r,"group"===y(r.entity_id)&&Object.assign(n,_(e,r)))}),n}function O(e){var t=[];return Object.keys(e).forEach(function(n){var r=e[n];r.attributes.view&&t.push(r)}),t.sort(function(e,t){return e.entity_id===T?-1:t.entity_id===T?1:e.attributes.order-t.attributes.order}),t}var j=1,P=2,M=3,w=function(e,t){this.url=e,this.options=t||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};w.prototype.addEventListener=function(e,t){var n=this.eventListeners[e];n||(n=this.eventListeners[e]=[]),n.push(t)},w.prototype.fireEvent=function(e){var t=this;(this.eventListeners[e]||[]).forEach(function(e){return e(t)})},w.prototype.connect=function(){var e=this;return new Promise(function(n,r){var s=e.commands;Object.keys(s).forEach(function(e){var t=s[e];t.reject&&t.reject(d(M,"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(t(e.options.authToken));break;case"auth_invalid":r(P),i=!0;break;case"auth_ok":n(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"):r(j);var t=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},t)}})})},w.prototype.close=function(){this.closeRequested=!0,this.socket.close()},w.prototype.getStates=function(){return this.sendMessagePromise(n()).then(f)},w.prototype.getServices=function(){return this.sendMessagePromise(s()).then(f)},w.prototype.getPanels=function(){return this.sendMessagePromise(i()).then(f)},w.prototype.getConfig=function(){return this.sendMessagePromise(r()).then(f)},w.prototype.callService=function(e,t,n){return this.sendMessagePromise(o(e,t,n))},w.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(c(t)).then(function(r){var s={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(u(r.id)).then(function(){delete n.commands[r.id]})}};return n.commands[r.id]=s,function(){return s.unsubscribe()}})},w.prototype.ping=function(){return this.sendMessagePromise(a())},w.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},w.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,r){t.commandId+=1;var s=t.commandId;e.id=s,t.commands[s]={resolve:n,reject:r},t.sendMessage(e)})};var T="group.default_view";e.ERR_CANNOT_CONNECT=j,e.ERR_INVALID_AUTH=P,e.createConnection=v,e.subscribeConfig=p,e.subscribeEntities=g,e.getGroupEntities=_,e.splitByGroups=E,e.getViewEntities=k,e.extractViews=O,e.extractDomain=y,e.extractObjectId=l,Object.defineProperty(e,"__esModule",{value:!0})});
!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 r(){return{type:"get_config"}}function s(){return{type:"get_services"}}function i(){return{type:"get_panels"}}function o(e,t,n){var r={type:"call_service",domain:e,service:t};return n&&(r.service_data=n),r}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,t){return{type:"result",success:!1,error:{code:e,message:t}}}function f(e){return e.result}function v(e,t){var n=new M(e,t);return n.connect()}function p(e,t){return new Promise(function(n,r){function s(e){var n=Object.assign({},c.core,{components:c.core.components.concat(e.data.component)});c=Object.assign({},c,{core:n}),t(c)}function i(e){var n,r=e.data,s=r.domain,i=r.service,o=Object.assign({},c.services[s]||{},(n={},n[i]={description:"",fields:{}},n));c=Object.assign({},c,{services:Object.assign({},c.services,(u={},u[s]=o,u))});var u;t(c)}function o(){return Promise.all([e.getConfig(),e.getPanels(),e.getServices()]).then(function(e){var n=e[0],r=e[1],s=e[2];c={core:n,panels:r,services:s},t(c)})}var c=null;Promise.all([e.subscribeEvents(s,"component_loaded"),e.subscribeEvents(i,"service_registered"),o()]).then(function(t){var r=t[0],s=t[1];e.addEventListener("ready",o),n(function(){removeEventListener("ready",o),r(),s()})},function(){return r()})})}function b(e){for(var t={},n=0;n<e.length;n++){var r=e[n];t[r.entity_id]=r}return t}function m(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 y(e,t){return new Promise(function(n,r){function s(e){var n=e.data,r=n.entity_id,s=n.new_state;o=s?m(o,s):h(o,r),t(o)}function i(){return e.getStates().then(function(e){o=b(e),t(o)})}var o=null;Promise.all([e.subscribeEvents(s,"state_changed"),i()]).then(function(t){var r=t[0];e.addEventListener("ready",i),n(function(){e.removeEventListener("ready",i),r()})},function(){return r()})})}function g(e){return e.substr(0,e.indexOf("."))}function l(e){return e.substr(e.indexOf(".")+1)}function _(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var r=e[t];r&&(n[r.entity_id]=r)}),n}function E(e){var t=[],n={};return Object.keys(e).forEach(function(r){var s=e[r];"group"===g(r)?t.push(s):n[r]=s}),t.sort(function(e,t){return e.attributes.order-t.attributes.order}),t.forEach(function(e){return e.attributes.entity_id.forEach(function(e){delete n[e]})}),{groups:t,ungrouped:n}}function k(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var r=e[t];r&&!r.attributes.hidden&&(n[r.entity_id]=r,"group"===g(r.entity_id)&&Object.assign(n,_(e,r)))}),n}function O(e){var t=[];return Object.keys(e).forEach(function(n){var r=e[n];r.attributes.view&&t.push(r)}),t.sort(function(e,t){return e.entity_id===w?-1:t.entity_id===w?1:e.attributes.order-t.attributes.order}),t}var j=1,P=2,L=3,M=function(e,t){this.url=e,this.options=t||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};M.prototype.addEventListener=function(e,t){var n=this.eventListeners[e];n||(n=this.eventListeners[e]=[]),n.push(t)},M.prototype.removeEventListener=function(e,t){var n=this.eventListeners[e];if(n){var r=n.indexOf(t);r!==-1&&n.splice(r,1)}},M.prototype.fireEvent=function(e){var t=this;(this.eventListeners[e]||[]).forEach(function(e){return e(t)})},M.prototype.connect=function(){var e=this;return new Promise(function(n,r){var s=e.commands;Object.keys(s).forEach(function(e){var t=s[e];t.reject&&t.reject(d(L,"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(t(e.options.authToken));break;case"auth_invalid":r(P),i=!0;break;case"auth_ok":n(e),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.fireEvent("ready")}}),e.socket.addEventListener("close",function(){if(!i&&!e.closeRequested){0===e.connectionTries?e.fireEvent("disconnected"):r(j);var t=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},t)}})})},M.prototype.close=function(){this.closeRequested=!0,this.socket.close()},M.prototype.getStates=function(){return this.sendMessagePromise(n()).then(f)},M.prototype.getServices=function(){return this.sendMessagePromise(s()).then(f)},M.prototype.getPanels=function(){return this.sendMessagePromise(i()).then(f)},M.prototype.getConfig=function(){return this.sendMessagePromise(r()).then(f)},M.prototype.callService=function(e,t,n){return this.sendMessagePromise(o(e,t,n))},M.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(c(t)).then(function(r){var s={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(u(r.id)).then(function(){delete n.commands[r.id]})}};return n.commands[r.id]=s,function(){return s.unsubscribe()}})},M.prototype.ping=function(){return this.sendMessagePromise(a())},M.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},M.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,r){t.commandId+=1;var s=t.commandId;e.id=s,t.commands[s]={resolve:n,reject:r},t.sendMessage(e)})};var w="group.default_view";e.ERR_CANNOT_CONNECT=j,e.ERR_INVALID_AUTH=P,e.createConnection=v,e.subscribeConfig=p,e.subscribeEntities=y,e.getGroupEntities=_,e.splitByGroups=E,e.getViewEntities=k,e.extractViews=O,e.extractDomain=g,e.extractObjectId=l,Object.defineProperty(e,"__esModule",{value:!0})});
{
"name": "home-assistant-js-websocket",
"version": "0.5.0",
"version": "0.6.0",
"description": "Home Assistant websocket client",

@@ -22,12 +22,12 @@ "main": "dist/haws.cjs.js",

"devDependencies": {
"eslint": "^3.11.0",
"eslint-config-airbnb-base": "^10.0.1",
"eslint": "^3.14.0",
"eslint-config-airbnb-base": "^11.0.1",
"eslint-plugin-import": "^2.2.0",
"faucet": "^0.0.1",
"reify": "^0.4.4",
"rollup": "^0.36.4",
"rollup-plugin-buble": "^0.14.0",
"rollup": "^0.41.4",
"rollup-plugin-buble": "^0.15.0",
"rollup-plugin-replace": "^1.1.1",
"rollup-plugin-uglify": "^1.0.1",
"rollup-watch": "^2.5.0",
"rollup-watch": "^3.2.2",
"tape": "^4.6.3",

@@ -34,0 +34,0 @@ "tape-watch": "^2.2.4",

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