Skip to content

Commit

Permalink
fix: correctly parse volume with decimal seperator (#238)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
felurx and pre-commit-ci[bot] authored Oct 15, 2023
1 parent 3e413b1 commit 35acae2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/aiovlc/model/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def parse_output(self, output: list[str]) -> StatusOutput:
input_loc_item = output.pop(0)
input_loc = "%20".join(input_loc_item.split(" ")[3:-1])
if len(output) == 2:
audio_volume = int(output[0].split(" ")[3])
audio_volume = int(float(output[0].split(" ")[3].replace(",", ".")))
state = output[1].split(" ")[2]
else:
raise CommandParseError("Could not get status.")
Expand Down Expand Up @@ -356,7 +356,7 @@ class Volume(Command[VolumeOutput]):
def parse_output(self, output: list[str]) -> VolumeOutput:
"""Parse command output."""
try:
audio_volume = int(output[0])
audio_volume = int(float(output[0].replace(",", ".")))
except (IndexError, ValueError) as err:
raise CommandParseError("Could not get volume.") from err
return VolumeOutput(audio_volume=audio_volume)
Expand Down
68 changes: 62 additions & 6 deletions tests/model/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,25 +423,53 @@ async def test_set_volume_command_error(
assert mock_writer.write.call_count == 0


@pytest.mark.parametrize(
"read, audio_volume, state, input_loc",
[
(b"( audio volume: 0 )\r\n( state stopped )\r\n> ", 0, "stopped", None),
(b"( audio volume: 0.0 )\r\n( state stopped )\r\n> ", 0, "stopped", None),
(b"( audio volume: 0,0 )\r\n( state stopped )\r\n> ", 0, "stopped", None),
(
(
b"( new input: file:///path/to/music.mp3 )\r\n"
b"( audio volume: 128.0 )\r\n( state paused )\r\n> "
),
128,
"paused",
"file:///path/to/music.mp3",
),
(
(
b"( new input: file:///home/felix/Musik/Madonna - Jump.ogg )\r\n"
b"( audio volume: 256.0 )\r\n( state playing )\r\n> "
),
256,
"playing",
"file:///home/felix/Musik/Madonna%20-%20Jump.ogg",
),
],
)
async def test_status_command(
transport: AsyncMock,
client_connected: Client,
read: list[bytes],
audio_volume: int,
state: str,
input_loc: str | None,
) -> None:
"""Test the status command."""
mock_reader, mock_writer = transport.return_value
mock_reader.readuntil.return_value = (
b"( audio volume: 0 )\r\n( state stopped )\r\n> "
)
mock_reader.readuntil.return_value = read

output = await client_connected.status()

assert mock_writer.write.call_count == 1
assert mock_writer.write.call_args == call(b"status\n")
assert mock_reader.readuntil.call_count == 1
assert output
assert output.audio_volume == 0
assert output.state == "stopped"
assert output.input_loc is None
assert output.audio_volume == audio_volume
assert output.state == state
assert output.input_loc == input_loc


async def test_stop_command(
Expand All @@ -457,3 +485,31 @@ async def test_stop_command(
assert mock_writer.write.call_count == 1
assert mock_writer.write.call_args == call(b"stop\n")
assert mock_reader.readuntil.call_count == 1


@pytest.mark.parametrize(
"read, audio_volume",
[
(b"0\r\n> ", 0),
(b"0.0\r\n> ", 0),
(b"0,0\r\n> ", 0),
(b"128.0\r\n> ", 128),
],
)
async def test_volume_command(
transport: AsyncMock,
client_connected: Client,
read: list[bytes],
audio_volume: int,
) -> None:
"""Test the volume command."""
mock_reader, mock_writer = transport.return_value
mock_reader.readuntil.return_value = read

output = await client_connected.volume()

assert mock_writer.write.call_count == 1
assert mock_writer.write.call_args == call(b"volume\n")
assert mock_reader.readuntil.call_count == 1
assert output
assert output.audio_volume == audio_volume

0 comments on commit 35acae2

Please sign in to comment.