Tivo Plugin - Basic

Here is my Tivo Plugin, this is basic in nature and only supports control of the Tivo box. It was written specifically to control the Virgin Media V6 box which does not as far as I am aware support full Tivo functionality. The functionality of the plugin is very basic as effectively the controls are as per the API, I used the command listings found here:

https://www.openhab.org/addons/bindings/tivo/

plugin.Name = "TIVO_Downstairs";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = -1;
plugin.DefaultSettings = {
    "Host"  : "192.168.10.121",
    "Port"  : "31339",
};

var socket = new TCPClient();

function onChangeRequest(device, attribute, value) {
    console.log(plugin.Name +" onChangeRequest called...");
    switch(attribute) {
    	case "MediaCommand":
    	case "InputSource":
    	case "TvChannel.Number":
            var cmd = "";

            if(attribute == "MediaCommand") {
                cmd = "IRCODE " + value;
            } else if(attribute == "InputSource") {
                cmd = "TELEPORT " + value;
            } else if(attribute == "TvChannel.Number") {
                cmd = "SETCH " + value;
            }

            if(cmd) {
                console.log("  sending command: " + cmd);
                socket.send(cmd + "\r");
            } else {
                console.log("ERROR: unsupported command: " + value);
            }
            break;

        default:
            console.log("ERROR: unsupported attribute type: " + attribute);
            break;
    }
}

function onConnect() {
    var host = plugin.Settings["Host"];
    var port = plugin.Settings["Port"];
    console.log(plugin.Name + " onConnect called...");
    console.log("  Host: " + host);
    console.log("  Port: " + port);    
    console.log("  connecting...");
    socket.connect(host, port);
}

function onDisconnect() {
    console.log("onDisconnect called...");
    socket.close();
    console.log("  disconnected");
}

function onPoll() {

}

function onSynchronizeDevices() {
    console.log("onSynchronizeDevices called...");

    var device = new Device();

    device.Id = plugin.Name;
    device.DisplayName = "TIVO_Downstairs";
    device.Icon = "TV";
    device.Capabilities = [ "MediaControl", "MediaInputSource", "TvChannel" ];
    device.Attributes = [];

    plugin.Devices[plugin.Name] = device;

    console.log("  done syncing");
}
1 Like