1 line
7.9 KiB
JavaScript
1 line
7.9 KiB
JavaScript
!function(){"use strict";function e(e){return{type:"auth",api_password:e}}function t(e,t,n){var i={type:"call_service",domain:e,service:t};return n&&(i.service_data=n),i}function n(e){var t={type:"subscribe_events"};return e&&(t.event_type=e),t}function i(e){return{type:"unsubscribe_events",subscription:e}}function s(e,t){return{type:"result",success:!1,error:{code:e,message:t}}}function r(t,n){function i(s,r,o){var c=new WebSocket(t),u=!1,a=function(){if(u)o(b);else if(0!==s){var e=-1===s?-1:s-1;setTimeout(function(){return i(e,r,o)},1e3)}else o(p)};c.addEventListener("message",function t(i){switch(JSON.parse(i.data).type){case"auth_required":"authToken"in n?c.send(JSON.stringify(e(n.authToken))):(u=!0,c.close());break;case"auth_invalid":u=!0,c.close();break;case"auth_ok":c.removeEventListener("message",t),c.removeEventListener("close",a),r(c)}}),c.addEventListener("close",a)}return new Promise(function(e,t){return i(n.setupRetry||0,e,t)})}function o(e){return e.result}function c(e,t){return void 0===t&&(t={}),r(e,t).then(function(n){var i=new g(e,t);return i.setSocket(n),i})}function u(e,t){return e._subscribeConfig?e._subscribeConfig(t):new Promise(function(n,i){var s=null,r=null,o=[],c=null;t&&o.push(t);var u=function(e){s=Object.assign({},s,e);for(var t=0;t<o.length;t++)o[t](s)},a=function(e,t){return u({services:Object.assign({},s.services,(n={},n[e]=t,n))});var n},d=function(){return Promise.all([e.getConfig(),e.getPanels(),e.getServices()]).then(function(e){var t=e[0],n=e[1],i=e[2];u({core:t,panels:n,services:i})})},f=function(e){e&&o.splice(o.indexOf(e),1),0===o.length&&r()};e._subscribeConfig=function(e){return e&&(o.push(e),null!==s&&e(s)),c.then(function(){return function(){return f(e)}})},(c=Promise.all([e.subscribeEvents(function(e){if(null!==s){var t=Object.assign({},s.core,{components:s.core.components.concat(e.data.component)});u({core:t})}},"component_loaded"),e.subscribeEvents(function(e){if(null!==s){var t,n=e.data,i=n.domain,r=n.service,o=Object.assign({},s.services[i]||{},(t={},t[r]={description:"",fields:{}},t));a(i,o)}},"service_registered"),e.subscribeEvents(function(e){if(null!==s){var t=e.data,n=t.domain,i=t.service,r=s.services[n];if(r&&i in r){var o={};Object.keys(r).forEach(function(e){e!==i&&(o[e]=r[e])}),a(n,o)}}},"service_removed"),d()])).then(function(i){var s=i[0],o=i[1],c=i[2];r=function(){removeEventListener("ready",d),s(),o(),c()},e.addEventListener("ready",d),n(function(){return f(t)})},function(){return i()})})}function a(e){for(var t={},n=0;n<e.length;n++){var i=e[n];t[i.entity_id]=i}return t}function d(e,t){var n=Object.assign({},e);return n[t.entity_id]=t,n}function f(e,t){var n=Object.assign({},e);return delete n[t],n}function v(e,t){return e._subscribeEntities?e._subscribeEntities(t):new Promise(function(n,i){function s(){return e.getStates().then(function(e){o=a(e);for(var t=0;t<u.length;t++)u[t](o)})}function r(t){t&&u.splice(u.indexOf(t),1),0===u.length&&(c(),e.removeEventListener("ready",s),e._subscribeEntities=null)}var o=null,c=null,u=[],v=null;t&&u.push(t),e._subscribeEntities=function(e){return e&&(u.push(e),null!==o&&e(o)),v.then(function(){return function(){return r(e)}})},(v=Promise.all([e.subscribeEvents(function(e){if(null!==o){var t=e.data,n=t.entity_id,i=t.new_state;o=i?d(o,i):f(o,n);for(var s=0;s<u.length;s++)u[s](o)}},"state_changed"),s()])).then(function(i){var o=i[0];c=o,e.addEventListener("ready",s),n(function(){return r(t)})},function(){return i()})})}function h(e){return e.substr(0,e.indexOf("."))}function l(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var i=e[t];i&&(n[i.entity_id]=i)}),n}var p=1,b=2,g=function(e,t){this.url=e,this.options=t||{},this.commandId=1,this.commands={},this.eventListeners={},this.closeRequested=!1,this._handleMessage=this._handleMessage.bind(this),this._handleClose=this._handleClose.bind(this)};g.prototype.setSocket=function(e){var t=this,n=this.socket;if(this.socket=e,e.addEventListener("message",this._handleMessage),e.addEventListener("close",this._handleClose),n){var i=this.commands;this.commandId=1,this.commands={},Object.keys(i).forEach(function(e){var n=i[e];n.eventType&&t.subscribeEvents(n.eventCallback,n.eventType).then(function(e){n.unsubscribe=e})}),this.fireEvent("ready")}},g.prototype.addEventListener=function(e,t){var n=this.eventListeners[e];n||(n=this.eventListeners[e]=[]),n.push(t)},g.prototype.removeEventListener=function(e,t){var n=this.eventListeners[e];if(n){var i=n.indexOf(t);-1!==i&&n.splice(i,1)}},g.prototype.fireEvent=function(e){var t=this;(this.eventListeners[e]||[]).forEach(function(e){return e(t)})},g.prototype.close=function(){this.closeRequested=!0,this.socket.close()},g.prototype.getStates=function(){return this.sendMessagePromise({type:"get_states"}).then(o)},g.prototype.getServices=function(){return this.sendMessagePromise({type:"get_services"}).then(o)},g.prototype.getPanels=function(){return this.sendMessagePromise({type:"get_panels"}).then(o)},g.prototype.getConfig=function(){return this.sendMessagePromise({type:"get_config"}).then(o)},g.prototype.callService=function(e,n,i){return this.sendMessagePromise(t(e,n,i))},g.prototype.subscribeEvents=function(e,t){var s=this;return this.sendMessagePromise(n(t)).then(function(n){var r={eventCallback:e,eventType:t,unsubscribe:function(){return s.sendMessagePromise(i(n.id)).then(function(){delete s.commands[n.id]})}};return s.commands[n.id]=r,function(){return r.unsubscribe()}})},g.prototype.ping=function(){return this.sendMessagePromise({type:"ping"})},g.prototype.sendMessage=function(e){this.socket.send(JSON.stringify(e))},g.prototype.sendMessagePromise=function(e){var t=this;return new Promise(function(n,i){t.commandId+=1;var s=t.commandId;e.id=s,t.commands[s]={resolve:n,reject:i},t.sendMessage(e)})},g.prototype._handleMessage=function(e){var t=JSON.parse(e.data);switch(t.type){case"event":this.commands[t.id].eventCallback(t.event);break;case"result":t.success?this.commands[t.id].resolve(t):this.commands[t.id].reject(t.error),delete this.commands[t.id]}},g.prototype._handleClose=function(){var e=this;if(Object.keys(this.commands).forEach(function(t){var n=e.commands[t].reject;n&&n(s(3,"Connection lost"))}),!this.closeRequested){this.fireEvent("disconnected");var t=Object.assign({},this.options,{setupRetry:0});!function n(i){setTimeout(function(){r(e.url,t).then(function(t){return e.setSocket(t)},function(){return n(i+1)})},1e3*Math.min(i,5))}(0)}};var m="group.default_view",y=Object.freeze({ERR_CANNOT_CONNECT:p,ERR_INVALID_AUTH:b,createConnection:c,subscribeConfig:u,subscribeEntities:v,getGroupEntities:l,splitByGroups:function(e){var t=[],n={};return Object.keys(e).forEach(function(i){var s=e[i];"group"===h(i)?t.push(s):n[i]=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}},getViewEntities:function(e,t){var n={};return t.attributes.entity_id.forEach(function(t){var i=e[t];if(i&&!i.attributes.hidden&&(n[i.entity_id]=i,"group"===h(i.entity_id))){var s=l(e,i);Object.keys(s).forEach(function(e){var t=s[e];t.attributes.hidden||(n[e]=t)})}}),n},extractViews:function(e){var t=[];return Object.keys(e).forEach(function(n){var i=e[n];i.attributes.view&&t.push(i)}),t.sort(function(e,t){return e.entity_id===m?-1:t.entity_id===m?1:e.attributes.order-t.attributes.order}),t},extractDomain:h,extractObjectId:function(e){return e.substr(e.indexOf(".")+1)}});window.HAWS=y,window.HASS_DEMO=!1,window.HASS_DEV=!1;var _=window.createHassConnection=function(e){var t=("https:"===window.location.protocol?"wss":"ws")+"://"+window.location.host+"/api/websocket",n={setupRetry:10};return void 0!==e&&(n.authToken=e),c(t,n).then(function(e){return v(e),u(e),e})};window.noAuth?window.hassConnection=_():window.localStorage.authToken?window.hassConnection=_(window.localStorage.authToken):window.hassConnection=null,"serviceWorker"in navigator&&window.addEventListener("load",function(){navigator.serviceWorker.register("/service_worker.js")})}();
|