Skip to content

Commit

Permalink
Add alga sound-output pick command (#84)
Browse files Browse the repository at this point in the history
It makes it much easier to change the sound output device.

I haven't been able to find an API to list the possible sound output devices,
so I've instead added a hardcoded list of devices which seems useful.
  • Loading branch information
Tenzer authored Dec 9, 2024
1 parent 3fd5680 commit be5ee1a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/alga/cli_sound_output.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Annotated

from pzp import pzp
from rich import print
from typer import Argument, Typer

from alga import client
from alga.types import SoundOutputDevice


app = Typer(no_args_is_help=True, help="Audio output device")
Expand All @@ -17,6 +19,26 @@ def get() -> None:
print(f"The current sound output is [bold]{response['soundOutput']}[/bold]")


@app.command()
def pick() -> None:
"""Show picker for selecting a sound output device."""

sound_output_device = pzp(
candidates=[
SoundOutputDevice("tv_speaker", "TV Speaker"),
SoundOutputDevice("external_optical", "Optical Out Device"),
SoundOutputDevice("tv_external_speaker", "Optical Out Device + TV Speaker"),
SoundOutputDevice("external_arc", "HDMI (ARC) Device"),
],
fullscreen=False,
layout="reverse",
)
if sound_output_device:
client.request(
"ssap://audio/changeSoundOutput", {"output": sound_output_device.id_}
)


@app.command()
def set(value: Annotated[str, Argument()]) -> None:
"""Change the output device"""
Expand Down
9 changes: 9 additions & 0 deletions src/alga/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ def __init__(self, input_device: dict[str, Any]) -> None:

def __str__(self) -> str:
return f"{self.name} ({self.id_})"


@dataclass
class SoundOutputDevice:
id_: str
name: str

def __str__(self) -> str:
return self.name
17 changes: 16 additions & 1 deletion tests/test_cli_sound_output.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch

from faker import Faker
from typer.testing import CliRunner

from alga.__main__ import app
from alga.types import SoundOutputDevice


runner = CliRunner()
Expand All @@ -30,3 +31,17 @@ def test_set(faker: Faker, mock_request: MagicMock) -> None:
)
assert result.exit_code == 0
assert result.stdout == ""


def test_pick(faker: Faker, mock_request: MagicMock) -> None:
sound_output_device = SoundOutputDevice(faker.pystr(), faker.pystr())

with patch("alga.cli_sound_output.pzp") as mock_pzp:
mock_pzp.return_value = sound_output_device

result = runner.invoke(app, ["sound-output", "pick"])

mock_request.assert_called_once_with(
"ssap://audio/changeSoundOutput", {"output": sound_output_device.id_}
)
assert result.exit_code == 0
9 changes: 8 additions & 1 deletion tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from faker import Faker

from alga.types import Channel, InputDevice
from alga.types import Channel, InputDevice, SoundOutputDevice


def test_channel(faker: Faker) -> None:
Expand All @@ -19,3 +19,10 @@ def test_input_device(faker: Faker) -> None:
input_device = InputDevice({"id": faker.pystr(), "label": faker.pystr()})

assert str(input_device) == f"{input_device.name} ({input_device.id_})"


def test_sound_output_device(faker: Faker) -> None:
name = faker.pystr()
sound_output_device = SoundOutputDevice(faker.pystr(), name)

assert str(sound_output_device) == name
15 changes: 15 additions & 0 deletions usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ $ alga sound-output [OPTIONS] COMMAND [ARGS]...
**Commands**:

* `get`: Show the current output device
* `pick`: Show picker for selecting a sound output...
* `set`: Change the output device

### `alga sound-output get`
Expand All @@ -583,6 +584,20 @@ $ alga sound-output get [OPTIONS]

* `--help`: Show this message and exit.

### `alga sound-output pick`

Show picker for selecting a sound output device.

**Usage**:

```console
$ alga sound-output pick [OPTIONS]
```

**Options**:

* `--help`: Show this message and exit.

### `alga sound-output set`

Change the output device
Expand Down

0 comments on commit be5ee1a

Please sign in to comment.