node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
109 lines (108 loc) • 3.94 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StatusShape = exports.StatusColor = void 0;
const globals_1 = require("../../globals");
const date_1 = require("../../helpers/date");
const homeAssistant_1 = require("../../homeAssistant");
const BaseError_1 = require("../errors/BaseError");
const ClientEvents_1 = __importDefault(require("../events/ClientEvents"));
const helpers_1 = require("./helpers");
var StatusColor;
(function (StatusColor) {
StatusColor["Blue"] = "blue";
StatusColor["Green"] = "green";
StatusColor["Grey"] = "grey";
StatusColor["Red"] = "red";
StatusColor["Yellow"] = "yellow";
})(StatusColor || (exports.StatusColor = StatusColor = {}));
var StatusShape;
(function (StatusShape) {
StatusShape["Dot"] = "dot";
StatusShape["Ring"] = "ring";
})(StatusShape || (exports.StatusShape = StatusShape = {}));
class Status {
constructor(props) {
this.lastStatus = {};
this.config = props.config;
this.exposeAsEntityConfigNode = props.exposeAsEntityConfigNode;
this.node = props.node;
if (this.exposeAsEntityConfigNode) {
const exposeAsConfigEvents = new ClientEvents_1.default({
node: this.node,
emitter: this.exposeAsEntityConfigNode,
});
exposeAsConfigEvents === null || exposeAsConfigEvents === void 0 ? void 0 : exposeAsConfigEvents.addListener(homeAssistant_1.HaEvent.StateChanged, this.onStateChange.bind(this));
}
}
onStateChange() {
this.updateStatus(this.lastStatus);
}
get isExposeAsEnabled() {
var _a, _b;
return (_b = (_a = this.exposeAsEntityConfigNode) === null || _a === void 0 ? void 0 : _a.state.isEnabled()) !== null && _b !== void 0 ? _b : true;
}
get dateString() {
var _a, _b;
const separator = (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.statusSeparator) !== null && _b !== void 0 ? _b : '';
const date = (0, date_1.formatDate)({
options: (0, helpers_1.getStatusOptions)(this.config),
});
return `${separator}${date}`;
}
translateText(data) {
const [key, params] = Array.isArray(data) ? data : [data, undefined];
const message = (0, BaseError_1.isTranslationKey)(key) ? globals_1.RED._(key, params) : key;
return `${message} ${this.dateString}`;
}
updateStatus(status) {
if (this.isExposeAsEnabled === false) {
status = {
fill: StatusColor.Grey,
shape: StatusShape.Dot,
text: 'home-assistant.status.disabled',
};
}
this.node.status(status);
}
set(status = {}) {
if (this.isExposeAsEnabled) {
this.lastStatus = status;
}
this.updateStatus(status);
}
setText(text = '') {
this.set({ text });
}
setError(text = 'home-assistant.status.error') {
this.set({
fill: StatusColor.Red,
shape: StatusShape.Ring,
text: this.translateText(text),
});
}
setFailed(text = 'home-assistant.status.failed') {
this.set({
fill: StatusColor.Red,
shape: StatusShape.Ring,
text: this.translateText(text),
});
}
setSending(text = 'home-assistant.status.sending') {
this.set({
fill: StatusColor.Yellow,
shape: StatusShape.Dot,
text: this.translateText(text),
});
}
setSuccess(text = 'home-assistant.status.success') {
this.set({
fill: StatusColor.Green,
shape: StatusShape.Dot,
text: this.translateText(text),
});
}
}
exports.default = Status;