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")
Example #3
0
    def do_setScene(self, line):
        """
        For setting the current scene
        If no parameter is specified: Default zone=main and scene=1

        Usage: do_setScene [zone=<main|zone2|zone3|zoneN>] [scene=<1,2,..8>] <enter>
        Example: do_getSceneInfo zone=main scene=1 <enter>
        """
        data = {'zone':'main', 'scene':1}

        args = line.split()
        for arg in args:
            k, v = arg.split('=')
            if k in data.keys():
                data[k] = v

        resp = DEV.request(Zone.set_scene(**data))
        pprint.pprint(resp.json())
Example #4
0
    def do_getSceneInfo(self, line):
        """
        For retrieving possible scene values
        If no parameter is specified: Default zone=main

        Usage: do_getSceneInfo [zone=<main|zone2|zone3|zoneN>] <enter>
        Example: do_getSceneInfo zone=main <enter>
        """
        data = {'zone':'main'}

        args = line.split()
        for arg in args:
            k, v = arg.split('=')
            if k in data.keys():
                data[k] = v

        resp = DEV.request(Zone.get_scene_info(**data))
        pprint.pprint(resp.json())
Example #5
0
    def do_setPower(self, line):
        """
        For setting power status of each Zone.
        If no parameter is specified: Default zone=main and power=toggle

        Usage: do_setPower [zone=<main|zone2|zone3|zoneN>] [power=<on|standby|toggle>] <enter>
        Example: do_setPower zone=main power=on <enter>
        """
        data = {'zone':'main', 'power':'toggle'}

        args = line.split()
        for arg in args:
            k, v = arg.split('=')
            if k in data.keys():
                data[k] = v

        resp = DEV.request(Zone.set_power(**data))
        pprint.pprint(resp.json())
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)
Example #7
0
 def booleans_are_outputted_in_lower_case(self):
     self.assertEqual(Zone.set_mute(DEFAULT_ZONE, False), self.expected)
Example #8
0
 def test_title_case_strings_are_made_lower_case(self):
     self.assertEqual(Zone.set_mute(DEFAULT_ZONE, 'False'), self.expected)
Example #9
0
 def test_lower_case_strings_are_unaffected(self):
     self.assertEqual(Zone.set_mute(DEFAULT_ZONE, 'false'), self.expected)
def set_power(dev, args, power):
    res = dev.request(Zone.set_power(args.zone, power))
    if res.status_code != 200 or res.json()["response_code"] != 0:
        print("Can't set device's power.", file=stderr)
def volume_up(dev, args):
    res = dev.request(Zone.set_volume(args.zone, 'up', None))
    if res.status_code != 200 or res.json()["response_code"] != 0:
        print("Can't increase device volume.", file=stderr)
def set_input(dev, args, input, mode):
    res = dev.request(Zone.set_input(args.zone, input, mode))
    if res.status_code != 200 or res.json()["response_code"] != 0:
        print("Can't set input.", file=stderr)
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