From c318968c6277ab73f4d7d3569eb0e58e486d4516 Mon Sep 17 00:00:00 2001 From: Jeppe Fihl-Pearson Date: Sun, 20 Oct 2024 21:02:26 +0100 Subject: [PATCH] Set TV channel using the channel ID instead of channel number (#52) Some models of TVs, e.g. the 50UN73006LA requires the `ssap://tv/openChannel` call to be provided with a channel ID instead of a channel number. Since the channel ID is something obscure like "1_21_101_101_16515_17539_9018" it doesn't make sense for a user to try to input the ID manually. Instead this change makes the `channel set` command look up the list of channels to convert a channel number to a channel ID, if the input looks like a channel number. --- src/alga/cli_channel.py | 13 +++++++++++-- tests/test_cli_channel.py | 23 +++++++++++++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/alga/cli_channel.py b/src/alga/cli_channel.py index c1d26ad..275c629 100644 --- a/src/alga/cli_channel.py +++ b/src/alga/cli_channel.py @@ -36,10 +36,19 @@ def down() -> None: @app.command() -def set(value: Annotated[int, Argument()]) -> None: +def set(value: Annotated[str, Argument()]) -> None: """Change to specific channel""" - client.request("ssap://tv/openChannel", {"channelNumber": value}) + if value.isnumeric(): + # If a channel number is provided, we look up the channel ID as some models require it. + response = client.request("ssap://tv/getChannelList") + + for channel in response["channelList"]: + if channel["channelNumber"] == value: + value = channel["channelId"] + break + + client.request("ssap://tv/openChannel", {"channelId": value}) @app.command() diff --git a/tests/test_cli_channel.py b/tests/test_cli_channel.py index e5c4bbf..f4f054b 100644 --- a/tests/test_cli_channel.py +++ b/tests/test_cli_channel.py @@ -42,18 +42,33 @@ def test_down(mock_request: MagicMock) -> None: assert result.stdout == "" -def test_set(faker: Faker, mock_request: MagicMock) -> None: - channel_number = faker.pyint() +def test_set_channel_id(faker: Faker, mock_request: MagicMock) -> None: + channel_id = faker.pystr() - result = runner.invoke(app, ["channel", "set", f"{channel_number}"]) + result = runner.invoke(app, ["channel", "set", channel_id]) mock_request.assert_called_once_with( - "ssap://tv/openChannel", {"channelNumber": channel_number} + "ssap://tv/openChannel", {"channelId": channel_id} ) assert result.exit_code == 0 assert result.stdout == "" +def test_set_channel_number(faker: Faker, mock_request: MagicMock) -> None: + channel_number = str(faker.pyint()) + channel_id = faker.pystr() + + mock_request.return_value = { + "channelList": [{"channelId": channel_id, "channelNumber": channel_number}] + } + + result = runner.invoke(app, ["channel", "set", channel_number]) + + mock_request.assert_called_with("ssap://tv/openChannel", {"channelId": channel_id}) + assert result.exit_code == 0 + assert result.stdout == "" + + def test_list(faker: Faker, mock_request: MagicMock) -> None: return_value = { "channelList": [