UNPKG

node-red-contrib-home-assistant-websocket

Version:
234 lines (233 loc) 10 kB
"use strict"; 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 __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 _Status_lastStatus, _EventsStatus_connectionState, _EventsStatus_eventListeners; Object.defineProperty(exports, "__esModule", { value: true }); exports.SwitchEntityStatus = exports.EventsStatus = exports.Status = exports.STATUS_SHAPE_RING = exports.STATUS_SHAPE_DOT = exports.STATUS_COLOR_YELLOW = exports.STATUS_COLOR_RED = exports.STATUS_COLOR_GREY = exports.STATUS_COLOR_GREEN = exports.STATUS_COLOR_BLUE = void 0; const const_1 = require("../const"); const globals_1 = require("../globals"); const date_1 = require("./date"); exports.STATUS_COLOR_BLUE = 'blue'; exports.STATUS_COLOR_GREEN = 'green'; exports.STATUS_COLOR_GREY = 'grey'; exports.STATUS_COLOR_RED = 'red'; exports.STATUS_COLOR_YELLOW = 'yellow'; exports.STATUS_SHAPE_DOT = 'dot'; exports.STATUS_SHAPE_RING = 'ring'; class Status { constructor(node) { var _a, _b; this.node = node; this.isNodeDisabled = false; _Status_lastStatus.set(this, {}); const serverId = (_b = (_a = this.node) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.server; const server = globals_1.RED.nodes.getNode(serverId); this.serverConfig = server === null || server === void 0 ? void 0 : server.config; } // eslint-disable-next-line no-empty-pattern, @typescript-eslint/no-empty-function init({} = {}) { } setNodeState(value) { if (this.isNodeDisabled === value) { this.isNodeDisabled = !value; this.updateStatus(__classPrivateFieldGet(this, _Status_lastStatus, "f")); } } set(status = {}) { if (this.isNodeDisabled === false) { __classPrivateFieldSet(this, _Status_lastStatus, status, "f"); } this.updateStatus(status); } setText(text = '') { this.set({ text }); } setSuccess(text = 'Success') { this.set({ fill: exports.STATUS_COLOR_GREEN, shape: exports.STATUS_SHAPE_DOT, text: this.appendDateString(text), }); } setSending(text = 'Sending') { this.set({ fill: exports.STATUS_COLOR_YELLOW, shape: exports.STATUS_SHAPE_DOT, text: this.appendDateString(text), }); } setFailed(text = 'Failed') { this.set({ fill: exports.STATUS_COLOR_RED, shape: exports.STATUS_SHAPE_RING, text: this.appendDateString(text), }); } updateStatus(status) { if (this.isNodeDisabled) { status = { fill: exports.STATUS_COLOR_GREY, shape: exports.STATUS_SHAPE_DOT, text: globals_1.RED._('home-assistant.status.disabled'), }; } this.node.status(status); } appendDateString(text) { var _a, _b; const separator = (_b = (_a = this.serverConfig) === null || _a === void 0 ? void 0 : _a.statusSeparator) !== null && _b !== void 0 ? _b : ''; const dateString = (0, date_1.formatDate)({ options: this.statusOptions(), }); return `${text} ${separator}${dateString}`; } statusOptions() { var _a, _b, _c; const config = this.serverConfig; const options = { year: (config === null || config === void 0 ? void 0 : config.statusYear) === 'hidden' ? undefined : config === null || config === void 0 ? void 0 : config.statusYear, month: (config === null || config === void 0 ? void 0 : config.statusMonth) === 'hidden' ? undefined : ((_a = config === null || config === void 0 ? void 0 : config.statusMonth) !== null && _a !== void 0 ? _a : 'short'), day: (config === null || config === void 0 ? void 0 : config.statusDay) === 'hidden' ? undefined : ((_b = config === null || config === void 0 ? void 0 : config.statusDay) !== null && _b !== void 0 ? _b : 'numeric'), hourCycle: (config === null || config === void 0 ? void 0 : config.statusHourCycle) === 'default' ? undefined : ((_c = config === null || config === void 0 ? void 0 : config.statusHourCycle) !== null && _c !== void 0 ? _c : 'h23'), hour: 'numeric', minute: 'numeric', }; switch (config === null || config === void 0 ? void 0 : config.statusTimeFormat) { case 'h:m:s': options.second = 'numeric'; break; case 'h:m:s.ms': options.second = 'numeric'; options.fractionalSecondDigits = 3; break; } return options; } } exports.Status = Status; _Status_lastStatus = new WeakMap(); class EventsStatus extends Status { constructor() { super(...arguments); _EventsStatus_connectionState.set(this, const_1.STATE_DISCONNECTED); _EventsStatus_eventListeners.set(this, []); } init({ nodeState, homeAssistant, }) { if (nodeState !== undefined) { this.isNodeDisabled = !nodeState; } if (homeAssistant) { this.enableConnectionStatus(homeAssistant); } } enableConnectionStatus(homeAssistant) { // Setup event listeners const events = { 'ha_client:close': this.onClientClose, 'ha_client:connecting': this.onClientConnecting, 'ha_client:error': this.onClientError, 'ha_client:open': this.onClientOpen, 'ha_client:running': this.onClientRunning, }; Object.entries(events).forEach(([event, callback]) => { __classPrivateFieldGet(this, _EventsStatus_eventListeners, "f").push(() => homeAssistant.removeListener(event, callback)); homeAssistant.addListener(event, callback.bind(this)); }); } onClientClose() { __classPrivateFieldSet(this, _EventsStatus_connectionState, const_1.STATE_DISCONNECTED, "f"); this.updateConnectionStatus(); } onClientConnecting() { __classPrivateFieldSet(this, _EventsStatus_connectionState, const_1.STATE_CONNECTING, "f"); this.updateConnectionStatus(); } onClientError() { __classPrivateFieldSet(this, _EventsStatus_connectionState, const_1.STATE_ERROR, "f"); this.updateConnectionStatus(); } onClientOpen() { __classPrivateFieldSet(this, _EventsStatus_connectionState, const_1.STATE_CONNECTED, "f"); this.updateConnectionStatus(); } onClientRunning() { __classPrivateFieldSet(this, _EventsStatus_connectionState, const_1.STATE_RUNNING, "f"); this.updateConnectionStatus(); } updateConnectionStatus() { const status = this.getConnectionStatus(); this.updateStatus(status); } getConnectionStatus() { const status = { fill: exports.STATUS_COLOR_RED, shape: exports.STATUS_SHAPE_RING, text: 'home-assistant.status.disconnected', }; switch (__classPrivateFieldGet(this, _EventsStatus_connectionState, "f")) { case const_1.STATE_CONNECTED: status.fill = exports.STATUS_COLOR_GREEN; status.text = 'home-assistant.status.connected'; break; case const_1.STATE_CONNECTING: status.fill = exports.STATUS_COLOR_YELLOW; status.text = 'home-assistant.status.connecting'; break; case const_1.STATE_ERROR: status.text = 'home-assistant.status.error'; break; case const_1.STATE_RUNNING: status.fill = exports.STATUS_COLOR_GREEN; status.text = 'home-assistant.status.running'; break; } if (status.text) { status.text = globals_1.RED._(status.text); } return status; } destroy() { __classPrivateFieldGet(this, _EventsStatus_eventListeners, "f").forEach((callback) => callback()); } } exports.EventsStatus = EventsStatus; _EventsStatus_connectionState = new WeakMap(), _EventsStatus_eventListeners = new WeakMap(); class SwitchEntityStatus extends Status { set({ fill = exports.STATUS_COLOR_YELLOW, shape = exports.STATUS_SHAPE_DOT, text = '', } = {}) { const status = { fill, shape, text, }; super.set(status); } setNodeState(value) { this.isNodeDisabled = !value; const status = { fill: exports.STATUS_COLOR_YELLOW, shape: value ? exports.STATUS_SHAPE_DOT : exports.STATUS_SHAPE_RING, text: this.appendDateString(value ? 'on' : 'off'), }; this.updateStatus(status); } updateStatus(status) { this.node.status(status); } } exports.SwitchEntityStatus = SwitchEntityStatus;