UNPKG

node-red-contrib-home-assistant-websocket

Version:
2 lines (1 loc) 5.58 kB
"use strict";var V=Object.create;var m=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var H=Object.getOwnPropertyNames;var k=Object.getPrototypeOf,P=Object.prototype.hasOwnProperty;var I=(r,e)=>{for(var t in e)m(r,t,{get:e[t],enumerable:!0})},_=(r,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of H(e))!P.call(r,a)&&a!==t&&m(r,a,{get:()=>e[a],enumerable:!(o=D(e,a))||o.enumerable});return r};var g=(r,e,t)=>(t=r!=null?V(k(r)):{},_(e||!r||!r.__esModule?m(t,"default",{value:r,enumerable:!0}):t,r)),$=r=>_(m({},"__esModule",{value:!0}),r);var A={};I(A,{default:()=>O});module.exports=$(A);var S=require("lodash"),y=g(require("selectn")),w=g(require("../../common/controllers/ExposeAsMixin")),v=g(require("../../common/controllers/InputOutputController")),h=g(require("../../common/errors/ConfigError")),C=require("../../common/TransformState"),E=require("../../helpers/assert"),b=require("../../helpers/mustache"),d=require("../../helpers/utils"),R=require("../../homeAssistant"),i=require("./const");const x=(0,w.default)(v.default);class O extends x{#t;#e;#a;constructor(e){super(e),this.#t=e.comparatorService,this.#e=e.state,this.#a=e.transformState}get isEnabled(){return this.exposeAsConfigNode?super.isEnabled:this.#e?.isEnabled()??!0}async#s(e,t){const o=[];for(const s of e){const{comparatorType:n,comparatorValue:f,comparatorValueDatatype:c,propertyValue:l}=s,u=this.#n(s,t.event),p=(0,y.default)(s.propertyValue,u.state),T=await this.#t.getComparatorResult(n,f,p,c,{entity:t.event.new_state,prevEntity:t.event.old_state});T===!1&&this.debugToClient(`constraint comparator: failed entity "${u.entityId}" property "${l}" with value ${p} failed "${n}" check against (${c}) ${f}`),o.push({constraint:s,constraintTarget:u,actualValue:p,comparatorResult:T})}const a=o.filter(s=>!s.comparatorResult);return{all:o,failed:a}}#n(e,t){const o=e.targetType===i.TargetType.ThisEntity,a=o?t.entity_id:e.targetValue,s=o?t:this.homeAssistant.websocket.getState(a);if(s===null)throw new h.default(["trigger-state.error.entity_id_not_found",{entity_id:a}]);const n={entityId:a,state:s};return!o&&e.propertyType===i.PropertyType.CurrentState&&(n.state={entity_id:a,old_state:null,new_state:this.homeAssistant.websocket.getState(a)}),n}async#o(e,t){return e.reduce(async(o,a)=>{const s={output:a,comparatorMatched:!0,actualValue:null,message:null};return a.comparatorPropertyType!==i.ComparatorPropertyType.Always&&(s.actualValue=(0,y.default)(a.comparatorPropertyValue,t.event),s.comparatorMatched=await this.#t.getComparatorResult(a.comparatorType,a.comparatorValue,s.actualValue,a.comparatorValueDataType,{entity:t.event.new_state,prevEntity:t.event.old_state})),s.message=await this.#i(s,t),[...await o,s]},Promise.resolve([]))}#r(e,t){const{entity_id:o,event:a}=t,s={topic:o,payload:a.new_state?.state,data:t};let n;return e.failed.length?(this.debugToClient("constraint comparator: one or more comparators failed to match constraints, message will send on the failed output"),s.failedComparators=e.failed,n=[null,s]):n=[s,null],n}async#i({output:e,comparatorMatched:t,actualValue:o},a){if(!t)return this.debugToClient(`output comparator failed: property "${e.comparatorPropertyValue}" with value ${o} failed "${e.comparatorType}" check against (${e.comparatorValueDataType}) ${e.comparatorValue}`),null;let s={topic:a.entity_id,payload:a.event.new_state?.state,data:a};if(e.messageType===i.MessageType.Custom||e.messageType===i.MessageType.Payload){let n=e.messageValue;if((0,d.containsMustache)(e.messageValue)&&(n=(0,b.renderTemplate)(e.messageValue,a.event,this.node.context(),this.homeAssistant.websocket.getStates())),n=await this.typedInputService.getValue(n,e.messageValueType,{eventData:a}),e.messageType===i.MessageType.Custom){if(!(0,E.isRecord)(n))throw new h.default("trigger-state.error.custom_output_message_needs_to_be_object");s=n}else s={payload:n}}return s}onInputEnabled(e){const t=e.payload===i.ENABLE;return this.#e?.setEnabled(t),this.enableExposeAs(t),!0}onInputTesting(e){const{entity_id:t,new_state:o,old_state:a}=e.payload;if(t&&o&&a){const s={event_type:R.HaEvent.StateChanged,entity_id:t,event:e.payload};this.onEntityStateChanged(s)}return!0}async onEntityStateChanged(e){if(this.isEnabled===!1||!this.homeAssistant.isHomeAssistantRunning)return;const t=(0,S.cloneDeep)(e);if(!t.event.new_state||!Object.entries(this.node.config.entities).some(([l,u])=>u?.some(p=>(0,d.shouldIncludeEvent)(t.entity_id,p,l))))return;t.event.old_state&&this.node.config.stateType!==C.TransformType.String&&(t.event.old_state.original_state=t.event.old_state.state,t.event.old_state.state=this.#a.transform(this.node.config.stateType,t.event.old_state.state)),t.event.new_state&&this.node.config.stateType!==C.TransformType.String&&(t.event.new_state.original_state=t.event.new_state.state,t.event.new_state.state=this.#a.transform(this.node.config.stateType,t.event.new_state.state)),t.event.new_state&&(t.event.new_state.timeSinceChangedMs=Date.now()-new Date(t.event.new_state.last_changed).getTime());const a=await this.#s(this.node.config.constraints,t);let s=this.#r(a,t);const n=t.event.new_state.state.toString();if(a.failed.length||!this.node.config.customOutputs.length){a.failed.length?this.status.setFailed(n):this.status.setSuccess(n),this.debugToClient("done processing sending messages:"),this.debugToClient(s),this.node.send(s);return}const c=(await this.#o(this.node.config.customOutputs,t)).map(l=>l.message);s=s.concat(c),this.debugToClient(`done processing sending messages: ${s}`),this.status.setSuccess(n),this.node.send(s)}}