aboutsummaryrefslogtreecommitdiff
path: root/test/test_amixer.py
blob: 1a78592a0d580d91aef4f7cee8c95e7fbbb1bc1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
import include_homekit

from argparse import ArgumentParser
from homekit.config import config
from homekit.audio import amixer


def validate_control(input: str):
    for control in config['amixer']['controls']:
        if control['name'] == input:
            return
    raise ValueError(f'invalid control name: {input}')


if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('--get-all', action='store_true')
    parser.add_argument('--mute', type=str)
    parser.add_argument('--unmute', type=str)
    parser.add_argument('--cap', type=str)
    parser.add_argument('--nocap', type=str)
    parser.add_argument('--get', type=str)
    parser.add_argument('--incr', type=str)
    parser.add_argument('--decr', type=str)
    # parser.add_argument('--dump-config', action='store_true')

    args = config.load_app('test_amixer', parser=parser)

    # if args.dump_config:
    #     print(config.data)
    #     sys.exit()

    if args.get_all:
        for control in amixer.get_all():
            print(f'control = {control["name"]}')
            for line in control['info'].split('\n'):
                print(f'    {line}')
            print()
        sys.exit()

    if args.get:
        info = amixer.get(args.get)
        print(info)
        sys.exit()

    for action in ['incr', 'decr']:
        if hasattr(args, action):
            control = getattr(args, action)
            if control is None:
                continue

            print(f'attempting to {action} {control}')
            validate_control(control)
            func = getattr(amixer, action)
            try:
                func(control, step=5)
            except amixer.AmixerError as e:
                print('error: ' + str(e))
            sys.exit()

    for action in ['mute', 'unmute', 'cap', 'nocap']:
        if hasattr(args, action):
            control = getattr(args, action)
            if control is None:
                continue

            print(f"attempting to {action} {control}")

            validate_control(control)
            func = getattr(amixer, action)
            try:
                func(control)
            except amixer.AmixerError as e:
                print('error: ' + str(e))
            sys.exit()