node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
121 lines (120 loc) • 5.26 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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 _Integration_instances, _Integration_onHaClose, _Integration_onHaIntegration;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageType = exports.IntegrationEvent = exports.IntegrationState = void 0;
const Websocket_1 = require("../../homeAssistant/Websocket");
const HomeAssistantError_1 = __importStar(require("../errors/HomeAssistantError"));
var IntegrationState;
(function (IntegrationState) {
IntegrationState["Loaded"] = "loaded";
IntegrationState["NotLoaded"] = "notloaded";
IntegrationState["Unloaded"] = "unloaded";
})(IntegrationState || (exports.IntegrationState = IntegrationState = {}));
var IntegrationEvent;
(function (IntegrationEvent) {
IntegrationEvent["Trigger"] = "trigger";
IntegrationEvent["ValueChange"] = "itegration_value_change";
})(IntegrationEvent || (exports.IntegrationEvent = IntegrationEvent = {}));
var MessageType;
(function (MessageType) {
MessageType["DeviceAction"] = "nodered/device/action";
MessageType["DeviceTrigger"] = "nodered/device/trigger";
MessageType["Discovery"] = "nodered/discovery";
MessageType["Entity"] = "nodered/entity";
MessageType["RemoveDevice"] = "nodered/device/remove";
MessageType["SentenceTrigger"] = "nodered/sentence";
MessageType["SentenceResponse"] = "nodered/sentence_response";
MessageType["UpdateConfig"] = "nodered/entity/update_config";
MessageType["Webhook"] = "nodered/webhook";
})(MessageType || (exports.MessageType = MessageType = {}));
class Integration {
constructor({ clientEvents, homeAssistant, state, }) {
_Integration_instances.add(this);
this.notInstalledMessage = 'Node-RED custom integration needs to be installed in Home Assistant for this node to function correctly.';
this.registered = false;
this.status = [];
this.clientEvents = clientEvents;
this.homeAssistant = homeAssistant;
this.state = state;
}
async init() {
this.clientEvents.addListeners(this, [
[Websocket_1.ClientEvent.Close, __classPrivateFieldGet(this, _Integration_instances, "m", _Integration_onHaClose)],
[Websocket_1.ClientEvent.Integration, __classPrivateFieldGet(this, _Integration_instances, "m", _Integration_onHaIntegration)],
]);
if (this.isIntegrationLoaded) {
await this.register();
}
}
get isConnected() {
return this.homeAssistant.isConnected;
}
get isIntegrationLoaded() {
return this.homeAssistant.isIntegrationLoaded;
}
get isRegistered() {
return this.registered;
}
setStatus(status) {
this.status.push(status);
}
async sendUpdateConfig(serverId, nodeId, config) {
const payload = {
type: MessageType.UpdateConfig,
server_id: serverId,
node_id: nodeId,
config,
};
try {
await this.homeAssistant.websocket.send(payload);
}
catch (err) {
if ((0, HomeAssistantError_1.isHomeAssistantApiError)(err)) {
throw new HomeAssistantError_1.default(err, 'home-assistant.error.error');
}
throw err;
}
}
}
_Integration_instances = new WeakSet(), _Integration_onHaClose = function _Integration_onHaClose() {
this.registered = false;
}, _Integration_onHaIntegration = async function _Integration_onHaIntegration(type) {
switch (type) {
case IntegrationState.Loaded:
await this.register();
break;
case IntegrationState.Unloaded:
case IntegrationState.NotLoaded:
this.registered = false;
break;
}
};
exports.default = Integration;
;