node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
90 lines (89 loc) • 3.3 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 = apiNode;
const joi_1 = __importDefault(require("joi"));
const helpers_1 = require("../../common/controllers/helpers");
const InputService_1 = __importDefault(require("../../common/services/InputService"));
const Status_1 = __importDefault(require("../../common/status/Status"));
const const_1 = require("../../const");
const globals_1 = require("../../globals");
const migrate_1 = require("../../helpers/migrate");
const node_1 = require("../../helpers/node");
const homeAssistant_1 = require("../../homeAssistant");
const ApiController_1 = __importDefault(require("./ApiController"));
const const_2 = require("./const");
const inputs = {
protocol: {
messageProp: 'payload.protocol',
configProp: 'protocol',
},
method: {
messageProp: 'payload.method',
configProp: 'method',
},
path: {
messageProp: 'payload.path',
configProp: 'path',
},
data: {
messageProp: 'payload.data',
configProp: 'data',
},
dataType: {
messageProp: 'payload.dataType',
configProp: 'dataType',
},
location: {
messageProp: 'payload.location',
configProp: 'location',
},
locationType: {
messageProp: 'payload.locationType',
configProp: 'locationType',
},
responseType: {
messageProp: 'payload.responseType',
configProp: 'responseType',
},
outputProperties: {
messageProp: 'payload.outputProperties',
configProp: 'outputProperties',
},
};
const inputSchema = joi_1.default.object({
protocol: joi_1.default.string().valid(...Object.values(const_2.ApiProtocol)),
method: joi_1.default.string().valid(...Object.values(const_2.ApiMethod)),
path: joi_1.default.string().allow(''),
dataType: joi_1.default.string().valid(const_1.TypedInputTypes.JSON, const_1.TypedInputTypes.JSONata),
data: joi_1.default.optional(),
location: joi_1.default.string(),
locationType: joi_1.default.string().valid(const_1.TypedInputTypes.Message, const_1.TypedInputTypes.Flow, const_1.TypedInputTypes.Global, const_1.TypedInputTypes.None),
responseType: joi_1.default.string().valid('json', 'text', 'arraybuffer'),
outputProperties: joi_1.default.array().items(),
});
function apiNode(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 status = new Status_1.default({
config: serverConfigNode.config,
node: this,
});
const inputService = new InputService_1.default({
inputs,
nodeConfig: this.config,
schema: inputSchema,
});
const controllerDeps = (0, helpers_1.createControllerDependencies)(this, homeAssistant);
// eslint-disable-next-line no-new
new ApiController_1.default({
inputService,
node: this,
status,
...controllerDeps,
});
}