UNPKG

node-red-contrib-home-assistant-websocket

Version:
98 lines (97 loc) 6.62 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 _TimeEntityController_instances, _TimeEntityController_entityConfigNode, _TimeEntityController_onInputModeGet, _TimeEntityController_onInputModeSet, _TimeEntityController_isValidValue, _TimeEntityController_getFormattedValue; 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 TimeEntityController extends InputOutputController_1.default { constructor(props) { var _a; super(props); _TimeEntityController_instances.add(this); _TimeEntityController_entityConfigNode.set(this, void 0); __classPrivateFieldSet(this, _TimeEntityController_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, _TimeEntityController_instances, "m", _TimeEntityController_onInputModeGet).call(this, { done, message, parsedMessage, send }); } else if (this.node.config.mode === const_1.ValueIntegrationMode.Set) { await __classPrivateFieldGet(this, _TimeEntityController_instances, "m", _TimeEntityController_onInputModeSet).call(this, { done, message, parsedMessage, send }); } else { throw new InputError_1.default('ha-text.error.mode_not_supported', 'home-assistant.status.error'); } } } _TimeEntityController_entityConfigNode = new WeakMap(), _TimeEntityController_instances = new WeakSet(), _TimeEntityController_onInputModeGet = // Handles input messages when the node is in "get" mode async function _TimeEntityController_onInputModeGet({ done, message, send }) { var _a, _b, _c; const value = (_c = (_b = (_a = __classPrivateFieldGet(this, _TimeEntityController_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; // inject value so colons are not removed this.status.setSuccess(['__value__', { value }]); await this.setCustomOutputs(this.node.config.outputProperties, message, { config: this.node.config, value, }); send(message); done(); }, _TimeEntityController_onInputModeSet = // Handles input messages when the node is in "set" mode async function _TimeEntityController_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 (__classPrivateFieldGet(this, _TimeEntityController_instances, "m", _TimeEntityController_isValidValue).call(this, value) === false) { throw new InputError_1.default('ha-time-entity.error.invalid_format', 'home-assistant.status.error'); } value = __classPrivateFieldGet(this, _TimeEntityController_instances, "m", _TimeEntityController_getFormattedValue).call(this, value); // get previous value before updating const previousValue = (_e = (_d = (_c = __classPrivateFieldGet(this, _TimeEntityController_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 time nodes (_g = __classPrivateFieldGet(this, _TimeEntityController_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, }); // inject value so colons are not removed this.status.setSuccess(['__value__', { value }]); send(message); done(); }, _TimeEntityController_isValidValue = function _TimeEntityController_isValidValue(text) { const pattern = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/; const regex = new RegExp(pattern); return regex.test(text); return true; }, _TimeEntityController_getFormattedValue = function _TimeEntityController_getFormattedValue(text) { var _a; const [hours, minutes, seconds] = text.split(':'); return `${hours.padStart(2, '0')}:${minutes.padStart(2, '0')}:${(_a = seconds === null || seconds === void 0 ? void 0 : seconds.padStart(2, '0')) !== null && _a !== void 0 ? _a : '00'}`; }; exports.default = TimeEntityController;