node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
73 lines (72 loc) • 4.35 kB
JavaScript
"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 _ConnectionLog_hostname, _ConnectionLog_node;
Object.defineProperty(exports, "__esModule", { value: true });
const Events_1 = require("../../common/events/Events");
const homeAssistant_1 = require("../../homeAssistant");
const Websocket_1 = require("../../homeAssistant/Websocket");
class ConnectionLog {
constructor(node, clientEvents) {
_ConnectionLog_hostname.set(this, void 0);
_ConnectionLog_node.set(this, void 0);
this.onHaEventsOpen = () => {
__classPrivateFieldGet(this, _ConnectionLog_node, "f").log(`Connected to ${__classPrivateFieldGet(this, _ConnectionLog_hostname, "f")}`);
};
this.onHaStatesLoaded = () => {
__classPrivateFieldGet(this, _ConnectionLog_node, "f").debug('States Loaded');
};
this.onHaServicesLoaded = () => {
__classPrivateFieldGet(this, _ConnectionLog_node, "f").debug('Services Loaded');
};
this.onHaEventsConnecting = () => {
__classPrivateFieldGet(this, _ConnectionLog_node, "f").log(`Connecting to ${__classPrivateFieldGet(this, _ConnectionLog_hostname, "f")}`);
};
this.onHaEventsClose = () => {
__classPrivateFieldGet(this, _ConnectionLog_node, "f").log(`Connection closed to ${__classPrivateFieldGet(this, _ConnectionLog_hostname, "f")}`);
};
this.onHaEventsRunning = () => {
__classPrivateFieldGet(this, _ConnectionLog_node, "f").debug(`HA State: running`);
};
this.onHaEventsError = (err) => {
__classPrivateFieldGet(this, _ConnectionLog_node, "f").debug(err);
};
this.onIntegrationEvent = (eventType) => {
__classPrivateFieldGet(this, _ConnectionLog_node, "f").debug(`Integration: ${eventType}`);
};
this.onRegistriesLoaded = () => {
__classPrivateFieldGet(this, _ConnectionLog_node, "f").debug('Registries Loaded');
};
__classPrivateFieldSet(this, _ConnectionLog_hostname, node.config.addon
? homeAssistant_1.SUPERVISOR_URL
: node.credentials.host, "f");
__classPrivateFieldSet(this, _ConnectionLog_node, node, "f");
clientEvents.addListeners(this, [
[Websocket_1.ClientEvent.Close, this.onHaEventsClose],
[Websocket_1.ClientEvent.Open, this.onHaEventsOpen],
[Websocket_1.ClientEvent.Connecting, this.onHaEventsConnecting],
[Websocket_1.ClientEvent.Error, this.onHaEventsError],
[Websocket_1.ClientEvent.Running, this.onHaEventsRunning],
[Websocket_1.ClientEvent.StatesLoaded, this.onHaStatesLoaded],
[Websocket_1.ClientEvent.ServicesLoaded, this.onHaServicesLoaded],
['integration', this.onIntegrationEvent],
[Websocket_1.ClientEvent.RegistriesLoaded, this.onRegistriesLoaded],
]);
node.on(Events_1.NodeEvent.Close, this.onClose.bind(this));
}
onClose(_removed, done) {
__classPrivateFieldGet(this, _ConnectionLog_node, "f").log(`Closing connection to ${__classPrivateFieldGet(this, _ConnectionLog_hostname, "f")}`);
done();
}
}
_ConnectionLog_hostname = new WeakMap(), _ConnectionLog_node = new WeakMap();
exports.default = ConnectionLog;