UNPKG

node-red-contrib-home-assistant-websocket

Version:
89 lines (88 loc) 5.82 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 _SelectController_instances, _SelectController_entityConfigNode, _SelectController_onInputModeGet, _SelectController_onInputModeSet, _SelectController_isValidValue; 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 SelectController extends InputOutputController_1.default { constructor(props) { var _a; super(props); _SelectController_instances.add(this); _SelectController_entityConfigNode.set(this, void 0); __classPrivateFieldSet(this, _SelectController_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, _SelectController_instances, "m", _SelectController_onInputModeGet).call(this, { done, message, parsedMessage, send }); } else if (this.node.config.mode === const_1.ValueIntegrationMode.Set) { await __classPrivateFieldGet(this, _SelectController_instances, "m", _SelectController_onInputModeSet).call(this, { done, message, parsedMessage, send }); } else { throw new InputError_1.default('ha-text.error.mode_not_supported', 'home-assistant.status.error'); } } } _SelectController_entityConfigNode = new WeakMap(), _SelectController_instances = new WeakSet(), _SelectController_onInputModeGet = async function _SelectController_onInputModeGet({ done, message, send }) { var _a, _b, _c; const value = (_c = (_b = (_a = __classPrivateFieldGet(this, _SelectController_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(); }, _SelectController_onInputModeSet = async function _SelectController_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'); } const value = await this.typedInputService.getValue(parsedMessage.value.value, parsedMessage.valueType.value, { message, }); if (__classPrivateFieldGet(this, _SelectController_instances, "m", _SelectController_isValidValue).call(this, value) === false) { throw new InputError_1.default('ha-select.error.invalid_value', 'home-assistant.status.error'); } // get previous value before updating const previousValue = (_e = (_d = (_c = __classPrivateFieldGet(this, _SelectController_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, _SelectController_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); send(message); done(); }, _SelectController_isValidValue = function _SelectController_isValidValue(option) { var _a; const options = (_a = this.integration) === null || _a === void 0 ? void 0 : _a.getEntityHomeAssistantConfigValue('options'); if (!options || !Array.isArray(options)) { return false; } return options.includes(option); }; exports.default = SelectController;