node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
72 lines (71 loc) • 3.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 = pollStateNode;
const helpers_1 = require("../../common/controllers/helpers");
const ConfigError_1 = __importDefault(require("../../common/errors/ConfigError"));
const ClientEvents_1 = __importDefault(require("../../common/events/ClientEvents"));
const ComparatorService_1 = __importDefault(require("../../common/services/ComparatorService"));
const EventStatus_1 = __importDefault(require("../../common/status/EventStatus"));
const Status_1 = require("../../common/status/Status");
const TransformState_1 = __importDefault(require("../../common/TransformState"));
const globals_1 = require("../../globals");
const migrate_1 = require("../../helpers/migrate");
const node_1 = require("../../helpers/node");
const homeAssistant_1 = require("../../homeAssistant");
const events_1 = require("./events");
const PollStateController_1 = __importDefault(require("./PollStateController"));
function pollStateNode(config) {
var _a;
globals_1.RED.nodes.createNode(this, config);
this.config = (0, migrate_1.migrate)(config);
if (!((_a = this.config) === null || _a === void 0 ? void 0 : _a.entityId)) {
const error = new ConfigError_1.default('poll-state.error.entity_id_required');
this.status({
fill: Status_1.StatusColor.Red,
shape: Status_1.StatusShape.Ring,
text: error.statusMessage,
});
throw error;
}
const serverConfigNode = (0, node_1.getServerConfigNode)(this.config.server);
const homeAssistant = (0, homeAssistant_1.getHomeAssistant)(serverConfigNode);
const exposeAsConfigNode = (0, node_1.getExposeAsConfigNode)(this.config.exposeAsEntityConfig);
const clientEvents = new ClientEvents_1.default({
node: this,
emitter: homeAssistant.eventBus,
});
const status = new EventStatus_1.default({
clientEvents,
config: serverConfigNode.config,
exposeAsEntityConfigNode: exposeAsConfigNode,
node: this,
});
clientEvents.setStatus(status);
const controllerDeps = (0, helpers_1.createControllerDependencies)(this, homeAssistant);
const transformState = new TransformState_1.default(serverConfigNode.config.ha_boolean);
const comparatorService = new ComparatorService_1.default({
nodeRedContextService: controllerDeps.nodeRedContextService,
homeAssistant,
jsonataService: controllerDeps.jsonataService,
transformState,
});
const controller = new PollStateController_1.default({
comparatorService,
node: this,
status,
transformState,
...controllerDeps,
});
controller.setExposeAsConfigNode(exposeAsConfigNode);
(0, events_1.startListeners)({
clientEvents,
config: this.config,
controller,
homeAssistant,
node: this,
status,
});
}