node-red-contrib-apple-tv-x
Version:
Nodes for controlling Apple TVs in Node-RED (wrapper pyatv)
70 lines (61 loc) • 1.84 kB
JavaScript
module.exports = function (RED) {
"use strict";
const types_1 = require("@sebbo2002/node-pyatv/dist/lib/types");
function ATVxOut(n) {
RED.nodes.createNode(this, n);
this.atvx = n.atvx;
this.atvxConfig = RED.nodes.getNode(this.atvx);
this.skip_deprecated = n.skip_deprecated;
let node = this;
node.status({});
node.onStatus = function (obj) {
if (obj) {
node.status({
fill: `${obj.color}`,
shape: "dot",
text: `${obj.text}`,
});
}
setTimeout(() => {
node.status({});
}, 3.5 * 1000);
};
this.on("input", function (msg) {
if (node.atvxConfig.connect) {
if (node.atvxConfig.backend == "pyatv-cli") {
let payload = msg.payload.replace(/(?:^|\s|["'([{])+\S/g, (match) =>
match.toLowerCase()
);
let command = payload.split("=")[0];
if (typeof types_1.NodePyATVInternalKeys[payload] !== "undefined") {
node.atvxConfig.connect.pressKey(payload).catch((error) => {
_errorCli(error);
});
} else if (
["play_url", "stream_file", "launch_app", "set_volume"].includes(command)
) {
node.atvxConfig.connect._pressKey(
payload,
types_1.NodePyATVExecutableType.atvremote
);
} else {
_errorCli(`Unsupported command: ${payload}`);
}
}
}
});
function _errorCli(error) {
if (error) {
if (
node.skip_deprecated &&
error.toString().match(/pyatv.*DeprecationWarning/i)
) {
return;
}
node.onStatus({ color: "red", text: "error" });
node.error(error);
}
}
}
RED.nodes.registerType("atvx-out", ATVxOut);
};