UNPKG

node-red-contrib-home-assistant-websocket

Version:
83 lines (82 loc) 7.3 kB
"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 _EditorContext_instances, _EditorContext_clientEvents, _EditorContext_node, _EditorContext_serverName_get, _EditorContext_resetContext, _EditorContext_startListeners, _EditorContext_setOnContext, _EditorContext_onHaOpen, _EditorContext_onHaStateChanged, _EditorContext_onHaStatesLoaded, _EditorContext_onHaServicesUpdated, _EditorContext_onHaConnecting, _EditorContext_onHaClose, _EditorContext_onHaRunning, _EditorContext_onHaError; Object.defineProperty(exports, "__esModule", { value: true }); const const_1 = require("../../const"); const utils_1 = require("../../helpers/utils"); const homeAssistant_1 = require("../../homeAssistant"); const Websocket_1 = require("../../homeAssistant/Websocket"); const NAMESPACE = 'homeassistant'; class EditorContext { constructor(node, clientEvents) { _EditorContext_instances.add(this); _EditorContext_clientEvents.set(this, void 0); _EditorContext_node.set(this, void 0); __classPrivateFieldSet(this, _EditorContext_clientEvents, clientEvents, "f"); __classPrivateFieldSet(this, _EditorContext_node, node, "f"); const globalContext = __classPrivateFieldGet(this, _EditorContext_node, "f").context().global.get(NAMESPACE); if (!(globalContext === null || globalContext === void 0 ? void 0 : globalContext[__classPrivateFieldGet(this, _EditorContext_instances, "a", _EditorContext_serverName_get)])) { if (!globalContext) { __classPrivateFieldGet(this, _EditorContext_node, "f").context().global.set(NAMESPACE, {}); } __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_resetContext).call(this); } __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_startListeners).call(this); } } _EditorContext_clientEvents = new WeakMap(), _EditorContext_node = new WeakMap(), _EditorContext_instances = new WeakSet(), _EditorContext_serverName_get = function _EditorContext_serverName_get() { return (0, utils_1.toCamelCase)(__classPrivateFieldGet(this, _EditorContext_node, "f").config.name); }, _EditorContext_resetContext = function _EditorContext_resetContext() { __classPrivateFieldGet(this, _EditorContext_node, "f").context().global.set(`${NAMESPACE}.${__classPrivateFieldGet(this, _EditorContext_instances, "a", _EditorContext_serverName_get)}`, { states: {}, services: {}, isConnected: false, isRunning: false, }); }, _EditorContext_startListeners = function _EditorContext_startListeners() { // Setup event listeners __classPrivateFieldGet(this, _EditorContext_clientEvents, "f").addListeners(this, [ [Websocket_1.ClientEvent.Close, __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_onHaClose)], [Websocket_1.ClientEvent.Open, __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_onHaOpen)], [Websocket_1.ClientEvent.Connecting, __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_onHaConnecting)], [Websocket_1.ClientEvent.Error, __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_onHaError)], [Websocket_1.ClientEvent.Running, __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_onHaRunning)], [Websocket_1.ClientEvent.StatesLoaded, __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_onHaStatesLoaded)], [const_1.HA_EVENT_SERVICES_UPDATED, __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_onHaServicesUpdated)], [`ha_events:${homeAssistant_1.HaEvent.StateChanged}`, __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_onHaStateChanged)], ]); }, _EditorContext_setOnContext = function _EditorContext_setOnContext(key, value) { const serverContext = `${NAMESPACE}.${__classPrivateFieldGet(this, _EditorContext_instances, "a", _EditorContext_serverName_get)}.${key}`; __classPrivateFieldGet(this, _EditorContext_node, "f").context().global.set(serverContext, value); }, _EditorContext_onHaOpen = function _EditorContext_onHaOpen() { __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_setOnContext).call(this, 'isConnected', true); }, _EditorContext_onHaStateChanged = function _EditorContext_onHaStateChanged(data) { const entity = data.event; __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_setOnContext).call(this, `states["${entity.entity_id}"]`, entity.new_state); }, _EditorContext_onHaStatesLoaded = function _EditorContext_onHaStatesLoaded(states) { __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_setOnContext).call(this, 'states', states); }, _EditorContext_onHaServicesUpdated = function _EditorContext_onHaServicesUpdated(services) { __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_setOnContext).call(this, 'services', services); }, _EditorContext_onHaConnecting = function _EditorContext_onHaConnecting() { __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_setOnContext).call(this, 'isConnected', false); __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_setOnContext).call(this, 'isRunning', false); }, _EditorContext_onHaClose = function _EditorContext_onHaClose() { __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_setOnContext).call(this, 'isConnected', false); __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_setOnContext).call(this, 'isRunning', false); }, _EditorContext_onHaRunning = function _EditorContext_onHaRunning() { __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_setOnContext).call(this, 'isRunning', true); }, _EditorContext_onHaError = function _EditorContext_onHaError() { __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_setOnContext).call(this, 'isConnected', false); __classPrivateFieldGet(this, _EditorContext_instances, "m", _EditorContext_setOnContext).call(this, 'isRunning', false); }; exports.default = EditorContext;