UNPKG

node-red-contrib-home-assistant-websocket

Version:
94 lines (93 loc) 6.05 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; 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 _NumberController_instances, _NumberController_entityConfigNode, _NumberController_onInputModeGet, _NumberController_onInputModeSet, _NumberController_getValidatedValue; Object.defineProperty(exports, "__esModule", { value: true }); const InputOutputController_1 = __importDefault(require("../../common/controllers/InputOutputController")); const InputError_1 = __importDefault(require("../../common/errors/InputError")); const NoConnectionError_1 = __importDefault(require("../../common/errors/NoConnectionError")); const Integration_1 = require("../../common/integration/Integration"); const const_1 = require("../../const"); class NumberController extends InputOutputController_1.default { constructor(props) { var _a; super(props); _NumberController_instances.add(this); _NumberController_entityConfigNode.set(this, void 0); __classPrivateFieldSet(this, _NumberController_entityConfigNode, (_a = this.integration) === null || _a === void 0 ? void 0 : _a.getEntityConfigNode(), "f"); } async onInput({ done, message, parsedMessage, send, }) { if (this.node.config.mode === const_1.ValueIntegrationMode.Get) { __classPrivateFieldGet(this, _NumberController_instances, "m", _NumberController_onInputModeGet).call(this, { done, message, parsedMessage, send }); } else if (this.node.config.mode === const_1.ValueIntegrationMode.Set) { await __classPrivateFieldGet(this, _NumberController_instances, "m", _NumberController_onInputModeSet).call(this, { done, message, parsedMessage, send }); } else { throw new InputError_1.default('ha-text.error.mode_not_supported', 'home-assistant.status.error'); } } } _NumberController_entityConfigNode = new WeakMap(), _NumberController_instances = new WeakSet(), _NumberController_onInputModeGet = async function _NumberController_onInputModeGet({ done, message, send }) { var _a, _b, _c; const value = (_c = (_b = (_a = __classPrivateFieldGet(this, _NumberController_entityConfigNode, "f")) === null || _a === void 0 ? void 0 : _a.state) === null || _b === void 0 ? void 0 : _b.getLastPayload()) === null || _c === void 0 ? void 0 : _c.state; this.status.setSuccess(value); await this.setCustomOutputs(this.node.config.outputProperties, message, { config: this.node.config, value, }); send(message); done(); }, _NumberController_onInputModeSet = async function _NumberController_onInputModeSet({ done, message, parsedMessage, send, }) { var _a, _b, _c, _d, _e, _f, _g; if (!((_a = this.integration) === null || _a === void 0 ? void 0 : _a.isConnected)) { throw new NoConnectionError_1.default(); } if (!((_b = this.integration) === null || _b === void 0 ? void 0 : _b.isIntegrationLoaded)) { throw new InputError_1.default('home-assistant.error.integration_not_loaded', 'home-assistant.status.error'); } let value = await this.typedInputService.getValue(parsedMessage.value.value, parsedMessage.valueType.value, { message, }); if (typeof value !== 'number') { throw new InputError_1.default('ha-number.error.value_not_number', 'home-assistant.status.error'); } value = __classPrivateFieldGet(this, _NumberController_instances, "m", _NumberController_getValidatedValue).call(this, value); // get previous value before updating const previousValue = (_e = (_d = (_c = __classPrivateFieldGet(this, _NumberController_entityConfigNode, "f")) === null || _c === void 0 ? void 0 : _c.state) === null || _d === void 0 ? void 0 : _d.getLastPayload()) === null || _e === void 0 ? void 0 : _e.state; await ((_f = this.integration) === null || _f === void 0 ? void 0 : _f.updateValue(value)); // send value change to all number nodes (_g = __classPrivateFieldGet(this, _NumberController_entityConfigNode, "f")) === null || _g === void 0 ? void 0 : _g.emit(Integration_1.IntegrationEvent.ValueChange, value, previousValue); await this.setCustomOutputs(this.node.config.outputProperties, message, { config: this.node.config, value, previousValue, }); this.status.setSuccess(value.toString()); send(message); done(); }, _NumberController_getValidatedValue = function _NumberController_getValidatedValue(value) { var _a, _b; const min = Number((_a = this.integration) === null || _a === void 0 ? void 0 : _a.getEntityHomeAssistantConfigValue('min_value')); const max = Number((_b = this.integration) === null || _b === void 0 ? void 0 : _b.getEntityHomeAssistantConfigValue('max_value')); if (min && value < min) { value = min; } if (max && value > max) { value = max; } return value; }; exports.default = NumberController;