Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alga sound-output pick command #84

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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