def toggle_input(dev, args, input, mode): res = dev.request(Zone.get_status(args.zone)) if res.status_code != 200 or res.json()["response_code"] != 0: print("Can't toggle input (can't get device status).", file=stderr) return powered = res.json()["power"] == "on" res = dev.request(NetUSB.get_play_info()) if res.status_code != 200 or res.json()["response_code"] != 0: print("Can't toggle input (can't get play info).", file=stderr) return False playing = res.json()["playback"] == "play" curr_input = res.json()["input"] if not powered: set_power(dev, args, "on") if curr_input != input or not powered: set_input(dev, args, input, mode) return if playing: set_playback(dev, args, "stop") else: set_playback(dev, args, "play")
def prev(dev, args): res = dev.request(Zone.get_status(args.zone)) if res.status_code != 200 or res.json()["response_code"] != 0: print( "Can't play previous track / radio station (can't get device status).", file=stderr) return if res.json()["input"] == "net_radio": radio_prev(dev, args) else: set_playback(dev, args, "previous")
def main(): parser = ArgumentParser(description="Controls a Yamaha MusicCast device") parser.add_argument("-v", "--version", action="version", version="Ampio Yamaha Integration " + __version__) parser.add_argument("-z", "--zone", help="target zone (default: main)", action="store", default="main") parser.add_argument("host", help="target device's IP address or hostname", action="store") parser.add_argument("--volume-up", help="increase target device's volume by one step", action="count", default=0) parser.add_argument("--volume-down", help="decrease target device's volume by one step", action="count", default=0) parser.add_argument( "--next", help="play next track / radio station (depends on input)", action="count", default=0) parser.add_argument( "--prev", help="play previous track / radio station (depends on input)", action="count", default=0) parser.add_argument("--set-input", help="set input", action="append", default=[], dest="inputs", nargs=2) parser.add_argument("--set-playback", help="set playback", action="append", default=[], dest="playback") parser.add_argument("--set-power", help="set power", action="append", default=[], dest="power") parser.add_argument( "--toggle-input", help="toggle between playing on the target input and power standby", action="append", default=[], dest="toggle", nargs=2) args = parser.parse_args() device = Device(args.host) try: device.request(Zone.get_status(args.zone)) except ConnectionError: print("Can't connect to host.", file=stderr) except AssertionError: print("Invalid zone specified.", file=stderr) else: for _ in range(args.volume_up): yamaha.volume_up(device, args) for _ in range(args.volume_down): yamaha.volume_down(device, args) for _ in range(args.next): yamaha.next(device, args) for _ in range(args.prev): yamaha.prev(device, args) for [input, mode] in args.inputs: yamaha.set_input(device, args, input, mode) for playback in args.playback: yamaha.set_playback(device, args, playback) for power in args.power: yamaha.set_power(device, args, power) for [input, mode] in args.toggle: yamaha.toggle_input(device, args, input, mode)
def radio_prev(dev, args): res = dev.request(Zone.get_status(args.zone)) if res.status_code != 200 or res.json()["response_code"] != 0: print("Can't play previous radio station (can't get device status).", file=stderr) return if res.json()["input"] != "net_radio": print("Can't play previous radio station (input is not 'net_radio').", file=stderr) return list_info = get_radio_list_info(dev) if list_info == None: print("Can't play previous radio station (can't get list info).", file=stderr) return while list_info != ["Radio", "Podcasts"] and list_info != None: if not set_radio_list_control(dev, args.zone, "return", 0): print( "Can't play previous radio station (can't set list control).", file=stderr) return list_info = get_radio_list_info(dev) if list_info == None: print("Can't play previous radio station (can't get list info).", file=stderr) return if not set_radio_list_control(dev, args.zone, "select", 0): print("Can't play previous radio station (can't set list control).", file=stderr) return list_info = get_radio_list_info(dev) if list_info == None: print("Can't play previous radio station (can't get list info).", file=stderr) return if not set_radio_list_control(dev, args.zone, "select", 0): print("Can't play previous radio station (can't set list control).", file=stderr) return list_info = get_radio_list_info(dev) if list_info == None: print("Can't play previous radio station (can't get list info).", file=stderr) return res = dev.request(NetUSB.get_play_info()) if res.status_code != 200 or res.json()["response_code"] != 0: print("Can't play previous radio station (can't get play info).", file=stderr) return current_station = search(r"(^.+) \(", res.json()["artist"]).groups()[0] new_station = 0 try: new_station = list_info.index(current_station) - 1 except ValueError: pass new_station %= len(list_info) if not set_radio_list_control(dev, args.zone, "play", new_station): print("Can't play previous radio station (can't set list control).", file=stderr) return