UNPKG

node-red-contrib-smartnora

Version:

Google Smart Home integration via Smart Nora https://smart-nora.eu/

73 lines (72 loc) 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("./util"); const SHORT_UNIT = new Map([ ['SECONDS', 'sec'], ['MILES', 'mi'], ['KILOMETERS', 'km'], ['PERCENTAGE', '%'], ['KILOWATT_HOURS', 'kWh'], ]); module.exports = function (RED) { RED.nodes.registerType('noraf-charger', 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.CHARGER', traits: ['action.devices.traits.EnergyStorage'], name: { name: config.devicename, }, roomHint: config.roomhint, willReportState: true, state: { online: true, descriptiveCapacityRemaining: 'MEDIUM', }, attributes: { queryOnlyEnergyStorage: !!config.queryOnlyEnergyStorage, energyStorageDistanceUnitForUX: config.energyStorageDistanceUnitForUX, isRechargeable: !!config.isRechargeable, }, noraSpecific: { asyncCommandExecution: !!config.asyncCmd, }, }, updateStatus: ({ state, update }) => { var _a, _b; const states = []; if ((_a = state.capacityRemaining) === null || _a === void 0 ? void 0 : _a.length) { states.push(`R:${humanReadable(state.capacityRemaining[0])}`); } else { states.push(state.descriptiveCapacityRemaining); } if ((_b = state.capacityUntilFull) === null || _b === void 0 ? void 0 : _b.length) { states.push(`F:${humanReadable(state.capacityUntilFull[0])}`); } if (state.isCharging) { states.push('charging'); } if (state.isPluggedIn) { states.push('plugged-in'); } update(states.join(',')); }, mapStateToOutput: state => ({ payload: Object.assign({}, state), }), handleNodeInput: async ({ msg, updateState }) => { await updateState(msg.payload); }, }); }); }; function humanReadable(cap) { const shortUnit = SHORT_UNIT.get(cap.unit); return `${cap.rawValue}${shortUnit}`; }