mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Honour num_repeats and delay_secs in Panasonic Viera remote (#183238)
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Claude Fable 5.1
Joost Lekkerkerker
Copilot Autofix powered by AI
parent
2c674e9871
commit
a49aca689f
@@ -1,9 +1,15 @@
|
||||
"""Remote control support for Panasonic Viera TV."""
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Iterable
|
||||
from typing import Any, override
|
||||
|
||||
from homeassistant.components.remote import RemoteEntity
|
||||
from homeassistant.components.remote import (
|
||||
ATTR_DELAY_SECS,
|
||||
ATTR_NUM_REPEATS,
|
||||
DEFAULT_DELAY_SECS,
|
||||
RemoteEntity,
|
||||
)
|
||||
from homeassistant.const import ATTR_MANUFACTURER, CONF_NAME, STATE_ON
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
@@ -100,5 +106,12 @@ class PanasonicVieraRemoteEntity(RemoteEntity):
|
||||
@override
|
||||
async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
|
||||
"""Send a command to one device."""
|
||||
for cmd in command:
|
||||
await self._remote.async_send_key(cmd)
|
||||
num_repeats: int = kwargs.get(ATTR_NUM_REPEATS, 1)
|
||||
delay_secs: float = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS)
|
||||
command_list = list(command)
|
||||
|
||||
for repeat in range(num_repeats):
|
||||
for index, cmd in enumerate(command_list):
|
||||
if repeat or index:
|
||||
await asyncio.sleep(delay_secs)
|
||||
await self._remote.async_send_key(cmd)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Test the Panasonic Viera remote entity."""
|
||||
|
||||
from unittest.mock import Mock, call
|
||||
from unittest.mock import AsyncMock, Mock, call, patch
|
||||
|
||||
from panasonic_viera import Keys, SOAPError
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
@@ -8,6 +8,9 @@ from pytest_homeassistant.common import MockConfigEntry
|
||||
from homeassistant.components.panasonic_viera.const import ATTR_UDN, DOMAIN
|
||||
from homeassistant.components.remote import (
|
||||
ATTR_COMMAND,
|
||||
ATTR_DELAY_SECS,
|
||||
ATTR_NUM_REPEATS,
|
||||
DEFAULT_DELAY_SECS,
|
||||
DOMAIN as REMOTE_DOMAIN,
|
||||
SERVICE_SEND_COMMAND,
|
||||
)
|
||||
@@ -59,3 +62,55 @@ async def test_send_command(hass: HomeAssistant, mock_remote) -> None:
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_remote.send_key.call_args == call("command")
|
||||
|
||||
|
||||
async def test_send_command_repeats_and_delay(
|
||||
hass: HomeAssistant, mock_remote: Mock
|
||||
) -> None:
|
||||
"""Test num_repeats and delay_secs are honoured by the send_command service."""
|
||||
|
||||
await setup_panasonic_viera(hass)
|
||||
|
||||
data = {
|
||||
ATTR_ENTITY_ID: "remote.panasonic_viera_tv",
|
||||
ATTR_COMMAND: ["down", "right"],
|
||||
ATTR_NUM_REPEATS: 3,
|
||||
ATTR_DELAY_SECS: 0.2,
|
||||
}
|
||||
with patch(
|
||||
"homeassistant.components.panasonic_viera.remote.asyncio.sleep",
|
||||
new_callable=AsyncMock,
|
||||
) as mock_sleep:
|
||||
await hass.services.async_call(
|
||||
REMOTE_DOMAIN, SERVICE_SEND_COMMAND, data, blocking=True
|
||||
)
|
||||
|
||||
assert (
|
||||
mock_remote.send_key.call_args_list == [call(Keys.DOWN), call(Keys.RIGHT)] * 3
|
||||
)
|
||||
# One delay between each key press, none before the first one
|
||||
assert mock_sleep.call_args_list == [call(0.2)] * 5
|
||||
|
||||
|
||||
async def test_send_command_default_delay(
|
||||
hass: HomeAssistant, mock_remote: Mock
|
||||
) -> None:
|
||||
"""Test the default delay is used between repeats when delay_secs is omitted."""
|
||||
|
||||
await setup_panasonic_viera(hass)
|
||||
|
||||
data = {
|
||||
ATTR_ENTITY_ID: "remote.panasonic_viera_tv",
|
||||
ATTR_COMMAND: "up",
|
||||
ATTR_NUM_REPEATS: 2,
|
||||
}
|
||||
with patch(
|
||||
"homeassistant.components.panasonic_viera.remote.asyncio.sleep",
|
||||
new_callable=AsyncMock,
|
||||
) as mock_sleep:
|
||||
await hass.services.async_call(
|
||||
REMOTE_DOMAIN, SERVICE_SEND_COMMAND, data, blocking=True
|
||||
)
|
||||
|
||||
assert mock_remote.send_key.call_args_list == [call(Keys.UP)] * 2
|
||||
mock_sleep.assert_called_once_with(DEFAULT_DELAY_SECS)
|
||||
|
||||
Reference in New Issue
Block a user