node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
2 lines (1 loc) • 2.62 kB
JavaScript
"use strict";var y=Object.create;var a=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames;var w=Object.getPrototypeOf,N=Object.prototype.hasOwnProperty;var v=(e,t)=>{for(var o in t)a(e,o,{get:t[o],enumerable:!0})},p=(e,t,o,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of _(t))!N.call(e,i)&&i!==o&&a(e,i,{get:()=>t[i],enumerable:!(n=I(t,i))||n.enumerable});return e};var l=(e,t,o)=>(o=e!=null?y(w(e)):{},p(t||!e||!e.__esModule?a(o,"default",{value:e,enumerable:!0}):o,e)),T=e=>p(a({},"__esModule",{value:!0}),e);var M={};v(M,{default:()=>C});module.exports=T(M);var d=l(require("../../common/controllers/InputOutputController")),u=l(require("../../common/errors/InputError")),m=require("../../common/TransformState"),s=require("../../const"),g=require("../../helpers/mustache"),S=require("../../helpers/utils");class C extends d.default{#e;#t;#o;constructor(t){super(t),this.#e=t.comparatorService,this.#t=t.homeAssistant,this.#o=t.transformState}async onInput({message:t,parsedMessage:o,send:n,done:i}){const f=(0,g.renderTemplate)(o.entityId.value,t,this.node.context(),this.#t.websocket.getStates()),r=this.#t.websocket.getState(f);if(!r)throw new u.default(`Entity could not be found in cache for entityId: ${f}`,"not found");r&&(r.timeSinceChangedMs=Date.now()-new Date(r.last_changed).getTime()),this.node.config.state_type!==m.TransformType.String&&(r.original_state=r.state,r.state=this.#o.transform(this.node.config.state_type,r.state));let c=await this.#e.getComparatorResult(this.node.config.halt_if_compare,this.node.config.halt_if,r.state,this.node.config.halt_if_type,{message:t,entity:r});if(this.#r(c)){const h=await this.#i(t);h>0&&(c=r.timeSinceChangedMs>h)}if(await this.setCustomOutputs(this.node.config.outputProperties,t,{config:this.node.config,entity:r,entityState:r.state,triggerId:f}),(this.node.config.halt_if||this.node.config.halt_if_type==="habool")&&!c){this.status.setFailed(r.state.toString()),n([null,t]),i();return}this.status.setSuccess(r.state.toString()),n([t,null]),i()}#r(t){return t&&(this.node.config.halt_if.length>0||this.node.config.halt_if_type==="habool")&&[s.ComparatorType.Is,s.ComparatorType.IsNot,s.ComparatorType.Includes,s.ComparatorType.DoesNotInclude].includes(this.node.config.halt_if_compare)}async#i(t){if(this.node.config.for==="")return 0;const o=await this.typedInputService.getValue(this.node.config.for,this.node.config.forType,{message:t});if(isNaN(o)||o<0)throw new u.default(`Invalid for value: ${o}`,"home-assistant.status.error");return(0,S.getTimeInMilliseconds)(o,this.node.config.forUnits)}}