node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
2 lines (1 loc) • 2.69 kB
JavaScript
;var n=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var g=(a,t)=>{for(var e in t)n(a,e,{get:t[e],enumerable:!0})},v=(a,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of p(t))!y.call(a,i)&&i!==e&&n(a,i,{get:()=>t[i],enumerable:!(s=m(t,i))||s.enumerable});return a};var u=a=>v(n({},"__esModule",{value:!0}),a);var _={};g(_,{default:()=>h});module.exports=u(_);var d=require("lodash"),l=require("../../globals"),r=require("../../homeAssistant"),o=require("../../homeAssistant/Websocket");class h{#s;#e;#i;constructor(t,e,s){this.#s=s,this.#e=e,this.#i=t,this.startListeners()}startListeners(){this.#s.addListeners(this,[["ha_events:state_changed",this.onStateChanged],[o.ClientEvent.Integration,this.onIntegrationEvent],[o.ClientEvent.StatesLoaded,this.onStatesLoaded],[r.HaEvent.AreaRegistryUpdated,this.onAreaRegistryUpdate],[r.HaEvent.DeviceRegistryUpdated,this.onDeviceRegistryUpdate],[r.HaEvent.FloorRegistryUpdated,this.onFloorRegistryUpdate],[r.HaEvent.LabelRegistryUpdated,this.onLabelRegistryUpdate],[r.HaEvent.EntityRegistryUpdated,this.onEntityRegistryUpdate],[r.HaEvent.ServicesUpdated,this.onServicesUpdated]])}publish(t,e,s=!0){l.RED.comms.publish(`homeassistant/${t}/${this.#i}`,e,s)}onAreaRegistryUpdate(t){this.publish("areas",t)}onDeviceRegistryUpdate(t){const e=t.map(s=>({area_id:s.area_id,id:s.id,labels:s.labels,name:s.name,name_by_user:s.name_by_user}));this.publish("devices",e)}onFloorRegistryUpdate(t){this.publish("floors",t)}onLabelRegistryUpdate(t){this.publish("labels",t)}onEntityRegistryUpdate(t){const e=t.map(s=>({area_id:s.area_id,device_id:s.device_id,entity_id:s.entity_id,id:s.id,labels:s.labels,name:s.name,original_name:s.original_name,platform:s.platform}));this.publish("entityRegistry",e)}onIntegrationEvent(t){this.publish("integration",{event:t,version:this.#e.integrationVersion})}onServicesUpdated(t){this.publish("services",t)}#t=new Map;#a=(0,d.throttle)(()=>{this.publish("entity",Array.from(this.#t.values()),!1),this.#t.clear()},1e3);onStateChanged(t){const e={entity_id:t.entity_id,state:t.event.new_state?.state,attributes:{device_class:t.event.new_state?.attributes.device_class,friendly_name:t.event.new_state?.attributes.friendly_name,supported_features:t.event.new_state?.attributes.supported_features}};e&&(this.#t.set(t.entity_id,e),this.#a())}onStatesLoaded(t){const e={};Object.keys(t).forEach(s=>{const i=t[s];e[s]={entity_id:s,state:i.state,attributes:{device_class:i.attributes.device_class,friendly_name:i.attributes.friendly_name,supported_features:i.attributes.supported_features}}}),this.publish("entities",e)}}