UNPKG

node-red-contrib-home-assistant-websocket

Version:
2 lines (1 loc) 3.91 kB
"use strict";const r=require("joi"),{merge:c}=require("lodash"),u=require("./EventsNode"),{INTEGRATION_UNLOADED:l,INTEGRATION_NOT_LOADED:f}=require("../const"),{STATUS_SHAPE_DOT:y,STATUS_SHAPE_RING:p}=require("../helpers/status"),m={debug:!1,config:{haConfig:{},exposeToHomeAssistant:a=>a.exposeToHomeAssistant===void 0?!1:a.exposeToHomeAssistant}};class v extends u{constructor({node:t,config:i,RED:n,status:e,nodeOptions:s={}}){s=c({},m,s),super({node:t,config:i,RED:n,status:e,nodeOptions:s}),this.nodeConfig.server&&(this.removeFromHA=this.nodeConfig.exposeToHomeAssistant===!1&&this.server.exposedNodes[this.id]===!0,this.server.exposedNodes[this.node.id]=this.nodeConfig.exposeToHomeAssistant),this.init()}get lastPayload(){return this.state.getLastPayload()}set lastPayload(t){this.state.setLastPayload(t)}async init(){this.isIntegrationLoaded&&(this.registerEntity(),this.removeFromHomeAssistant())}async onClose(t){super.onClose(t),t&&this.isIntegrationLoaded&&this.nodeConfig.exposeToHomeAssistant&&this.removeFromHomeAssistant(!0),this.removeSubscription()}onHaEventsOpen(){this.subscription=null}onHaIntegration(t){switch(super.onHaIntegration(t),t){case l:case f:this.removeSubscription(),this.node.type!=="trigger-state"&&(this.isEnabled=!0);break}}getDiscoveryPayload(t){return{type:"nodered/discovery",server_id:this.nodeConfig.server.id,node_id:this.node.id,component:"switch",state:this.isEnabled,config:t}}async registerEntity(t=!0){if(super.registerEntity()===!1)return;const i={};(this.nodeConfig.haConfig||this.nodeConfig.config).filter(e=>e.value.length).forEach(e=>i[e.property]=e.value);try{const e=this.getDiscoveryPayload(i);this.node.debug("Registering with Home Assistant"),this.subscription=await this.homeAssistant.subscribeMessage(this.onHaEventMessage.bind(this),e,{resubscribe:!1})}catch(e){this.status.setFailed(this.RED._("config-server.status.error")),this.node.error(e.message);return}t&&this.status.setSuccess(this.RED._("config-server.status.registered")),this.registered=!0}onHaEventMessage(t){if(t.type===void 0&&(t.type="state_changed"),t.type)switch(t.type){case"state_changed":this.isEnabled=t.state,this.updateHomeAssistant();break;case"automation_triggered":this.handleTriggerMessage(t.data);break}}async handleTriggerMessage(t={}){if(this.isEnabled===!1)return;const i=r.object({entity_id:r.string().allow(null),skip_condition:r.boolean().default(!1),output_path:r.boolean().default(!0)});let n,e,s;try{if(n=await i.validateAsync(t),s=n.entity_id||this.getNodeEntityId(),!s)throw new Error("Entity filter type is not set to exact and no entity_id found in trigger data.");if(e=this.homeAssistant.getStates(s),!e)throw new Error(`entity_id provided by trigger event not found in cache: ${s}`)}catch(g){this.status.setFailed("Error"),this.node.error(`Trigger Error: ${g.message}`,{});return}const o={event_type:"triggered",entity_id:e.entity_id,event:{entity_id:e.entity_id,old_state:e,new_state:e}};if(!n.skip_condition){this.triggerNode(o);return}const d=n.output_path,h={topic:s,payload:o.event.new_state.state,data:o.event};this.status.set({shape:d?y:p,text:this.status.appendDateString(o.event.new_state.state)}),this.send(d?[h,null]:[null,h])}getNodeEntityId(){}triggerNode(){}updateHomeAssistant(){if(!this.isIntegrationLoaded)return;const t={type:"nodered/entity",server_id:this.nodeConfig.server.id,node_id:this.node.id,state:this.isEnabled};this.homeAssistant.send(t)}removeFromHomeAssistant(t=!1){if(!this.homeAssistant.isIntegrationLoaded||!this.removeFromHA&&!t||this.nodeConfig.entityType&&this.nodeConfig.entityType!=="switch")return;const i={...this.getDiscoveryPayload(),remove:!0};this.homeAssistant.send(i),this.removeFromHA=!1,this.removeSubscription(),this.isEnabled=!0}async removeSubscription(){this.subscription&&(this.node.debug("Unregistering from HA"),await this.subscription().catch(()=>{})),this.subscription=null}}module.exports=v;