node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
130 lines (129 loc) • 6.62 kB
JavaScript
"use strict";
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Comms_clientEvents, _Comms_homeAssistant, _Comms_serverId, _Comms_stateChangedBatchedUpdates, _Comms_throttledStateChangedPublish;
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const globals_1 = require("../../globals");
const homeAssistant_1 = require("../../homeAssistant");
const Websocket_1 = require("../../homeAssistant/Websocket");
class Comms {
constructor(serverId, homeAssistant, clientEvents) {
_Comms_clientEvents.set(this, void 0);
_Comms_homeAssistant.set(this, void 0);
_Comms_serverId.set(this, void 0);
_Comms_stateChangedBatchedUpdates.set(this, new Map());
_Comms_throttledStateChangedPublish.set(this, (0, lodash_1.throttle)(() => {
this.publish('entity', Array.from(__classPrivateFieldGet(this, _Comms_stateChangedBatchedUpdates, "f").values()), false);
__classPrivateFieldGet(this, _Comms_stateChangedBatchedUpdates, "f").clear();
}, 1000));
__classPrivateFieldSet(this, _Comms_clientEvents, clientEvents, "f");
__classPrivateFieldSet(this, _Comms_homeAssistant, homeAssistant, "f");
__classPrivateFieldSet(this, _Comms_serverId, serverId, "f");
this.startListeners();
}
startListeners() {
__classPrivateFieldGet(this, _Comms_clientEvents, "f").addListeners(this, [
['ha_events:state_changed', this.onStateChanged],
[Websocket_1.ClientEvent.Integration, this.onIntegrationEvent],
[Websocket_1.ClientEvent.StatesLoaded, this.onStatesLoaded],
[homeAssistant_1.HaEvent.AreaRegistryUpdated, this.onAreaRegistryUpdate],
[homeAssistant_1.HaEvent.DeviceRegistryUpdated, this.onDeviceRegistryUpdate],
[homeAssistant_1.HaEvent.FloorRegistryUpdated, this.onFloorRegistryUpdate],
[homeAssistant_1.HaEvent.LabelRegistryUpdated, this.onLabelRegistryUpdate],
[homeAssistant_1.HaEvent.EntityRegistryUpdated, this.onEntityRegistryUpdate],
[homeAssistant_1.HaEvent.ServicesUpdated, this.onServicesUpdated],
]);
}
publish(type, data, retain = true) {
globals_1.RED.comms.publish(`homeassistant/${type}/${__classPrivateFieldGet(this, _Comms_serverId, "f")}`, data, retain);
}
onAreaRegistryUpdate(areas) {
this.publish('areas', areas);
}
onDeviceRegistryUpdate(devices) {
const slimDevices = devices.map((device) => {
return {
area_id: device.area_id,
id: device.id,
labels: device.labels,
name: device.name,
name_by_user: device.name_by_user,
};
});
this.publish('devices', slimDevices);
}
onFloorRegistryUpdate(floors) {
this.publish('floors', floors);
}
onLabelRegistryUpdate(labels) {
this.publish('labels', labels);
}
onEntityRegistryUpdate(entities) {
const slimEntities = entities.map((entity) => {
return {
area_id: entity.area_id,
device_id: entity.device_id,
entity_id: entity.entity_id,
id: entity.id,
labels: entity.labels,
name: entity.name,
original_name: entity.original_name,
platform: entity.platform,
};
});
this.publish('entityRegistry', slimEntities);
}
onIntegrationEvent(eventType) {
this.publish('integration', {
event: eventType,
version: __classPrivateFieldGet(this, _Comms_homeAssistant, "f").integrationVersion,
});
}
onServicesUpdated(services) {
this.publish('services', services);
}
onStateChanged(event) {
var _a, _b, _c, _d;
const entity = {
entity_id: event.entity_id,
state: (_a = event.event.new_state) === null || _a === void 0 ? void 0 : _a.state,
attributes: {
device_class: (_b = event.event.new_state) === null || _b === void 0 ? void 0 : _b.attributes.device_class,
friendly_name: (_c = event.event.new_state) === null || _c === void 0 ? void 0 : _c.attributes.friendly_name,
supported_features: (_d = event.event.new_state) === null || _d === void 0 ? void 0 : _d.attributes.supported_features,
},
};
if (!entity)
return;
__classPrivateFieldGet(this, _Comms_stateChangedBatchedUpdates, "f").set(event.entity_id, entity);
__classPrivateFieldGet(this, _Comms_throttledStateChangedPublish, "f").call(this);
}
onStatesLoaded(entities) {
const slimEntities = {};
Object.keys(entities).forEach((entityId) => {
const entity = entities[entityId];
slimEntities[entityId] = {
entity_id: entityId,
state: entity.state,
attributes: {
device_class: entity.attributes.device_class,
friendly_name: entity.attributes.friendly_name,
supported_features: entity.attributes.supported_features,
},
};
});
this.publish('entities', slimEntities);
}
}
_Comms_clientEvents = new WeakMap(), _Comms_homeAssistant = new WeakMap(), _Comms_serverId = new WeakMap(), _Comms_stateChangedBatchedUpdates = new WeakMap(), _Comms_throttledStateChangedPublish = new WeakMap();
exports.default = Comms;