class SnipsLGTV: def __init__(self, _ip, _mac, _onkyoip): self.ip = str(_ip) self.mac = _mac self.onkyoip = _onkyoip self.client = WebOsClient(self.ip, "/keys/lg.key") self.receiver = eISCP(self.onkyoip) def turn_on(self): send_magic_packet(self.mac) self.client.power_on() def turn_off(self): print("SNIPSLGTV OFF CALLED: %s" % self.ip) self.client.power_off() def open_app(self, app_name): print("Asked to start %s" % app_name) app_id = id_dict[app_name] print("Given app_id %s" % app_id) self.client.launch_app(app_id) def close_app(self): self.client.close_app() def set_volume(self, volume): if volume > 70: print("TOO LOUD!") else: print("Setting volume to %i" % volume) self.receiver.command("volume %d" % volume) print(self.client.last_response) def mute(self): self.client.set_mute("true") def unmute(self): self.client.set_mute("false")
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()