node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
94 lines (93 loc) • 5.19 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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _SwitchController_instances, _SwitchController_entityConfigNode, _SwitchController_integration, _SwitchController_isSwitchEntityEnabled_get, _SwitchController_isSwitchEntityEnabled_set;
Object.defineProperty(exports, "__esModule", { value: true });
const InputOutputController_1 = __importDefault(require("../../common/controllers/InputOutputController"));
const globals_1 = require("../../globals");
var OutputType;
(function (OutputType) {
OutputType["Input"] = "input";
OutputType["StateChange"] = "stateChange";
})(OutputType || (OutputType = {}));
class SwitchController extends InputOutputController_1.default {
constructor(props) {
super(props);
_SwitchController_instances.add(this);
_SwitchController_entityConfigNode.set(this, void 0);
_SwitchController_integration.set(this, void 0);
__classPrivateFieldSet(this, _SwitchController_entityConfigNode, props.entityConfigNode, "f");
__classPrivateFieldSet(this, _SwitchController_integration, __classPrivateFieldGet(this, _SwitchController_entityConfigNode, "f")
.integration, "f");
}
async onInput({ message, parsedMessage, send, done, }) {
if (typeof parsedMessage.enable.value === 'boolean') {
__classPrivateFieldSet(this, _SwitchController_instances, parsedMessage.enable.value, "a", _SwitchController_isSwitchEntityEnabled_set);
try {
await __classPrivateFieldGet(this, _SwitchController_integration, "f").updateHomeAssistant();
}
catch (e) {
done(e);
}
done();
return;
}
message.outputType = OutputType.Input;
if (__classPrivateFieldGet(this, _SwitchController_instances, "a", _SwitchController_isSwitchEntityEnabled_get)) {
this.status.setSuccess(OutputType.Input);
send(message);
}
else {
this.status.setFailed(OutputType.Input);
send([null, message]);
}
}
async onStateChange(payload) {
if (!payload.changed || !this.node.config.outputOnStateChange)
return;
const message = {};
await this.setCustomOutputs(this.node.config.outputProperties, message, {
config: this.node.config,
entity: {
state: __classPrivateFieldGet(this, _SwitchController_instances, "a", _SwitchController_isSwitchEntityEnabled_get),
},
entityState: __classPrivateFieldGet(this, _SwitchController_instances, "a", _SwitchController_isSwitchEntityEnabled_get),
});
if (__classPrivateFieldGet(this, _SwitchController_instances, "a", _SwitchController_isSwitchEntityEnabled_get)) {
this.node.send([message, null]);
}
else {
this.node.send([null, message]);
}
}
onTrigger(data) {
const message = { topic: 'triggered', ...data.message };
const status = globals_1.RED._('ha-switch.status.triggered');
if (__classPrivateFieldGet(this, _SwitchController_instances, "a", _SwitchController_isSwitchEntityEnabled_get)) {
this.status.setSuccess(status);
this.node.send(message);
}
else {
this.status.setFailed(status);
this.node.send([null, message]);
}
}
}
_SwitchController_entityConfigNode = new WeakMap(), _SwitchController_integration = new WeakMap(), _SwitchController_instances = new WeakSet(), _SwitchController_isSwitchEntityEnabled_get = function _SwitchController_isSwitchEntityEnabled_get() {
return __classPrivateFieldGet(this, _SwitchController_entityConfigNode, "f").state.isEnabled();
}, _SwitchController_isSwitchEntityEnabled_set = function _SwitchController_isSwitchEntityEnabled_set(value) {
__classPrivateFieldGet(this, _SwitchController_entityConfigNode, "f").state.setEnabled(value);
};
exports.default = SwitchController;