Skip to content

Commit

Permalink
Support response format of newer webOS payloads for "volume get" comm…
Browse files Browse the repository at this point in the history
…and (#75)

The response use a slightly different format for newer webOS versions than what
I have available, so I haven't been aware of this difference until it was
reported in #73.
  • Loading branch information
Tenzer authored Dec 9, 2024
1 parent 0cce990 commit 3fd5680
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
12 changes: 11 additions & 1 deletion src/alga/cli_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ def get() -> None:
"""Get current volume"""

response = client.request("ssap://audio/getVolume")

if "volume" in response:
# webOS 3.0
volume = response["volume"]
muted = response["muted"]
else:
# Newer versions
volume = response["volumeStatus"]["volume"]
muted = response["volumeStatus"]["muteStatus"]

print(
f"Volume is currently set to [bold]{response['volume']}[/bold] and is currently {'[red]' if response['muted'] else '[green]not '}muted"
f"Volume is currently set to [bold]{volume}[/bold] and is currently {'[red]' if muted else '[green]not '}muted"
)


Expand Down
29 changes: 15 additions & 14 deletions tests/test_cli_volume.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest.mock import MagicMock

import pytest
from faker import Faker
from typer.testing import CliRunner

Expand Down Expand Up @@ -35,30 +36,30 @@ def test_set(faker: Faker, mock_request: MagicMock) -> None:
assert result.stdout == ""


def test_get_muted(faker: Faker, mock_request: MagicMock) -> None:
@pytest.mark.parametrize(
["muted", "version"], [[True, "old"], [False, "old"], [True, "new"], [False, "new"]]
)
def test_get(faker: Faker, mock_request: MagicMock, muted: bool, version: str) -> None:
volume = faker.pyint()
mock_request.return_value = {"volume": volume, "muted": True}
if version == "old":
mock_request.return_value = {"volume": volume, "muted": muted}
else:
mock_request.return_value = {
"volumeStatus": {"volume": volume, "muteStatus": muted}
}

result = runner.invoke(app, ["volume", "get"])

mock_request.assert_called_once_with("ssap://audio/getVolume")
assert result.exit_code == 0
assert (
result.stdout == f"Volume is currently set to {volume} and is currently muted\n"
)


def test_get_not_muted(faker: Faker, mock_request: MagicMock) -> None:
volume = faker.pyint()
mock_request.return_value = {"volume": volume, "muted": False}

result = runner.invoke(app, ["volume", "get"])
muted_text = ""
if not muted:
muted_text = "not "

mock_request.assert_called_once_with("ssap://audio/getVolume")
assert result.exit_code == 0
assert (
result.stdout
== f"Volume is currently set to {volume} and is currently not muted\n"
== f"Volume is currently set to {volume} and is currently {muted_text}muted\n"
)


Expand Down

0 comments on commit 3fd5680

Please sign in to comment.