From eece43fb2e530a492b2ff1ec58dd6f20c1933197 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Tue, 17 Nov 2026 08:16:57 +0100 Subject: [PATCH] Fix full power off of Frame TVs for SamsungTV (#190890) --- homeassistant/components/samsungtv/bridge.py | 23 ++++++++++++++--- tests/components/samsungtv/conftest.py | 4 ++- .../components/samsungtv/test_media_player.py | 25 +++++++++++-------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/samsungtv/bridge.py b/homeassistant/components/samsungtv/bridge.py index f482f228d068..27ba64d0e06b 100644 --- a/homeassistant/components/samsungtv/bridge.py +++ b/homeassistant/components/samsungtv/bridge.py @@ -10,7 +10,7 @@ from typing import Any, cast, override from samsungtvws.async_remote import SamsungTVWSAsyncRemote from samsungtvws.async_rest import SamsungTVAsyncRest -from samsungtvws.command import SamsungTVCommand +from samsungtvws.command import SamsungTVCommand, SamsungTVSleepCommand from samsungtvws.encrypted.command import SamsungTVEncryptedCommand from samsungtvws.encrypted.remote import ( SamsungTVEncryptedWSAsyncRemote, @@ -80,6 +80,10 @@ KEY_PRESS_TIMEOUT = 1.2 ENCRYPTED_MODEL_USES_POWER_OFF = {"H6400", "H6410"} ENCRYPTED_MODEL_USES_POWER = {"JU6400", "JU641D"} +# ~4 s of repeated presses, as sent by a held physical remote +FRAME_POWER_OFF_PRESSES = 40 +FRAME_POWER_OFF_PRESS_INTERVAL = 0.1 + REST_EXCEPTIONS = (HttpApiError, AsyncioTimeoutError, ResponseError) @@ -429,14 +433,19 @@ class SamsungTVWSBaseBridge[ return remote.is_alive() return False - async def _async_send_commands(self, commands: list[_CommandT]) -> None: + async def _async_send_commands( + self, commands: list[_CommandT], key_press_delay: float | None = None + ) -> None: """Send the commands using websocket protocol.""" # recreate connection if connection was dead retry_count = 1 for attempt in range(retry_count + 1): try: if remote := await self._async_get_remote(): - await remote.send_commands(commands) # type: ignore[arg-type] + await remote.send_commands( + commands, # type: ignore[arg-type] + key_press_delay=key_press_delay, + ) except (BrokenPipeError, WebSocketException) as err: # BrokenPipe can occur when the commands is sent to fast # WebSocketException can occur when timed out @@ -732,7 +741,13 @@ class SamsungTVWSBridge( async def _async_send_power_off(self) -> None: """Send power off command to remote.""" if self._get_device_spec("FrameTVSupport") == "true": - await self._async_send_commands(SendRemoteKey.hold("KEY_POWER", 3)) + # A single long press only toggles Art mode on recent Frame firmware + commands: list[SamsungTVCommand] = [] + for _ in range(FRAME_POWER_OFF_PRESSES): + commands.append(SendRemoteKey.press("KEY_POWER")) + commands.append(SamsungTVSleepCommand(FRAME_POWER_OFF_PRESS_INTERVAL)) + commands.append(SendRemoteKey.release("KEY_POWER")) + await self._async_send_commands(commands, key_press_delay=0) else: await self._async_send_commands([SendRemoteKey.click("KEY_POWER")]) diff --git a/tests/components/samsungtv/conftest.py b/tests/components/samsungtv/conftest.py index f05217a7aec9..9c653dcd32f4 100644 --- a/tests/components/samsungtv/conftest.py +++ b/tests/components/samsungtv/conftest.py @@ -230,7 +230,9 @@ def remote_websocket_fixture() -> Generator[Mock]: ): remote_websocket.ws_event_callback = ws_event_callback - async def _send_commands(commands: list[SamsungTVCommand]): + async def _send_commands( + commands: list[SamsungTVCommand], key_press_delay: float | None = None + ): if ( len(commands) == 1 and isinstance(commands[0], ChannelEmitCommand) diff --git a/tests/components/samsungtv/test_media_player.py b/tests/components/samsungtv/test_media_player.py index 24dcac3779e2..6f5e0ba462e5 100644 --- a/tests/components/samsungtv/test_media_player.py +++ b/tests/components/samsungtv/test_media_player.py @@ -814,16 +814,21 @@ async def test_turn_off_websocket_frame( ) # key called assert remote_websocket.send_commands.call_count == 1 - commands = remote_websocket.send_commands.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" + call_args = remote_websocket.send_commands.call_args_list[0] + commands = call_args.args[0] + # Repeated presses must not be slowed down by the default key delay + assert call_args.kwargs["key_press_delay"] == 0 + assert len(commands) == 81 + presses = commands[0:-1:2] + sleeps = commands[1:-1:2] + assert all(isinstance(command, SendRemoteKey) for command in presses) + assert {command.params["Cmd"] for command in presses} == {"Press"} + assert {command.params["DataOfCmd"] for command in presses} == {"KEY_POWER"} + assert all(isinstance(command, SamsungTVSleepCommand) for command in sleeps) + assert {command.delay for command in sleeps} == {0.1} + assert isinstance(commands[-1], SendRemoteKey) + assert commands[-1].params["Cmd"] == "Release" + assert commands[-1].params["DataOfCmd"] == "KEY_POWER" async def test_turn_off_encrypted_websocket(