node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
88 lines (87 loc) • 4.08 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = neviceNode;
const helpers_1 = require("../../common/controllers/helpers");
const ConfigError_1 = __importDefault(require("../../common/errors/ConfigError"));
const ClientEvents_1 = __importDefault(require("../../common/events/ClientEvents"));
const Events_1 = __importDefault(require("../../common/events/Events"));
const Integration_1 = require("../../common/integration/Integration");
const InputService_1 = __importDefault(require("../../common/services/InputService"));
const State_1 = __importDefault(require("../../common/State"));
const EventStatus_1 = __importDefault(require("../../common/status/EventStatus"));
const Status_1 = __importDefault(require("../../common/status/Status"));
const globals_1 = require("../../globals");
const migrate_1 = require("../../helpers/migrate");
const node_1 = require("../../helpers/node");
const homeAssistant_1 = require("../../homeAssistant");
const const_1 = require("./const");
const DeviceActionController_1 = __importDefault(require("./DeviceActionController"));
const DeviceIntegration_1 = __importDefault(require("./DeviceIntegration"));
const DeviceTriggerController_1 = __importDefault(require("./DeviceTriggerController"));
function neviceNode(config) {
globals_1.RED.nodes.createNode(this, config);
this.config = (0, migrate_1.migrate)(config);
const serverConfigNode = (0, node_1.getServerConfigNode)(this.config.server);
const homeAssistant = (0, homeAssistant_1.getHomeAssistant)(serverConfigNode);
const controllerDeps = (0, helpers_1.createControllerDependencies)(this, homeAssistant);
let controller;
let status;
switch (this.config.deviceType) {
case const_1.DeviceType.Action: {
status = new Status_1.default({
config: serverConfigNode.config,
node: this,
});
const inputService = new InputService_1.default({
nodeConfig: this.config,
});
controller = new DeviceActionController_1.default({
inputService,
node: this,
status,
...controllerDeps,
});
break;
}
case const_1.DeviceType.Trigger: {
const clientEvents = new ClientEvents_1.default({
node: this,
emitter: homeAssistant.eventBus,
});
const exposeAsConfigNode = (0, node_1.getExposeAsConfigNode)(this.config.exposeAsEntityConfig);
status = new EventStatus_1.default({
clientEvents,
config: serverConfigNode.config,
exposeAsEntityConfigNode: exposeAsConfigNode,
node: this,
});
const nodeEvents = new Events_1.default({ node: this, emitter: this });
const integration = new DeviceIntegration_1.default({
node: this,
clientEvents,
homeAssistant,
state: new State_1.default(this),
});
controller = new DeviceTriggerController_1.default({
node: this,
status,
...controllerDeps,
});
nodeEvents.addListener(Integration_1.IntegrationEvent.Trigger, controller.onTrigger.bind(controller));
clientEvents.setStatus(status);
exposeAsConfigNode === null || exposeAsConfigNode === void 0 ? void 0 : exposeAsConfigNode.integration.setStatus(status);
integration.setStatus(status);
controller.setExposeAsConfigNode(exposeAsConfigNode);
integration.init();
break;
}
default:
throw new ConfigError_1.default([
'ha-deivce.error.invalid_device_type',
{ device_type: this.config.deviceType },
]);
}
}