def channel(client, userdata, message): printmsg(message) payload = json.loads(message.payload.decode('utf-8')) remote_config = get_config(payload['endpointid']) try: if payload['operation'] == 'ChangeChannel': if 'number' in payload['channel_data']['channel']: with samsungctl_ts.Remote(remote_config) as remote: for c in payload['channel_data']['channel']['number']: remote.control("KEY_" + str(c)) time.sleep(0.7) remote.control("KEY_ENTER") else: channel_name = '' if 'callSign' in payload['channel_data']['channel']: channel_name = payload['channel_data']['channel'][ 'callSign'] elif 'affiliateCallSign' in payload['channel_data']['channel']: channel_name = payload['channel_data']['channel'][ 'affiliateCallSign'] elif 'channelMetadata' in payload[ 'channel_data'] and 'name' in payload['channel_data'][ 'channelMetadata']: channel_name = payload['channel_data']['channelMetadata'][ 'name'] with samsungctl_ts.Remote(remote_config) as remote: res = get_close_matches( channel_name.lower().replace('&', 'and').replace( 'the ', '').replace(' channel', ''), tv_channels) if len(res) > 0: chan = tv_listings_dict[res[0]] num = "" if chan[3] != None and tv_dict[ payload['endpointid']]['prefer_HD']: num = str(chan[3]) else: num = str(chan[2]) print(channel_name + ': closest match - ' + res[0] + ' - ' + str(num)) for c in num: remote.control("KEY_" + str(c)) time.sleep(0.7) remote.control("KEY_ENTER") elif payload['operation'] == 'SkipChannels': steps = payload['channelCount'] chandown = steps < 0 steps = abs(steps) for i in range(0, steps): with samsungctl_ts.Remote(remote_config) as remote: remote.control("KEY_CHDOWN" if chandown else "KEY_CHUP") time.sleep(0.7) #delay for volume except BaseException as e: print("Failed to send message to TV: " + str(e))
def playback(client, userdata, message): printmsg(message) payload = json.loads(message.payload.decode('utf-8')) remote_config = get_config(payload['endpointid']) try: if payload['operation'] == 'Pause' or payload['operation'] == 'Stop': with samsungctl_ts.Remote(remote_config) as remote: remote.control("KEY_PAUSE") elif payload['operation'] == 'Play': with samsungctl_ts.Remote(remote_config) as remote: remote.control("KEY_PLAY") except BaseException as e: print("Failed to send message to TV: " + str(e))
def power(client, userdata, message): printmsg(message) payload = json.loads(message.payload.decode('utf-8')) remote_config = get_config(payload['endpointid']) try: if payload['operation'] == 'TurnOn': send_magic_packet(payload['endpointid']) time.sleep(3) with samsungctl_ts.Remote(remote_config) as remote: remote.control( "KEY_RETURN" ) #get rid of the on menu when you turn the tv on elif payload['operation'] == 'TurnOff': with samsungctl_ts.Remote(remote_config) as remote: remote.control(power_off_command(payload['endpointid'])) except BaseException as e: print("Failed to send message to TV: " + str(e))
def speaker(client, userdata, message): printmsg(message) payload = json.loads(message.payload.decode('utf-8')) remote_config = get_config(payload['endpointid']) try: if payload['operation'] == 'SetMute': with samsungctl_ts.Remote(remote_config) as remote: remote.control("KEY_MUTE") elif payload['operation'] == 'AdjustVolume': steps = payload['volumeSteps'] voldown = steps < 0 steps = abs(steps) if steps == 10: steps = tvconfig.volume_step_size with samsungctl_ts.Remote(remote_config) as remote: for i in range(0, steps): remote.control("KEY_VOLDOWN" if voldown else "KEY_VOLUP") time.sleep(0.05) #delay for volume except BaseException as e: print("Failed to send message to TV: " + str(e))