node-red-contrib-smartnora
Version:
Google Smart Home integration via Smart Nora https://smart-nora.eu/
85 lines (84 loc) • 4.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("./util");
module.exports = function (RED) {
RED.nodes.registerType('noraf-lock', 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;
}
const { value: lockValue, type: lockType } = (0, util_1.convertValueType)(RED, config.lockValue, config.lockValueType, { defaultValue: true });
const { value: unlockValue, type: unlockType } = (0, util_1.convertValueType)(RED, config.unlockValue, config.unlockValueType, { defaultValue: false });
const { value: jammedValue, type: jammedType } = (0, util_1.convertValueType)(RED, config.jammedValue, config.jammedValueType, { defaultValue: true });
const { value: unjammedValue, type: unjammedType } = (0, util_1.convertValueType)(RED, config.unjammedValue, config.unjammedValueType, { defaultValue: false });
(0, util_1.registerNoraDevice)(this, RED, config, {
deviceConfig: {
type: 'action.devices.types.LOCK',
traits: ['action.devices.traits.LockUnlock'],
name: {
name: config.devicename,
},
roomHint: config.roomhint,
willReportState: true,
attributes: {},
state: {
online: true,
isLocked: false,
isJammed: false,
},
noraSpecific: {
returnLockUnlockErrorCodeIfStateAlreadySet: !!config.errorifstateunchaged,
},
},
updateStatus: ({ state, update }) => {
if (state.isJammed) {
update('jammed');
}
else {
update(`${state.isLocked ? 'locked' : 'unlocked'}`);
}
},
mapStateToOutput: state => {
const lvalue = state.isLocked;
if (!state.isJammed) {
return {
payload: (0, util_1.getValue)(RED, this, lvalue ? lockValue : unlockValue, lvalue ? lockType : unlockType),
};
}
else {
this.error('Lock is jammed');
}
},
handleNodeInput: async ({ msg, updateState }) => {
var _a;
const myLockValue = (0, util_1.getValue)(RED, this, lockValue, lockType);
const myUnlockValue = (0, util_1.getValue)(RED, this, unlockValue, unlockType);
if (((_a = msg.topic) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'jammed') {
const myJammedValue = (0, util_1.getValue)(RED, this, jammedValue, jammedType);
const myUnjammedValue = (0, util_1.getValue)(RED, this, unjammedValue, unjammedType);
if (RED.util.compareObjects(myJammedValue, msg.payload)) {
await updateState({ isJammed: true });
}
else if (RED.util.compareObjects(myUnjammedValue, msg.payload)) {
await updateState({ isJammed: false });
}
else {
await updateState(msg.payload);
}
}
else {
if (RED.util.compareObjects(myLockValue, msg.payload)) {
await updateState({ isLocked: true });
}
else if (RED.util.compareObjects(myUnlockValue, msg.payload)) {
await updateState({ isLocked: false });
}
else {
await updateState(msg.payload);
}
}
},
});
});
};