node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
102 lines (101 loc) • 3.48 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = __importDefault(require("debug"));
const const_1 = require("../const");
const Websocket_1 = require("./Websocket");
const debug = (0, debug_1.default)('home-assistant');
const websocketMethods = [
'callService',
'connect',
'getDevices',
'getDeviceActions',
'getDeviceActionCapabilities',
'getDeviceTriggers',
'getDeviceTriggerCapabilities',
'getServices',
'getStates',
'getTranslations',
'getUser',
'send',
'subscribeMessage',
];
const httpMethods = [
'fireEvent',
'get',
'getHistory',
'post',
'renderTemplate',
];
class HomeAssistant {
constructor({ websocketAPI, httpAPI, eventBus, }) {
// TODO: this can be made private after typescript conversion
this.eventsList = {};
debug('Instantiating HomeAssistant');
this.eventBus = eventBus;
this.http = httpAPI;
this.websocket = websocketAPI;
this.exposeMethods(this.websocket, websocketMethods);
this.exposeMethods(this.http, httpMethods);
}
get isConnected() {
return this.websocket.connectionState === Websocket_1.ClientState.Connected;
}
get isHomeAssistantRunning() {
return this.isConnected && this.websocket.isHomeAssistantRunning;
}
get integrationVersion() {
return this.websocket.integrationVersion;
}
get isIntegrationLoaded() {
return this.integrationVersion !== const_1.NO_VERSION;
}
get connectionState() {
return this.websocket.connectionState;
}
get version() {
var _a, _b;
const client = (_a = this === null || this === void 0 ? void 0 : this.websocket) === null || _a === void 0 ? void 0 : _a.client;
return (_b = client === null || client === void 0 ? void 0 : client.haVersion) !== null && _b !== void 0 ? _b : const_1.NO_VERSION;
}
// TODO: remove after typescript conversion done
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
exposeMethods(cls, methods) {
methods.forEach((method) => {
if (typeof cls[method] === 'function') {
// @ts-ignore - needs to remove in the future
this[method] = cls[method].bind(cls);
}
});
}
getEntities() {
const states = this.websocket.getStates();
const entities = Object.keys(states).sort();
return entities;
}
getTags() {
var _a, _b;
return (_b = (_a = this === null || this === void 0 ? void 0 : this.websocket) === null || _a === void 0 ? void 0 : _a.tags) !== null && _b !== void 0 ? _b : [];
}
subscribeEvents() {
return this.websocket.subscribeEvents(this.eventsList);
}
close() {
var _a;
(_a = this === null || this === void 0 ? void 0 : this.websocket) === null || _a === void 0 ? void 0 : _a.close();
}
addListener(event, handler, options = { once: false }) {
if (options.once === true) {
this.eventBus.once(event, handler);
}
else {
this.eventBus.on(event, handler);
}
}
removeListener(event, handler) {
this.eventBus.removeListener(event, handler);
}
}
exports.default = HomeAssistant;