node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
2 lines (1 loc) • 2.76 kB
JavaScript
"use strict";var g=Object.create;var s=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var v=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty;var C=(e,t)=>{for(var o in t)s(e,o,{get:t[o],enumerable:!0})},c=(e,t,o,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of y(t))!T.call(e,r)&&r!==o&&s(e,r,{get:()=>t[r],enumerable:!(i=S(t,r))||i.enumerable});return e};var n=(e,t,o)=>(o=e!=null?g(v(e)):{},c(t||!e||!e.__esModule?s(o,"default",{value:e,enumerable:!0}):o,e)),I=e=>c(s({},"__esModule",{value:!0}),e);var w={};C(w,{default:()=>p});module.exports=I(w);var l=n(require("../../common/controllers/ExposeAsMixin")),d=n(require("../../common/controllers/OutputController")),a=n(require("../../common/errors/ConfigError")),f=require("../../common/errors/inputErrorHandler"),m=require("../../common/TransformState"),h=require("../../const"),u=require("../../helpers/utils");const N=(0,l.default)(d.default);class p extends N{#o;#t;#r;#e;constructor(t){super(t),this.#o=t.comparatorService,this.#r=t.transformState}async#i(){let t=this.node.config.updateInterval||"0";this.node.config.updateIntervalType===h.TypedInputTypes.JSONata&&(t=await this.jsonataService.evaluate(t));const o=(0,u.getTimeInMilliseconds)(Number(t),this.node.config.updateIntervalUnits);if(isNaN(o))throw new a.default(["poll-state.error.interval_not_a_number",{interval:t}]);return Number(o)}onClose(){this.#t&&(clearInterval(this.#t),this.#t=void 0)}getNodeEntityId(){return this.node.config.entityId}async onTimer(){if(this.isEnabled===!1)return;const t=this.homeAssistant.websocket.getState(this.node.config.entityId);if(!t)throw new a.default(["poll-state.error.entity_id_not_found",{entity_id:this.node.config.entityId}]);t&&(t.timeSinceChangedMs=Date.now()-new Date(t.last_changed).getTime()),this.node.config.stateType!==m.TransformType.String&&(t.original_state=t.state,t.state=this.#r.transform(this.node.config.stateType,t.state));const o=await this.#o.getComparatorResult(this.node.config.ifStateOperator,this.node.config.ifState,t.state,this.node.config.ifStateType,{entity:t}),i={};await this.setCustomOutputs(this.node.config.outputProperties,i,{config:this.node.config,entity:t,entityState:t.state,triggerId:this.node.config.entityId});const r=`${t.state}`;if((this.node.config.ifState||this.node.config.ifStateType==="habool")&&!o){this.status.setFailed(r),this.node.send([null,i]);return}this.status.setSuccess(r),this.node.send([i,null])}async onIntervalUpdate(){const t=await this.#i();t!==this.#e&&(clearInterval(this.#t),this.#e=t,this.#t=setInterval(async()=>{await this.onTimer().catch(o=>{const{error:i,statusMessage:r}=(0,f.getErrorData)(o);this.status.setError(r),this.node.error(i)})},this.#e))}}