node-red-contrib-smartnora
Version:
Google Smart Home integration via Smart Nora https://smart-nora.eu/
57 lines (56 loc) • 2.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("./util");
module.exports = function (RED) {
RED.nodes.registerType('noraf-switch', 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: onValue, type: onType } = (0, util_1.convertValueType)(RED, config.onvalue, config.onvalueType, { defaultValue: true });
const { value: offValue, type: offType } = (0, util_1.convertValueType)(RED, config.offvalue, config.offvalueType, { defaultValue: false });
(0, util_1.registerNoraDevice)(this, RED, config, {
deviceConfig: {
type: 'action.devices.types.SWITCH',
traits: ['action.devices.traits.OnOff'],
name: {
name: config.devicename,
},
roomHint: config.roomhint,
willReportState: true,
state: {
on: false,
online: true,
},
attributes: {},
noraSpecific: {
returnOnOffErrorCodeIfStateAlreadySet: !!config.errorifstateunchaged,
asyncCommandExecution: !!config.asyncCmd,
},
},
updateStatus: ({ state, update }) => {
update(`${state.on ? 'on' : 'off'}`);
},
mapStateToOutput: state => {
const value = state.on;
return {
payload: (0, util_1.getValue)(RED, this, value ? onValue : offValue, value ? onType : offType),
};
},
handleNodeInput: async ({ msg, updateState }) => {
const myOnValue = (0, util_1.getValue)(RED, this, onValue, onType);
const myOffValue = (0, util_1.getValue)(RED, this, offValue, offType);
if (RED.util.compareObjects(myOnValue, msg.payload)) {
await updateState({ on: true });
}
else if (RED.util.compareObjects(myOffValue, msg.payload)) {
await updateState({ on: false });
}
else {
await updateState(msg.payload);
}
},
});
});
};