node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
58 lines (57 loc) • 3.17 kB
JavaScript
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _TagController_instances, _TagController_isValidTag, _TagController_isValidDevice, _TagController_getTagName;
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const ExposeAsMixin_1 = __importDefault(require("../../common/controllers/ExposeAsMixin"));
const OutputController_1 = __importDefault(require("../../common/controllers/OutputController"));
const const_1 = require("../../const");
const ExposeAsController = (0, ExposeAsMixin_1.default)((OutputController_1.default));
class TagController extends ExposeAsController {
constructor() {
super(...arguments);
_TagController_instances.add(this);
}
async onTagScanned(evt) {
if (!this.isEnabled)
return;
const { event } = (0, lodash_1.cloneDeep)(evt);
const { device_id: deviceId, tag_id: tagId } = event;
if (!__classPrivateFieldGet(this, _TagController_instances, "m", _TagController_isValidTag).call(this, tagId) || !__classPrivateFieldGet(this, _TagController_instances, "m", _TagController_isValidDevice).call(this, deviceId))
return;
const tagName = __classPrivateFieldGet(this, _TagController_instances, "m", _TagController_getTagName).call(this, tagId);
const eventData = {
tag_name: tagName,
user_id: evt.context.user_id,
...event,
};
const msg = {};
await this.setCustomOutputs(this.node.config.outputProperties, msg, {
config: this.node.config,
eventData,
triggerId: tagId,
});
this.status.setSuccess(`${tagName || tagId} scanned`);
this.node.send(msg);
}
}
_TagController_instances = new WeakSet(), _TagController_isValidTag = function _TagController_isValidTag(tag) {
return this.node.config.tags.some((t) => {
return t === const_1.TAGS_ALL || t === tag;
});
}, _TagController_isValidDevice = function _TagController_isValidDevice(deviceId) {
var _a;
const devices = this.node.config.devices;
return (_a = ((devices === null || devices === void 0 ? void 0 : devices.length) === 0 || (devices === null || devices === void 0 ? void 0 : devices.includes(deviceId)))) !== null && _a !== void 0 ? _a : false;
}, _TagController_getTagName = function _TagController_getTagName(tagId) {
var _a;
return (_a = this.homeAssistant.getTags().find((tag) => tag.tag_id === tagId)) === null || _a === void 0 ? void 0 : _a.name;
};
exports.default = TagController;
;