node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
2 lines (1 loc) • 5.22 kB
JavaScript
"use strict";var U=Object.create;var p=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var R=Object.getPrototypeOf,D=Object.prototype.hasOwnProperty;var B=(n,e)=>{for(var s in e)p(n,s,{get:e[s],enumerable:!0})},w=(n,e,s,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of L(e))!D.call(n,i)&&i!==s&&p(n,i,{get:()=>e[i],enumerable:!(t=T(e,i))||t.enumerable});return n};var d=(n,e,s)=>(s=n!=null?U(R(n)):{},w(e||!n||!n.__esModule?p(s,"default",{value:n,enumerable:!0}):s,n)),x=n=>w(p({},"__esModule",{value:!0}),n);var q={};B(q,{IssueType:()=>F,default:()=>O});module.exports=x(q);var f=require("../../../globals"),c=require("../../../homeAssistant"),P=require("../../../homeAssistant/Websocket"),l=d(require("../../../nodes/action/issue-check")),g=d(require("../../../nodes/current-state/issue-check")),m=d(require("../../../nodes/device/issue-check")),I=d(require("../../../nodes/events-calendar/issue-check")),v=d(require("../../../nodes/events-state/issue-check")),y=d(require("../../../nodes/get-history/issue-check")),b=d(require("../../../nodes/poll-state/issue-check")),N=d(require("../../../nodes/tag/issue-check")),S=d(require("../../../nodes/time/issue-check")),C=d(require("../../../nodes/trigger-state/issue-check")),H=d(require("../../../nodes/wait-until/issue-check")),A=d(require("../../../nodes/zone/issue-check")),E=d(require("../../services/StorageService")),r=require("./utils"),M=(e=>(e.FlowsStarted="flows:started",e))(M||{}),z=(s=>(s.Full="full",s.Nodes="nodes",s))(z||{}),F=(a=>(a.AreaId="areaId",a.DeprecatedConfig="deprecatedConfig",a.DeviceId="deviceId",a.EntityId="entityId",a.FloorId="floorId",a.InvalidAction="invalidAction",a.LabelId="labelId",a.StateId="stateId",a.TagId="tagId",a))(F||{});const _={areaId:c.HaEvent.AreaRegistryUpdated,deviceId:c.HaEvent.DeviceRegistryUpdated,floorId:c.HaEvent.FloorRegistryUpdated,labelId:c.HaEvent.LabelRegistryUpdated},W=360*60*1e3;class ${#d=!1;#i=new Map;#s=new Map;#e=new Set;#t=[];#o={};init(e){this.#d||(this.#d=!0,e&&(Array.isArray(e)?this.#t.push(...e):this.#t.push(e)),this.#b(),f.RED.events.on("flows:started",s=>{this.#p(),this.#l(s)}),setInterval(this.#g.bind(this),W))}#u(e){if(e.type==="full")return this.#s.clear(),e.config.flows;const s=new Set;if(e.diff){for(const o of e.diff.changed)e.diff.removed.includes(o)||s.add(o);for(const o of e.diff.added)s.add(o);for(const o of e.diff.removed)this.#h(o)}return Array.from(s).reduce((o,h)=>{const u=e.config.flows.find(k=>k.id===h);return u&&o.push(u),o},[])}#p(){f.RED.nodes.eachNode(e=>{e.type==="tab"&&(this.#o[e.id]=e.disabled)})}#l(e){const s=this.#u(e).filter(t=>!(0,r.isNodeDisabled)(t,this.#o[t.z]));this.#a(s)}#g(){const e=[],s=new Set;f.RED.nodes?.eachNode(t=>{this.#e.has(t.id)&&s.add(t.id),(0,r.isNodeDisabled)(t,this.#o[t.z])||e.push(t)}),this.#e=s,this.#f(),this.#a(e)}#a(e){for(const s of e){if(!(0,r.isHomeAssistantNode)(s))continue;if((0,r.isHomeAssistantDataLoaded)(s)){this.#r(s);continue}const t=(0,r.getServerId)(s);if(!t)continue;this.#i.has(t)?this.#i.get(t)?.push(s):this.#i.set(t,[s]),(0,r.getHomeAssistant)(s)?.eventBus.once(P.ClientEvent.RegistriesLoaded,()=>{const o=this.#i.get(t);if(o)for(const h of o)this.#r(h);this.#i.delete(t)})}}#m=[[l.isActionNodeProperties,l.default],[g.isCurrentStateNodeProperties,g.default],[m.isDeviceNodeProperties,m.default],[I.isEventsCalendarNodeProperties,I.default],[v.isEventsStateNodeProperties,v.default],[y.isGetHistoryNodeProperties,y.default],[b.isPollStateNodeProperties,b.default],[N.isTagNodeProperties,N.default],[S.isTimeNodeProperties,S.default],[C.isTriggerStateProperties,C.default],[H.isWaitUntilNodeProperties,H.default],[A.isZoneNodeProperties,A.default]];#r(e){let s=[];for(const[t,i]of this.#m)if(t(e)){s=i(e);break}return s.length?(this.#I(e,s),this.#y(e.id,s)):this.#h(e.id),s}#I(e,s){const t=(0,r.getHomeAssistant)(e);if(t)for(const i of s)switch(i.type){case"stateId":{const o=`ha_events:state_changed:${i.identity}`,h=u=>{if(u?.entity_id===i.identity){const k=this.#r(e);(0,r.includesIssue)(k,i)||i.unsubscribe?.()}};this.#c(t,i,o,h);break}case"areaId":case"deviceId":case"entityId":case"floorId":case"labelId":this.#v(t,i,e);break}}#v(e,s,t){const i=_[s.type],o=()=>{const h=this.#r(t);(0,r.includesIssue)(h,s)||s.unsubscribe?.()};this.#c(e,s,i,o)}#y(e,s){(0,r.isIssuesEqual)(s,this.#s.get(e)||[])||(this.#s.set(e,s),f.RED.log.debug(`[Home Assistant] Issue added: ${e}`),this.#n())}#h(e){if(!this.#s.has(e))return;this.#s.get(e)?.forEach(t=>{t.unsubscribe?.()}),this.#s.delete(e),this.#e.delete(e),f.RED.log.debug(`[Home Assistant] Issue removed: ${e}`),this.#n()}#n(){const e=Array.from(this.#s).map(([s,t])=>({nodeId:s,messages:t.map(i=>i.message),hide:this.#e.has(s)}));this.#t.forEach(s=>s(e))}toggleIssueHiddenStatus(e){this.#e.has(e)?this.#e.delete(e):this.#e.add(e),this.#f(),this.#n()}#b(){this.#e=new Set(E.default.getIssues())}#f(){E.default.saveIssues(Array.from(this.#e))}#c(e,s,t,i){e.eventBus.on(t,i),s.unsubscribe=()=>{e.eventBus.removeListener(t,i),s.unsubscribe=void 0}}addUpdateHandler(e){this.#t.push(e)}removeUpdateHandler(e){const s=this.#t.indexOf(e);s!==-1&&this.#t.splice(s,1)}}var O=new $;0&&(module.exports={IssueType});