Ejemplo n.º 1
0
class LgCommand(object):
    def __init__(self, ip):
        self.client = WebOsClient(ip)

        self.commandLines = {
            'off': self.off,
            'on': self.wakeonlan,
            'software-info': self.software_info,
            'volume-up': self.volume_up,
            'volume-down': self.volume_down,
            'current-app': self.current_app,
            'apps': self.apps,
            'services': self.services,
            'get-volume': self.get_volume,
            'get-inputs': self.get_inputs,
            'app': self.app,
            'set-volume': self.set_volume,
            'close-app': self.close_app,
            'mute': self.mute,
            'unmute': self.unmute,
            'get-mute': self.get_mute,
            'get-input': self.get_input,
            'set-input': self.set_input,
            'channel-up': self.channel_up,
            'channel-down': self.channel_down,
            'channels': self.channels,
            'get-channel': self.get_channel,
            'info': self.info,
            'set-channel': self.set_channel,
            'play': self.play,
            'pause': self.pause,
            'stop': self.stop,
            'close': self.close,
            'rewind': self.rewind,
            'fast-forward': self.fast_forward,
            'send-message': self.send_message,
            'enter': self.enter,
            'delete': self.delete,

            #'3d-on': threeDOn,
            #'3d-off': threeDOff,
        }

    def run(self, command, arg=None):
        return self.commandLines[command](arg)

    def send_message(self, arg):
        return self.client.send_message(arg)

    def delete(self, arg):
        return self.client.send_delete_key()

    def enter(self, arg):
        return self.client.send_enter_key()

    def play(self, arg):
        return self.client.play()

    def pause(self, arg):
        return self.client.pause()

    def stop(self, arg):
        return self.client.stop()

    def close(self, arg):
        return self.client.close()

    def rewind(self, arg):
        return self.client.rewind()

    def fast_forward(self, arg):
        return self.client.fast_forward()

    def info(self, arg):
        return self.client.get_channel_info()

    def set_channel(self, arg):
        return self.client.set_channel(arg)

    def get_channel(self, arg):
        return self.client.get_current_channel()

    def channels(self, arg):
        return self.client.get_channels()

    def channel_down(self, arg):
        return self.client.channel_down()

    def channel_up(self, arg):
        return self.client.channel_up()

    def get_input(self, arg):
        return self.client.get_input()

    def set_input(self, arg):
        return self.client.set_input(arg)

    def unmute(self, arg):
        return self.client.set_mute(False)

    def mute(self, arg):
        return self.client.set_mute(True)

    def get_mute(self, arg):
        return self.client.get_muted()

    def set_volume(self, arg):
        return self.client.set_volume(int(arg))

    def close_app(self, arg):
        return self.client.close_app(arg)

    def app(self, arg):
        return self.client.launch_app(arg)

    def get_inputs(self, arg):
        return self.client.get_inputs()

    def get_volume(self, arg):
        return self.client.get_volume()

    def services(self, arg):
        return self.client.get_services()

    def current_app(self, arg):
        return self.client.get_current_app()

    def apps(self, arg):
        return self.client.get_apps()

    def off(self, arg):
        self.client.power_off()
        return "TV has been turned off."

    def software_info(self, arg):
        return self.check()

    def volume_down(self, arg):
        return self.client.volume_down()

    def volume_up(self, arg):
        return self.client.volume_up()

    def wakeonlan(self, mac):
        if mac is not None:
            addr_byte = mac.split(':')
            hw_addr = struct.pack('BBBBBB', int(addr_byte[0], 16),
                                  int(addr_byte[1], 16), int(addr_byte[2], 16),
                                  int(addr_byte[3], 16), int(addr_byte[4], 16),
                                  int(addr_byte[5], 16))
            msg = b'\xff' * 6 + hw_addr * 16
            socket_instance = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            socket_instance.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,
                                       1)
            socket_instance.sendto(msg, ('<broadcast>', 9))
            socket_instance.close()
            return "TV has been turned on."
        else:
            return "mac address (arg) not set. use -h for help."

    def check(self):
        return self.client.get_software_info()
Ejemplo n.º 2
0
class BasePlugin:
    def onStart(self):
        if Parameters["Mode6"] == "Debug":
            Domoticz.Debugging(1)
            Domoticz.Trace(True)
            self.debug = True
            DumpConfigToLog()

        Domoticz.Debug("onStart called")
        updateInterval = int(Parameters["Mode1"])
        if updateInterval > 30 or updateInterval < 7:
            updateInterval = 10
            Domoticz.Error("Invalid interval setting, changed to 10")

        Domoticz.Heartbeat(updateInterval)
        self.webos_client = WebOsClient(Parameters["Address"], "NONE", 5)

        # First get some TV details
        try:
            tv_details = self.webos_client.get_software_info()
            Domoticz.Log("Product name: " + tv_details["product_name"])
            Domoticz.Log("Software version: " + tv_details["major_ver"] + "." +
                         tv_details["minor_ver"])
            Domoticz.Log("MAC Address: " + tv_details["device_id"])
        except Exception as e:
            Domoticz.Error("Error at reading TV info")

        if not (1 in Devices):
            Domoticz.Device(Name="TV Status", Unit=1, Type=17,
                            Switchtype=17).Create()

        if not (2 in Devices):
            Domoticz.Device(Name="TV Volume",
                            Unit=2,
                            Type=244,
                            Subtype=73,
                            Switchtype=7,
                            Image=8).Create()

        return True

    def onStop(self):
        Domoticz.Debug("onStop called")

    def onConnect(self, Connection, Status, Description):
        Domoticz.Debug("onConnect called")

    def onMessage(self, Connection, Data):
        Domoticz.Debug("onMessage called")

    def onCommand(self, Unit, Command, Level, Hue):
        Domoticz.Debug("onCommand called for Unit " + str(Unit) +
                       ": Parameter '" + str(Command) + "', Level: " +
                       str(Level))

        if Unit == 2:
            self.webos_client.set_volume(int(Level))

    def onNotification(self, Name, Subject, Text, Status, Priority, Sound,
                       ImageFile):
        Domoticz.Debug("Notification: " + Name + "," + Subject + "," + Text +
                       "," + Status + "," + str(Priority) + "," + Sound + "," +
                       ImageFile)
        if Parameters["Mode2"] == "true":
            self.webos_client.send_message(Subject)

    def onDisconnect(self, Connection):
        Domoticz.Debug("onDisconnect called")

    def onHeartbeat(self):
        Domoticz.Debug("onHeartbeat called")
        try:
            tv_input = self.webos_client.get_input()
            if (tv_input.lower() == "none"):
                UpdateDevice(1, 0, "Off")
                return True
            else:
                UpdateDevice(1, 1, str(tv_input))

            UpdateDevice(2, 2, str(self.webos_client.get_volume()))
        except:
            #Domoticz.Error("Timeout connecting to television")
            UpdateDevice(1, 0, "Off")
            UpdateDevice(2, 0, "0")

    def GetValue(self, arr, sKey, defValue):
        try:
            if str(sKey) in arr:
                if (str(arr[str(sKey)]).lower() == "none"):
                    return defValue
                else:
                    return arr[str(sKey)]
            else:
                return defValue
        except:
            return defValue