Skip to content

Commit

Permalink
fix list parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisChrist committed Apr 19, 2024
1 parent 24b2705 commit 84c9e39
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/pyblu/_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def parse_presets(response_dict: dict[str, Any]) -> list[Preset]:
if not presets_raw:
return []

if not isinstance(presets_raw, list):
presets_raw = [presets_raw]

presets = [
Preset(
name=chained_get(x, "@name"),
Expand Down
7 changes: 6 additions & 1 deletion src/pyblu/_player.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import aiohttp
import xmltodict
from urllib.parse import unquote

from pyblu._entities import Status, Volume, SyncStatus, PairedPlayer, PlayQueue, Preset, Input
from pyblu._parse import parse_slave_list, parse_sync_status, parse_status, parse_volume, chained_get, parse_play_queue, parse_presets
Expand Down Expand Up @@ -366,12 +367,16 @@ async def inputs(self) -> list[Input]:
response_dict = xmltodict.parse(response_data)

inputs_raw = chained_get(response_dict, "radiotime", "item")

if not isinstance(inputs_raw, list):
inputs_raw = [inputs_raw]

inputs = [
Input(
id=chained_get(x, "@id"),
text=chained_get(x, "@text"),
image=chained_get(x, "@image"),
url=chained_get(x, "@URL"),
url=chained_get(x, "@URL", _map=unquote),
)
for x in inputs_raw
]
Expand Down
46 changes: 43 additions & 3 deletions tests/test_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,26 @@ async def test_presets():
Preset(id=2, name="Second", url="Spotify:play", volume=10, image="/Sources/images/SpotifyIcon.png"),
]

async def test_presets_only_one():
with aioresponses() as mocked:
mocked.get(
"http://node:11000/Presets",
status=200,
body="""
<presets prid="2">
<preset url="Spotify:play" id="1" name="My preset" image="/Sources/images/SpotifyIcon.png"/>
</presets>
""",
)
async with Player("node") as client:
presets = await client.presets()

mocked.assert_called_once()

assert presets == [
Preset(id=1, name="My preset", url="Spotify:play", volume=None, image="/Sources/images/SpotifyIcon.png"),
]


async def test_preset_empty():
with aioresponses() as mocked:
Expand Down Expand Up @@ -502,7 +522,27 @@ async def test_inputs():
mocked.assert_called_once()

assert inputs == [
Input(id="input3", text="Bluetooth", image="/images/BluetoothIcon.png", url="Capture%3Abluez%3Abluetooth"),
Input(id="input2", text="HDMI ARC", image="/images/capture/ic_tv.png", url="Capture%3Ahw%3Aimxspdif%2C0%2F1%2F25%2F2%3Fid%3Dinput2"),
Input(id="Spotify", text="Spotify", image="/Sources/images/SpotifyIcon.png", url="Spotify%3Aplay"),
Input(id="input3", text="Bluetooth", image="/images/BluetoothIcon.png", url="Capture:bluez:bluetooth"),
Input(id="input2", text="HDMI ARC", image="/images/capture/ic_tv.png", url="Capture:hw:imxspdif,0/1/25/2?id=input2"),
Input(id="Spotify", text="Spotify", image="/Sources/images/SpotifyIcon.png", url="Spotify:play"),
]

async def test_inputs_only_one():
with aioresponses() as mocked:
mocked.get(
"http://node:11000/RadioBrowse?service=Capture",
status=200,
body="""
<radiotime service="Capture">
<item typeIndex="bluetooth-1" playerName="Node" text="Bluetooth" inputType="bluetooth" id="input3" URL="Capture%3Abluez%3Abluetooth" image="/images/BluetoothIcon.png" type="audio"/>
</radiotime>
""",
)
async with Player("node") as client:
inputs = await client.inputs()

mocked.assert_called_once()

assert inputs == [
Input(id="input3", text="Bluetooth", image="/images/BluetoothIcon.png", url="Capture:bluez:bluetooth"),
]

0 comments on commit 84c9e39

Please sign in to comment.