node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
117 lines (116 loc) • 4.61 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = issueCheck;
exports.isActionNodeProperties = isActionNodeProperties;
const IssueService_1 = require("../../common/services/IssueService");
const check_1 = require("../../common/services/IssueService/check");
const utils_1 = require("../../common/services/IssueService/utils");
const const_1 = require("../../const");
const globals_1 = require("../../globals");
// TODO: Fix - Why is IssueType throwing an access undefined error?
const targets = [
{
idsProperty: 'entityId',
// type: IssueType.EntityId,
type: 'stateId',
issueMessage: 'target_entity_not_found',
issueProperty: 'entity_id',
},
{
idsProperty: 'deviceId',
// type: IssueType.DeviceId,
type: 'deviceId',
issueMessage: 'target_device_not_found',
issueProperty: 'device_id',
},
{
idsProperty: 'areaId',
// type: IssueType.AreaId,
type: 'areaId',
issueMessage: 'target_area_not_found',
issueProperty: 'area_id',
},
{
idsProperty: 'floorId',
// type: IssueType.FloorId,
type: 'floorId',
issueMessage: 'target_floor_not_found',
issueProperty: 'floor_id',
},
{
idsProperty: 'labelId',
// type: IssueType.LabelId,
type: 'labelId',
issueMessage: 'target_label_not_found',
issueProperty: 'label_id',
},
];
function issueCheck(config) {
var _a, _b, _c, _d, _e;
const issues = [];
// Check if the action is valid
if (config.action && !(0, utils_1.isDynamicValue)(config.action)) {
const ha = (0, utils_1.getHomeAssistant)(config);
const services = ha === null || ha === void 0 ? void 0 : ha.websocket.getServices();
const [domain, service] = config.action
.toLocaleLowerCase()
.split('.')
.map((part) => part.trim());
if (!((_a = services === null || services === void 0 ? void 0 : services[domain]) === null || _a === void 0 ? void 0 : _a[service])) {
const message = globals_1.RED._('home-assistant.service.issue.invalid_action', {
action: config.action,
});
issues.push({
type: IssueService_1.IssueType.InvalidAction,
message,
identity: config.action,
});
}
else {
// TODO: Remove in version 1.0 - Should no longer be needed
// check if target.entity_id should be in data.entity_id
if (((_d = (_c = (_b = services[domain]) === null || _b === void 0 ? void 0 : _b[service]) === null || _c === void 0 ? void 0 : _c.fields) === null || _d === void 0 ? void 0 : _d.entity_id) !== undefined) {
let data = {};
try {
data = JSON.parse(config.data || '{}');
if (((_e = config.entityId) === null || _e === void 0 ? void 0 : _e.length) && !data.entity_id) {
const ids = config.entityId.join(', ');
issues.push({
type: IssueService_1.IssueType.EntityId,
message: globals_1.RED._('home-assistant.service.issue.entity_id_target_data', {
entity_id: ids,
}),
identity: ids,
});
}
}
catch (e) {
// if data is not valid JSON we can't check if entity_id is in data so we skip this check
}
}
}
}
// Check if the targets are valid
for (const target of targets) {
const invalidIds = (0, check_1.getInvalidIds)(target.type, config, config[target.idsProperty]);
for (const id of invalidIds) {
// Skip the 'all' entity id if the target is entityId
// https://www.home-assistant.io/docs/scripts/perform-actions/#the-basics
if (id === 'all' && target.idsProperty === 'entityId') {
continue;
}
const message = globals_1.RED._(`home-assistant.service.issue.${target.issueMessage}`, {
[target.issueProperty]: id,
});
issues.push({
type: target.type,
message,
identity: id,
});
}
}
return issues;
}
function isActionNodeProperties(node) {
return node.type === const_1.NodeType.Action;
}
;