node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
2 lines (1 loc) • 2.06 kB
JavaScript
;var P=Object.create;var b=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var U=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty;var O=(e,t)=>{for(var n in t)b(e,n,{get:t[n],enumerable:!0})},S=(e,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of v(t))!W.call(e,s)&&s!==n&&b(e,s,{get:()=>t[s],enumerable:!(i=T(t,s))||i.enumerable});return e};var k=(e,t,n)=>(n=e!=null?P(U(e)):{},S(t||!e||!e.__esModule?b(n,"default",{value:e,enumerable:!0}):n,e)),C=e=>S(b({},"__esModule",{value:!0}),e);var D={};O(D,{atLeastHaVersion:()=>y,default:()=>A});module.exports=C(D);var _=k(require("debug")),r=require("home-assistant-js-websocket"),E=k(require("ws")),N=require("./Websocket");const h=(0,_.default)("home-assistant:socket");function A({auth:e,connectionDelay:t,eventBus:n,rejectUnauthorizedCerts:i,url:s}){h("[Auth Phase] Initializing",s);function a(c,m){h("[Auth Phase] New connection",s),n.emit(N.ClientEvent.Connecting);const o=new E.default(s,{rejectUnauthorized:i});let d=!1;const g=async()=>{try{o.send(JSON.stringify(e))}catch(l){d=l===r.ERR_INVALID_AUTH,o.close()}},p=(l,H)=>{if(H)return;const f=JSON.parse(l.toString());switch(h("[Auth Phase] Received",f),f.type){case r.MSG_TYPE_AUTH_INVALID:d=!0,o.close();break;case r.MSG_TYPE_AUTH_OK:o.off("open",g),o.off("message",p),o.off("close",u),o.off("error",u),o.haVersion=f.ha_version,y(o.haVersion,2022,9)&&o.send(JSON.stringify({type:"supported_features",id:1,features:{coalesce_messages:1}})),c(o);break;default:f.type!==r.MSG_TYPE_AUTH_REQUIRED&&h("[Auth Phase] Unhandled message",f)}},u=()=>{if(o.off("close",u),d){m(r.ERR_INVALID_AUTH);return}setTimeout(()=>a(c,m),5e3)};o.on("open",g),o.on("message",p),o.on("close",u),o.on("error",u)}return new Promise((c,m)=>{setTimeout(()=>a(c,m),t!==!1?5e3:0)})}function y(e,t,n,i){const[s,a,c]=e.split(".",3);return Number(s)>t||Number(s)===t&&(i===void 0?Number(a)>=n:Number(a)>n)||i!==void 0&&Number(s)===t&&Number(a)===n&&Number(c)>=i}0&&(module.exports={atLeastHaVersion});