node-red-contrib-smartnora
Version:
Google Smart Home integration via Smart Nora https://smart-nora.eu/
59 lines (58 loc) • 2.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const nora_firebase_common_1 = require("@andrei-tatar/nora-firebase-common");
const rxjs_1 = require("rxjs");
const util_1 = require("./util");
module.exports = function (RED) {
RED.nodes.registerType('noraf-doorbell', function (config) {
RED.nodes.createNode(this, config);
const noraConfig = RED.nodes.getNode(config.nora);
if (!(noraConfig === null || noraConfig === void 0 ? void 0 : noraConfig.valid)) {
return;
}
(0, util_1.registerNoraDevice)(this, RED, config, {
deviceConfig: {
type: 'action.devices.types.DOORBELL',
traits: ['action.devices.traits.ObjectDetection'],
name: {
name: config.devicename,
},
roomHint: config.roomhint,
willReportState: true,
notificationSupportedByAgent: true,
state: {
online: true,
},
attributes: {},
noraSpecific: {},
},
handleNodeInput: async ({ msg, updateState, device$ }) => {
const { online, named, familiar, unfamiliar, unclassified } = msg.payload;
if (typeof online !== 'undefined') {
await updateState({ online });
}
const objects = { named, familiar, unfamiliar, unclassified };
for (const key of Object.keys(objects)) {
if (objects[key] === void 0) {
delete objects[key];
}
}
if (Object.entries(objects).length) {
const notification = {
ObjectDetection: {
priority: 0,
detectionTimestamp: new Date().getTime(),
objects,
}
};
const result = (0, nora_firebase_common_1.validateIndividual)('object-detection-notification', notification);
if (!result.valid) {
throw new Error(`Invalid notification object ${result.errors}`);
}
const device = await (0, rxjs_1.firstValueFrom)(device$);
await device.sendNotification(notification);
}
},
});
});
};