node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
2 lines (1 loc) • 4.03 kB
JavaScript
"use strict";var y=Object.create;var u=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var M=Object.getPrototypeOf,x=Object.prototype.hasOwnProperty;var L=(n,t)=>{for(var e in t)u(n,e,{get:t[e],enumerable:!0})},m=(n,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of S(t))!x.call(n,i)&&i!==e&&u(n,i,{get:()=>t[i],enumerable:!(s=E(t,i))||s.enumerable});return n};var h=(n,t,e)=>(e=n!=null?y(M(n)):{},m(t||!n||!n.__esModule?u(e,"default",{value:n,enumerable:!0}):e,n)),O=n=>m(u({},"__esModule",{value:!0}),n);var _={};L(_,{default:()=>a});module.exports=O(_);var f=h(require("../../common/controllers/ExposeAsMixin")),l=h(require("../../common/controllers/OutputController")),c=require("../../common/errors/inputErrorHandler"),g=require("../../globals"),p=require("../../helpers/utils"),v=require("./CalendarItem"),T=h(require("./EventQueue")),r=require("./helpers"),I=require("./retryWithBackoff"),w=h(require("./SentEventCache"));const P=(0,f.default)(l.default);class a extends P{static{this.OVERLAP_MS=6e4}static{this.POLL_INTERVAL_MS=9e5}#e=null;#s=new w.default;#n=null;#t=new T.default(this.#s);async startPolling(){this.#n||(await this.#o(),this.#n=(0,c.setIntervalWithErrorHandling)(()=>this.#o(),a.POLL_INTERVAL_MS,{status:this.status}))}onClose(){this.#n&&(clearInterval(this.#n),this.#n=null),this.#e&&(clearTimeout(this.#e),this.#e=null),this.#t.clear(),this.#s.clear()}async#i(){const t=await this.typedInputService.getValue(this.node.config.offset,this.node.config.offsetType);return(0,p.getTimeInMilliseconds)(t,this.node.config.offsetUnits)}async#u(){const t=Date.now(),e=Math.abs(await this.#i());return{start:new Date(t-a.OVERLAP_MS-e),end:new Date(t+a.POLL_INTERVAL_MS+a.OVERLAP_MS+e)}}async#h(t){let e=[];try{e=await(0,I.retryWithBackoff)(()=>this.homeAssistant.http.get(`/calendars/${this.node.config.entityId}`,{start:t.start.toISOString(),end:t.end.toISOString()}),{retries:5,baseMs:1e3,maxMs:3e4,beforeRetry:(s,i)=>{this.status.setFailed(["ha-events-calendar.status.fetch_failed",{attempt:s+1,max_attempts:5,delay:Math.round(i/1e3)}])},onGiveUp:s=>{this.status.setError(["ha-events-calendar.status.fetch_max_failed",{error_message:s?.message??s}])}})}catch{return[]}return Array.isArray(e)?e:[]}async#c(t){const e=(0,v.createCalendarItem)(t);let s=e.date(this.node.config.eventType);if(this.#s.has(e.uniqueId))return;e.isAllDayEvent&&(s=(0,r.alignDateToMidnight)(s));const i=await this.#i()??0,o=new Date(s.getTime()+i);if((0,r.isTriggerTimeInPast)(o,Date.now()))return;const d=this.node.config.filter?await this.typedInputService.getValue(this.node.config.filter,this.node.config.filterType):void 0;if(!(d&&!e.summary.includes(d)))return{triggerTime:o,event:e}}#a(){if(this.#e&&(clearTimeout(this.#e),this.#e=null),this.#t.length===0)return;const t=Date.now(),e=this.#t.nextEvent.triggerTime.getTime(),s=Math.max(e-t,0);this.#e=(0,c.setTimeoutWithErrorHandling)(async()=>{await this.#r(),this.#a()},s,{status:this.status})}async#r(){const t=Math.floor(Date.now()/1e3),e=await this.#i()??0;for(;this.#t.length>0;){const s=this.#t.nextEvent;if(Math.floor(s.triggerTime.getTime()/1e3)>t)break;if(this.#t.shift(),this.#s.has(s.event.uniqueId))continue;const o={};await this.setCustomOutputs(this.node.config.outputProperties,o,{calendarItem:s.event.convertToObject()}),this.node.send(o),this.status.setSuccess(g.RED._("ha-events-calendar.status.sent",{summary:(0,r.shortenString)(s.event.summary,32)})),this.#s.mark(s.event,e,a.OVERLAP_MS)}}async#o(){this.#s.prune();const t=await this.#u(),e=await this.#h(t);await this.#d(e),this.#m(),await this.#r(),this.#a()}async#d(t){let e=0;for(const s of t){const i=await this.#c(s);i&&this.#t.add(i)&&e++}return e}#m(){const t=this.#t.length;t>1?this.status.setSending(["ha-events-calendar.status.queued-multi",{count:this.#t.length}]):t>0?this.status.setSending("ha-events-calendar.status.queued-one"):this.status.setSending(["ha-events-calendar.status.queued-none",{minutes:a.POLL_INTERVAL_MS/6e4}])}}