home-assistant-js-websocket
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -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 processEvent(e,t){var n=t.data.new_state;n?e.updateState(n):e.removeState(t.data.entity_id)}function createEntityStore(e){return new Promise(function(t,n){var s=new EntityStore,i=e.subscribeEvents(processEvent.bind(null,s),"state_changed").then(function(e){s.unsubscribe=e}),r=e.getStates().then(function(e){return s.setStates(e)});Promise.all([i,r]).then(function(){return t(s)},function(){return n()})})}Object.defineProperty(exports,"__esModule",{value:!0});var ERR_CANNOT_CONNECT=1,ERR_INVALID_AUTH=2,Connection=function(e,t){this.url=e,this.options=t||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};Connection.prototype.addEventListener=function(e,t){var n=this.eventListeners[e];n||(n=this.eventListeners[e]=[]),n.push(t)},Connection.prototype.fireEvent=function(e){var t=this;(this.eventListeners[e]||[]).forEach(function(e){return e(t)})},Connection.prototype.connect=function(){var e=this;return new Promise(function(t,n){var s=e.commands;Object.keys(s).forEach(function(e){var t=s[e];t.reject&&t.reject()});var i=!1;e.connectionTries+=1,e.socket=new WebSocket(e.url),e.socket.addEventListener("open",function(){e.connectionTries=0}),e.socket.addEventListener("message",function(r){var o=JSON.parse(r.data);switch(o.type){case"event":e.commands[o.id].eventCallback(o.event);break;case"result":o.success?e.commands[o.id].resolve(o):e.commands[o.id].reject(o.error),delete e.commands[o.id];break;case"pong":break;case"auth_required":e.sendMessage(auth(e.options.authToken));break;case"auth_invalid":n({code:ERR_INVALID_AUTH}),i=!0;break;case"auth_ok":t(e),e.fireEvent("ready"),e.commandId=1,e.commands={},Object.keys(s).forEach(function(t){var n=s[t];n.eventType&&e.subscribeEvents(n.eventCallback,n.eventType).then(function(e){n.unsubscribe=e})})}}),e.socket.addEventListener("close",function(){if(!i&&!e.closeRequested){0===e.connectionTries?e.fireEvent("disconnected"):n(ERR_CANNOT_CONNECT);var t=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},t)}})})},Connection.prototype.close=function(){this.closeRequested=!0,this.socket.close()},Connection.prototype.getStates=function(){return this.sendMessagePromise(states()).then(extractResult)},Connection.prototype.getServices=function(){return this.sendMessagePromise(services()).then(extractResult)},Connection.prototype.getPanels=function(){return this.sendMessagePromise(panels()).then(extractResult)},Connection.prototype.getConfig=function(){return this.sendMessagePromise(config()).then(extractResult)},Connection.prototype.callService=function(e,t,n){return this.sendMessagePromise(callService$1(e,t,n))},Connection.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(subscribeEvents$1(t)).then(function(s){var i={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(unsubscribeEvents(s.id)).then(function(){delete n.commands[s.id]})}};return n.commands[s.id]=i,function(){return i.unsubscribe()}})},Connection.prototype.ping=function(){return this.sendMessagePromise(ping$1())},Connection.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},Connection.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,s){t.commandId+=1;var i=t.commandId;e.id=i,t.commands[i]={resolve:n,reject:s},t.sendMessage(e)})};var EntityStore=function(){this.listeners={},this.entities={}};EntityStore.prototype.addEventListener=function(e,t){var n=this.listeners[e];n||(n=this.listeners[e]=[]),n.push(t)},EntityStore.prototype.fireEvent=function(e){var t=this;(this.listeners[e]||[]).forEach(function(e){return e(t.entities)})},EntityStore.prototype.setStates=function(e){for(var t={},n=0;n<e.length;n++){var s=e[n];t[s.entity_id]=s}this.entities=t,this.fireEvent("change")},EntityStore.prototype.updateState=function(e){var t=Object.assign({},this.entities);t[e.entity_id]=e,this.entities=t,this.fireEvent("change")},EntityStore.prototype.removeState=function(e){var t=Object.assign({},this.entities);delete t[e],this.entities=t,this.fireEvent("change")},EntityStore.prototype.unsubscribe=function(){},exports.createConnection=createConnection,exports.createEntityStore=createEntityStore,exports.ERR_CANNOT_CONNECT=ERR_CANNOT_CONNECT,exports.ERR_INVALID_AUTH=ERR_INVALID_AUTH; | ||
"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; |
@@ -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 s={type:"call_service",domain:e,service:t};return n&&(s.service_data=n),s}function subscribeEvents$1(e){var t={type:"subscribe_events"};return e&&(t.event_type=e),t}function unsubscribeEvents(e){return{type:"unsubscribe_events",subscription:e}}function ping$1(){return{type:"ping"}}function extractResult(e){return e.result}function createConnection(e,t){var n=new Connection(e,t);return n.connect()}function processEvent(e,t){var n=t.data.new_state;n?e.updateState(n):e.removeState(t.data.entity_id)}function createEntityStore(e){return new Promise(function(t,n){var s=new EntityStore,i=e.subscribeEvents(processEvent.bind(null,s),"state_changed").then(function(e){s.unsubscribe=e}),r=e.getStates().then(function(e){return s.setStates(e)});Promise.all([i,r]).then(function(){return t(s)},function(){return n()})})}var ERR_CANNOT_CONNECT=1,ERR_INVALID_AUTH=2,Connection=function(e,t){this.url=e,this.options=t||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};Connection.prototype.addEventListener=function(e,t){var n=this.eventListeners[e];n||(n=this.eventListeners[e]=[]),n.push(t)},Connection.prototype.fireEvent=function(e){var t=this;(this.eventListeners[e]||[]).forEach(function(e){return e(t)})},Connection.prototype.connect=function(){var e=this;return new Promise(function(t,n){var s=e.commands;Object.keys(s).forEach(function(e){var t=s[e];t.reject&&t.reject()});var i=!1;e.connectionTries+=1,e.socket=new WebSocket(e.url),e.socket.addEventListener("open",function(){e.connectionTries=0}),e.socket.addEventListener("message",function(r){var o=JSON.parse(r.data);switch(o.type){case"event":e.commands[o.id].eventCallback(o.event);break;case"result":o.success?e.commands[o.id].resolve(o):e.commands[o.id].reject(o.error),delete e.commands[o.id];break;case"pong":break;case"auth_required":e.sendMessage(auth(e.options.authToken));break;case"auth_invalid":n({code:ERR_INVALID_AUTH}),i=!0;break;case"auth_ok":t(e),e.fireEvent("ready"),e.commandId=1,e.commands={},Object.keys(s).forEach(function(t){var n=s[t];n.eventType&&e.subscribeEvents(n.eventCallback,n.eventType).then(function(e){n.unsubscribe=e})})}}),e.socket.addEventListener("close",function(){if(!i&&!e.closeRequested){0===e.connectionTries?e.fireEvent("disconnected"):n(ERR_CANNOT_CONNECT);var t=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},t)}})})},Connection.prototype.close=function(){this.closeRequested=!0,this.socket.close()},Connection.prototype.getStates=function(){return this.sendMessagePromise(states()).then(extractResult)},Connection.prototype.getServices=function(){return this.sendMessagePromise(services()).then(extractResult)},Connection.prototype.getPanels=function(){return this.sendMessagePromise(panels()).then(extractResult)},Connection.prototype.getConfig=function(){return this.sendMessagePromise(config()).then(extractResult)},Connection.prototype.callService=function(e,t,n){return this.sendMessagePromise(callService$1(e,t,n))},Connection.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(subscribeEvents$1(t)).then(function(s){var i={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(unsubscribeEvents(s.id)).then(function(){delete n.commands[s.id]})}};return n.commands[s.id]=i,function(){return i.unsubscribe()}})},Connection.prototype.ping=function(){return this.sendMessagePromise(ping$1())},Connection.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},Connection.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,s){t.commandId+=1;var i=t.commandId;e.id=i,t.commands[i]={resolve:n,reject:s},t.sendMessage(e)})};var EntityStore=function(){this.listeners={},this.entities={}};EntityStore.prototype.addEventListener=function(e,t){var n=this.listeners[e];n||(n=this.listeners[e]=[]),n.push(t)},EntityStore.prototype.fireEvent=function(e){var t=this;(this.listeners[e]||[]).forEach(function(e){return e(t.entities)})},EntityStore.prototype.setStates=function(e){for(var t={},n=0;n<e.length;n++){var s=e[n];t[s.entity_id]=s}this.entities=t,this.fireEvent("change")},EntityStore.prototype.updateState=function(e){var t=Object.assign({},this.entities);t[e.entity_id]=e,this.entities=t,this.fireEvent("change")},EntityStore.prototype.removeState=function(e){var t=Object.assign({},this.entities);delete t[e],this.entities=t,this.fireEvent("change")},EntityStore.prototype.unsubscribe=function(){};export{createConnection:createConnection,createEntityStore:createEntityStore,ERR_CANNOT_CONNECT:ERR_CANNOT_CONNECT,ERR_INVALID_AUTH:ERR_INVALID_AUTH}; | ||
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}; |
@@ -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 i(){return{type:"get_services"}}function r(){return{type:"get_panels"}}function o(e,t,n){var s={type:"call_service",domain:e,service:t};return n&&(s.service_data=n),s}function c(e){var t={type:"subscribe_events"};return e&&(t.event_type=e),t}function u(e){return{type:"unsubscribe_events",subscription:e}}function a(){return{type:"ping"}}function d(e){return e.result}function f(e,t){var n=new y(e,t);return n.connect()}function p(e,t){var n=t.data.new_state;n?e.updateState(n):e.removeState(t.data.entity_id)}function v(e){return new Promise(function(t,n){var s=new l,i=e.subscribeEvents(p.bind(null,s),"state_changed").then(function(e){s.unsubscribe=e}),r=e.getStates().then(function(e){return s.setStates(e)});Promise.all([i,r]).then(function(){return t(s)},function(){return n()})})}var h=1,m=2,y=function(e,t){this.url=e,this.options=t||{},this.commandId=1,this.commands={},this.connectionTries=0,this.eventListeners={},this.closeRequested=!1};y.prototype.addEventListener=function(e,t){var n=this.eventListeners[e];n||(n=this.eventListeners[e]=[]),n.push(t)},y.prototype.fireEvent=function(e){var t=this;(this.eventListeners[e]||[]).forEach(function(e){return e(t)})},y.prototype.connect=function(){var e=this;return new Promise(function(n,s){var i=e.commands;Object.keys(i).forEach(function(e){var t=i[e];t.reject&&t.reject()});var r=!1;e.connectionTries+=1,e.socket=new WebSocket(e.url),e.socket.addEventListener("open",function(){e.connectionTries=0}),e.socket.addEventListener("message",function(o){var c=JSON.parse(o.data);switch(c.type){case"event":e.commands[c.id].eventCallback(c.event);break;case"result":c.success?e.commands[c.id].resolve(c):e.commands[c.id].reject(c.error),delete e.commands[c.id];break;case"pong":break;case"auth_required":e.sendMessage(t(e.options.authToken));break;case"auth_invalid":s({code:m}),r=!0;break;case"auth_ok":n(e),e.fireEvent("ready"),e.commandId=1,e.commands={},Object.keys(i).forEach(function(t){var n=i[t];n.eventType&&e.subscribeEvents(n.eventCallback,n.eventType).then(function(e){n.unsubscribe=e})})}}),e.socket.addEventListener("close",function(){if(!r&&!e.closeRequested){0===e.connectionTries?e.fireEvent("disconnected"):s(h);var t=1e3*Math.min(e.connectionTries,5);setTimeout(function(){return e.connect()},t)}})})},y.prototype.close=function(){this.closeRequested=!0,this.socket.close()},y.prototype.getStates=function(){return this.sendMessagePromise(n()).then(d)},y.prototype.getServices=function(){return this.sendMessagePromise(i()).then(d)},y.prototype.getPanels=function(){return this.sendMessagePromise(r()).then(d)},y.prototype.getConfig=function(){return this.sendMessagePromise(s()).then(d)},y.prototype.callService=function(e,t,n){return this.sendMessagePromise(o(e,t,n))},y.prototype.subscribeEvents=function(e,t){var n=this;return this.sendMessagePromise(c(t)).then(function(s){var i={eventCallback:e,eventType:t,unsubscribe:function(){return n.sendMessagePromise(u(s.id)).then(function(){delete n.commands[s.id]})}};return n.commands[s.id]=i,function(){return i.unsubscribe()}})},y.prototype.ping=function(){return this.sendMessagePromise(a())},y.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},y.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,s){t.commandId+=1;var i=t.commandId;e.id=i,t.commands[i]={resolve:n,reject:s},t.sendMessage(e)})};var l=function(){this.listeners={},this.entities={}};l.prototype.addEventListener=function(e,t){var n=this.listeners[e];n||(n=this.listeners[e]=[]),n.push(t)},l.prototype.fireEvent=function(e){var t=this;(this.listeners[e]||[]).forEach(function(e){return e(t.entities)})},l.prototype.setStates=function(e){for(var t={},n=0;n<e.length;n++){var s=e[n];t[s.entity_id]=s}this.entities=t,this.fireEvent("change")},l.prototype.updateState=function(e){var t=Object.assign({},this.entities);t[e.entity_id]=e,this.entities=t,this.fireEvent("change")},l.prototype.removeState=function(e){var t=Object.assign({},this.entities);delete t[e],this.entities=t,this.fireEvent("change")},l.prototype.unsubscribe=function(){},e.createConnection=f,e.createEntityStore=v,e.ERR_CANNOT_CONNECT=h,e.ERR_INVALID_AUTH=m,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!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})}); |
{ | ||
"name": "home-assistant-js-websocket", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Home Assistant websocket client", | ||
@@ -5,0 +5,0 @@ "main": "dist/haws.cjs.js", |
# :aerial_tramway: JavaScript websocket client for Home Assistant | ||
This is a websocket client written in JavaScript that communicates with the Home Assistant websocket API. It can be used to integrate Home Assistant into your apps. | ||
This is a websocket client written in JavaScript that communicates with the Home Assistant websocket API. It can be used to integrate Home Assistant into your apps. It has 0 dependencies. | ||
@@ -21,2 +21,4 @@ ```javascript | ||
[Try it on JSFiddle.](https://jsfiddle.net/balloob/9w3oyswa/) | ||
## Usage | ||
@@ -72,15 +74,10 @@ | ||
An Entity Store is available that at all times will represent the latest information about the available entities. It is possible to listen for `change` events on the store to be notified when it gets updated. | ||
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 | ||
import { createEntityStore } from 'home-assistant-js-websocket'; | ||
import { subscribeEntities } from 'home-assistant-js-websocket'; | ||
// conn is connection from earlier. | ||
// conn is the connection from earlier. | ||
createEntityStore(conn).then((entityStore) => { | ||
console.log('Current entities', entityStore.entities); | ||
entityStore.addEventListener('change', entities => console.log('Store updated!', entities)); | ||
}); | ||
subscribeEntities(conn, entities => console.log('New entities!', entities)); | ||
``` | ||
@@ -87,0 +84,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
18714
50
114