UNPKG

node-red-contrib-home-assistant-websocket

Version:
98 lines (97 loc) 4.77 kB
"use strict"; 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 __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 _BidirectionalIntegration_unsubscribe; Object.defineProperty(exports, "__esModule", { value: true }); const node_1 = require("../../helpers/node"); const HomeAssistantError_1 = require("../errors/HomeAssistantError"); const Events_1 = require("../events/Events"); const Integration_1 = __importStar(require("./Integration")); class BidirectionalIntegration extends Integration_1.default { constructor(props) { super(props); _BidirectionalIntegration_unsubscribe.set(this, void 0); this.node = props.node; this.node.on(Events_1.NodeEvent.Close, this.onNodeClose.bind(this)); } async register() { if (!this.isIntegrationLoaded) { this.node.error(this.notInstalledMessage); this.status.forEach((status) => status.setFailed('home-assistant.status.error')); return; } if (this.isRegistered) return; const payload = this.getDiscoveryPayload(); this.debugToClient('register', payload); try { __classPrivateFieldSet(this, _BidirectionalIntegration_unsubscribe, await this.homeAssistant.websocket.subscribeMessage(this.onReceivedMessage.bind(this), payload, { resubscribe: false }), "f"); } catch (err) { this.status.forEach((status) => status.setFailed('home-assistant.status.error_registering')); const message = err instanceof Error || (0, HomeAssistantError_1.isHomeAssistantApiError)(err) ? err.message : err; this.node.error(`Error registering entity. Error Message: ${JSON.stringify(message)}`); return; } this.status.forEach((status) => status === null || status === void 0 ? void 0 : status.setSuccess('home-assistant.status.registered')); this.registered = true; } async unregister() { var _a; await ((_a = __classPrivateFieldGet(this, _BidirectionalIntegration_unsubscribe, "f")) === null || _a === void 0 ? void 0 : _a.call(this)); } async onNodeClose(removed, done) { this.registered = false; if (this.isIntegrationLoaded) { try { await this.unregister(); } catch (err) { done(err); } } done(); } onReceivedMessage(message) { this.node.emit(Integration_1.IntegrationEvent.Trigger, message.data); } debugToClient(topic, message) { (0, node_1.debugToClient)(this.node, message, topic); } } _BidirectionalIntegration_unsubscribe = new WeakMap(); exports.default = BidirectionalIntegration;