From 36049ac5146f1f80870c8b66849978c5743b1cce Mon Sep 17 00:00:00 2001
From: epenet <6771947+epenet@users.noreply.github.com>
Date: Tue, 8 Mar 2022 10:10:17 +0100
Subject: [PATCH] Batch send commands in SamsungTV (#67847)

Co-authored-by: epenet <epenet@users.noreply.github.com>
---
 homeassistant/components/samsungtv/bridge.py  |  3 +-
 .../components/samsungtv/test_media_player.py | 44 ++++++++++---------
 2 files changed, 24 insertions(+), 23 deletions(-)

diff --git a/homeassistant/components/samsungtv/bridge.py b/homeassistant/components/samsungtv/bridge.py
index 02a36915dc6..99e9f877413 100644
--- a/homeassistant/components/samsungtv/bridge.py
+++ b/homeassistant/components/samsungtv/bridge.py
@@ -422,8 +422,7 @@ class SamsungTVWSBridge(SamsungTVBridge):
             for _ in range(retry_count + 1):
                 try:
                     if remote := await self._async_get_remote():
-                        for command in commands:
-                            await remote.send_command(command)
+                        await remote.send_command(commands)
                     break
                 except (
                     BrokenPipeError,
diff --git a/tests/components/samsungtv/test_media_player.py b/tests/components/samsungtv/test_media_player.py
index bc693918b5f..e5520098a4a 100644
--- a/tests/components/samsungtv/test_media_player.py
+++ b/tests/components/samsungtv/test_media_player.py
@@ -641,9 +641,10 @@ async def test_turn_off_websocket(
     )
     # key called
     assert remotews.send_command.call_count == 1
-    command = remotews.send_command.call_args_list[0].args[0]
-    assert isinstance(command, SendRemoteKey)
-    assert command.params["DataOfCmd"] == "KEY_POWER"
+    commands = remotews.send_command.call_args_list[0].args[0]
+    assert len(commands) == 1
+    assert isinstance(commands[0], SendRemoteKey)
+    assert commands[0].params["DataOfCmd"] == "KEY_POWER"
 
     # commands not sent : power off in progress
     remotews.send_command.reset_mock()
@@ -678,18 +679,17 @@ async def test_turn_off_websocket_frame(
         DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_ID}, True
     )
     # key called
-    assert remotews.send_command.call_count == 3
-    command = remotews.send_command.call_args_list[0].args[0]
-    assert isinstance(command, SendRemoteKey)
-    assert command.params["Cmd"] == "Press"
-    assert command.params["DataOfCmd"] == "KEY_POWER"
-    command = remotews.send_command.call_args_list[1].args[0]
-    assert isinstance(command, SamsungTVSleepCommand)
-    assert command.delay == 3
-    command = remotews.send_command.call_args_list[2].args[0]
-    assert isinstance(command, SendRemoteKey)
-    assert command.params["Cmd"] == "Release"
-    assert command.params["DataOfCmd"] == "KEY_POWER"
+    assert remotews.send_command.call_count == 1
+    commands = remotews.send_command.call_args_list[0].args[0]
+    assert len(commands) == 3
+    assert isinstance(commands[0], SendRemoteKey)
+    assert commands[0].params["Cmd"] == "Press"
+    assert commands[0].params["DataOfCmd"] == "KEY_POWER"
+    assert isinstance(commands[1], SamsungTVSleepCommand)
+    assert commands[1].delay == 3
+    assert isinstance(commands[2], SendRemoteKey)
+    assert commands[2].params["Cmd"] == "Release"
+    assert commands[2].params["DataOfCmd"] == "KEY_POWER"
 
 
 async def test_turn_off_legacy(hass: HomeAssistant, remote: Mock) -> None:
@@ -1023,9 +1023,10 @@ async def test_play_media_app(hass: HomeAssistant, remotews: Mock) -> None:
         True,
     )
     assert remotews.send_command.call_count == 1
-    command = remotews.send_command.call_args_list[0].args[0]
-    assert isinstance(command, ChannelEmitCommand)
-    assert command.params["data"]["appId"] == "3201608010191"
+    commands = remotews.send_command.call_args_list[0].args[0]
+    assert len(commands) == 1
+    assert isinstance(commands[0], ChannelEmitCommand)
+    assert commands[0].params["data"]["appId"] == "3201608010191"
 
 
 async def test_select_source_app(hass: HomeAssistant, remotews: Mock) -> None:
@@ -1040,6 +1041,7 @@ async def test_select_source_app(hass: HomeAssistant, remotews: Mock) -> None:
         True,
     )
     assert remotews.send_command.call_count == 1
-    command = remotews.send_command.call_args_list[0].args[0]
-    assert isinstance(command, ChannelEmitCommand)
-    assert command.params["data"]["appId"] == "3201608010191"
+    commands = remotews.send_command.call_args_list[0].args[0]
+    assert len(commands) == 1
+    assert isinstance(commands[0], ChannelEmitCommand)
+    assert commands[0].params["data"]["appId"] == "3201608010191"
-- 
GitLab