Fix full power off of Frame TVs for SamsungTV (#190890)

This commit is contained in:
Simone Chemelli
2026-11-17 07:16:57 +00:00
committed by GitHub
parent 519fe0748b
commit eece43fb2e
3 changed files with 37 additions and 15 deletions
+19 -4
View File
@@ -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")])
+3 -1
View File
@@ -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)
+15 -10
View File
@@ -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(