init home-assistant

This commit is contained in:
2026-11-19 15:15:32 -05:00
parent 2e81d9d300
commit feec6ef9fb
19230 changed files with 3335526 additions and 0 deletions

View File

@@ -0,0 +1 @@
"""Tests for the Android TV Remote integration."""

View File

@@ -0,0 +1,109 @@
"""Fixtures for the Android TV Remote integration tests."""
from collections.abc import Callable, Generator
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from homeassistant.components.androidtv_remote.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from tests.common import MockConfigEntry
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Mock setting up a config entry."""
with patch(
"homeassistant.components.androidtv_remote.async_setup_entry",
return_value=True,
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture
def mock_unload_entry() -> Generator[AsyncMock]:
"""Mock unloading a config entry."""
with patch(
"homeassistant.components.androidtv_remote.async_unload_entry",
return_value=True,
) as mock_unload_entry:
yield mock_unload_entry
@pytest.fixture
def mock_api() -> Generator[MagicMock]:
"""Return a mocked AndroidTVRemote."""
with patch(
"homeassistant.components.androidtv_remote.helpers.AndroidTVRemote",
) as mock_api_cl:
mock_api = mock_api_cl.return_value
mock_api.async_connect = AsyncMock(return_value=None)
mock_api.device_info = {
"manufacturer": "My Android TV manufacturer",
"model": "My Android TV model",
}
is_on_updated_callbacks: list[Callable] = []
current_app_updated_callbacks: list[Callable] = []
volume_info_updated_callbacks: list[Callable] = []
is_available_updated_callbacks: list[Callable] = []
def mocked_add_is_on_updated_callback(callback: Callable):
is_on_updated_callbacks.append(callback)
def mocked_add_current_app_updated_callback(callback: Callable):
current_app_updated_callbacks.append(callback)
def mocked_add_volume_info_updated_callback(callback: Callable):
volume_info_updated_callbacks.append(callback)
def mocked_add_is_available_updated_callbacks(callback: Callable):
is_available_updated_callbacks.append(callback)
def mocked_is_on_updated(is_on: bool):
for callback in is_on_updated_callbacks:
callback(is_on)
def mocked_current_app_updated(current_app: str):
for callback in current_app_updated_callbacks:
callback(current_app)
def mocked_volume_info_updated(volume_info: dict[str, str | bool]):
for callback in volume_info_updated_callbacks:
callback(volume_info)
def mocked_is_available_updated(is_available: bool):
for callback in is_available_updated_callbacks:
callback(is_available)
mock_api.add_is_on_updated_callback.side_effect = (
mocked_add_is_on_updated_callback
)
mock_api.add_current_app_updated_callback.side_effect = (
mocked_add_current_app_updated_callback
)
mock_api.add_volume_info_updated_callback.side_effect = (
mocked_add_volume_info_updated_callback
)
mock_api.add_is_available_updated_callback.side_effect = (
mocked_add_is_available_updated_callbacks
)
mock_api._on_is_on_updated.side_effect = mocked_is_on_updated
mock_api._on_current_app_updated.side_effect = mocked_current_app_updated
mock_api._on_volume_info_updated.side_effect = mocked_volume_info_updated
mock_api._on_is_available_updated.side_effect = mocked_is_available_updated
yield mock_api
@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return the default mocked config entry."""
return MockConfigEntry(
title="My Android TV",
domain=DOMAIN,
data={"host": "1.2.3.4", "name": "My Android TV", "mac": "1A:2B:3C:4D:5E:6F"},
unique_id="1a:2b:3c:4d:5e:6f",
state=ConfigEntryState.NOT_LOADED,
)

View File

@@ -0,0 +1,14 @@
# serializer version: 1
# name: test_diagnostics
dict({
'api_device_info': dict({
'manufacturer': 'My Android TV manufacturer',
'model': 'My Android TV model',
}),
'config_entry_data': dict({
'host': '**REDACTED**',
'mac': '**REDACTED**',
'name': 'My Android TV',
}),
})
# ---

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,29 @@
"""Tests for the diagnostics data provided by the Android TV Remote integration."""
from unittest.mock import MagicMock
from syrupy.assertion import SnapshotAssertion
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator
async def test_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_config_entry: MockConfigEntry,
mock_api: MagicMock,
snapshot: SnapshotAssertion,
) -> None:
"""Test diagnostics."""
mock_api.is_on = True
mock_api.current_app = "some app"
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert (
await get_diagnostics_for_config_entry(hass, hass_client, mock_config_entry)
== snapshot
)

View File

@@ -0,0 +1,107 @@
"""Tests for the Android TV Remote integration."""
from collections.abc import Callable
from unittest.mock import AsyncMock, MagicMock
from androidtvremote2 import CannotConnect, InvalidAuth
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def test_load_unload_config_entry(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test the Android TV Remote configuration entry loading/unloading."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED
assert mock_api.async_connect.call_count == 1
assert mock_api.keep_reconnecting.call_count == 1
await hass.config_entries.async_unload(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
assert mock_api.disconnect.call_count == 1
async def test_config_entry_not_ready(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test the Android TV Remote configuration entry not ready."""
mock_api.async_connect = AsyncMock(side_effect=CannotConnect())
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
assert mock_api.async_connect.call_count == 1
assert mock_api.keep_reconnecting.call_count == 0
async def test_config_entry_reauth_at_setup(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test the Android TV Remote configuration entry needs reauth at setup."""
mock_api.async_connect = AsyncMock(side_effect=InvalidAuth())
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
assert any(mock_config_entry.async_get_active_flows(hass, {"reauth"}))
assert mock_api.async_connect.call_count == 1
assert mock_api.keep_reconnecting.call_count == 0
async def test_config_entry_reauth_while_reconnecting(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test the Android TV Remote configuration entry needs reauth while reconnecting."""
invalid_auth_callback: Callable | None = None
def mocked_keep_reconnecting(callback: Callable):
nonlocal invalid_auth_callback
invalid_auth_callback = callback
mock_api.keep_reconnecting.side_effect = mocked_keep_reconnecting
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED
assert not any(mock_config_entry.async_get_active_flows(hass, {"reauth"}))
assert mock_api.async_connect.call_count == 1
assert mock_api.keep_reconnecting.call_count == 1
assert invalid_auth_callback is not None
invalid_auth_callback()
await hass.async_block_till_done()
assert any(mock_config_entry.async_get_active_flows(hass, {"reauth"}))
async def test_disconnect_on_stop(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test we close the connection with the Android TV when Home Assistants stops."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.LOADED
assert mock_api.async_connect.call_count == 1
assert mock_api.keep_reconnecting.call_count == 1
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
await hass.async_block_till_done()
assert mock_api.disconnect.call_count == 1

View File

@@ -0,0 +1,413 @@
"""Tests for the Android TV Remote remote platform."""
from unittest.mock import MagicMock, call
from androidtvremote2 import ConnectionClosed
import pytest
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from tests.common import MockConfigEntry
from tests.typing import WebSocketGenerator
MEDIA_PLAYER_ENTITY = "media_player.my_android_tv"
async def test_media_player_receives_push_updates(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test the Android TV Remote media player receives push updates and state is updated."""
mock_config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry(
mock_config_entry,
options={"apps": {"com.google.android.youtube.tv": {"app_name": "YouTube"}}},
)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
mock_api._on_is_on_updated(False)
assert hass.states.is_state(MEDIA_PLAYER_ENTITY, STATE_OFF)
mock_api._on_is_on_updated(True)
assert hass.states.is_state(MEDIA_PLAYER_ENTITY, STATE_ON)
mock_api._on_current_app_updated("com.google.android.tvlauncher")
assert (
hass.states.get(MEDIA_PLAYER_ENTITY).attributes.get("app_id")
== "com.google.android.tvlauncher"
)
assert (
hass.states.get(MEDIA_PLAYER_ENTITY).attributes.get("app_name")
== "com.google.android.tvlauncher"
)
mock_api._on_current_app_updated("com.google.android.youtube.tv")
assert (
hass.states.get(MEDIA_PLAYER_ENTITY).attributes.get("app_id")
== "com.google.android.youtube.tv"
)
assert hass.states.get(MEDIA_PLAYER_ENTITY).attributes.get("app_name") == "YouTube"
mock_api._on_volume_info_updated({"level": 35, "muted": False, "max": 100})
assert hass.states.get(MEDIA_PLAYER_ENTITY).attributes.get("volume_level") == 0.35
mock_api._on_volume_info_updated({"level": 50, "muted": True, "max": 100})
assert hass.states.get(MEDIA_PLAYER_ENTITY).attributes.get("volume_level") == 0.50
assert hass.states.get(MEDIA_PLAYER_ENTITY).attributes.get("is_volume_muted")
mock_api._on_volume_info_updated({"level": 0, "muted": False, "max": 0})
assert hass.states.get(MEDIA_PLAYER_ENTITY).attributes.get("volume_level") is None
assert (
hass.states.get(MEDIA_PLAYER_ENTITY).attributes.get("is_volume_muted") is None
)
mock_api._on_is_available_updated(False)
assert hass.states.is_state(MEDIA_PLAYER_ENTITY, STATE_UNAVAILABLE)
mock_api._on_is_available_updated(True)
assert hass.states.is_state(MEDIA_PLAYER_ENTITY, STATE_ON)
async def test_media_player_toggles(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test the Android TV Remote media player toggles."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.services.async_call(
"media_player",
"turn_off",
{"entity_id": MEDIA_PLAYER_ENTITY},
blocking=True,
)
mock_api._on_is_on_updated(False)
mock_api.send_key_command.assert_called_with("POWER", "SHORT")
await hass.services.async_call(
"media_player",
"turn_on",
{"entity_id": MEDIA_PLAYER_ENTITY},
blocking=True,
)
mock_api._on_is_on_updated(True)
mock_api.send_key_command.assert_called_with("POWER", "SHORT")
async def test_media_player_volume(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test the Android TV Remote media player up/down/mute volume."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.services.async_call(
"media_player",
"volume_up",
{"entity_id": MEDIA_PLAYER_ENTITY},
blocking=True,
)
mock_api._on_volume_info_updated({"level": 10, "muted": False, "max": 100})
mock_api.send_key_command.assert_called_with("VOLUME_UP", "SHORT")
await hass.services.async_call(
"media_player",
"volume_down",
{"entity_id": MEDIA_PLAYER_ENTITY},
blocking=True,
)
mock_api._on_volume_info_updated({"level": 9, "muted": False, "max": 100})
mock_api.send_key_command.assert_called_with("VOLUME_DOWN", "SHORT")
await hass.services.async_call(
"media_player",
"volume_mute",
{"entity_id": MEDIA_PLAYER_ENTITY, "is_volume_muted": True},
blocking=True,
)
mock_api._on_volume_info_updated({"level": 9, "muted": True, "max": 100})
mock_api.send_key_command.assert_called_with("VOLUME_MUTE", "SHORT")
await hass.services.async_call(
"media_player",
"volume_mute",
{"entity_id": MEDIA_PLAYER_ENTITY, "is_volume_muted": False},
blocking=True,
)
mock_api._on_volume_info_updated({"level": 9, "muted": False, "max": 100})
mock_api.send_key_command.assert_called_with("VOLUME_MUTE", "SHORT")
async def test_media_player_controls(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test the Android TV Remote media player play/pause/stop/next/prev."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.services.async_call(
"media_player",
"media_play",
{"entity_id": MEDIA_PLAYER_ENTITY},
blocking=True,
)
mock_api.send_key_command.assert_called_with("MEDIA_PLAY", "SHORT")
await hass.services.async_call(
"media_player",
"media_pause",
{"entity_id": MEDIA_PLAYER_ENTITY},
blocking=True,
)
mock_api.send_key_command.assert_called_with("MEDIA_PAUSE", "SHORT")
await hass.services.async_call(
"media_player",
"media_play_pause",
{"entity_id": MEDIA_PLAYER_ENTITY},
blocking=True,
)
mock_api.send_key_command.assert_called_with("MEDIA_PLAY_PAUSE", "SHORT")
await hass.services.async_call(
"media_player",
"media_stop",
{"entity_id": MEDIA_PLAYER_ENTITY},
blocking=True,
)
mock_api.send_key_command.assert_called_with("MEDIA_STOP", "SHORT")
await hass.services.async_call(
"media_player",
"media_previous_track",
{"entity_id": MEDIA_PLAYER_ENTITY},
blocking=True,
)
mock_api.send_key_command.assert_called_with("MEDIA_PREVIOUS", "SHORT")
await hass.services.async_call(
"media_player",
"media_next_track",
{"entity_id": MEDIA_PLAYER_ENTITY},
blocking=True,
)
mock_api.send_key_command.assert_called_with("MEDIA_NEXT", "SHORT")
async def test_media_player_play_media(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test the Android TV Remote media player play_media."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.services.async_call(
"media_player",
"play_media",
{
"entity_id": MEDIA_PLAYER_ENTITY,
"media_content_type": "channel",
"media_content_id": "45",
},
blocking=True,
)
assert mock_api.send_key_command.mock_calls == [
call("4"),
call("5"),
]
# Test that set channel task has been canceled
mock_api.send_key_command.reset_mock()
await hass.services.async_call(
"media_player",
"play_media",
{
"entity_id": MEDIA_PLAYER_ENTITY,
"media_content_type": "channel",
"media_content_id": "7777",
},
blocking=False,
)
# Give background task time to run
await hass.async_block_till_done()
await hass.services.async_call(
"media_player",
"play_media",
{
"entity_id": MEDIA_PLAYER_ENTITY,
"media_content_type": "channel",
"media_content_id": "11",
},
blocking=True,
)
await hass.async_block_till_done()
# 4 7s should be sent
# 2 1s should be sent
assert mock_api.send_key_command.call_count == 6
await hass.services.async_call(
"media_player",
"play_media",
{
"entity_id": MEDIA_PLAYER_ENTITY,
"media_content_type": "url",
"media_content_id": "https://www.youtube.com",
},
blocking=True,
)
mock_api.send_launch_app_command.assert_called_with("https://www.youtube.com")
await hass.services.async_call(
"media_player",
"play_media",
{
"entity_id": MEDIA_PLAYER_ENTITY,
"media_content_type": "app",
"media_content_id": "tv.twitch.android.app",
},
blocking=True,
)
mock_api.send_launch_app_command.assert_called_with("tv.twitch.android.app")
with pytest.raises(ValueError):
await hass.services.async_call(
"media_player",
"play_media",
{
"entity_id": MEDIA_PLAYER_ENTITY,
"media_content_type": "channel",
"media_content_id": "abc",
},
blocking=True,
)
with pytest.raises(ValueError):
await hass.services.async_call(
"media_player",
"play_media",
{
"entity_id": MEDIA_PLAYER_ENTITY,
"media_content_type": "music",
"media_content_id": "invalid",
},
blocking=True,
)
async def test_browse_media(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
mock_config_entry: MockConfigEntry,
mock_api: MagicMock,
) -> None:
"""Test the Android TV Remote media player browse media."""
new_options = {
"apps": {
"com.google.android.youtube.tv": {
"app_name": "YouTube",
"app_icon": "https://www.youtube.com/icon.png",
},
"org.xbmc.kodi": {"app_name": "Kodi"},
}
}
mock_config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry(mock_config_entry, options=new_options)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
client = await hass_ws_client()
await client.send_json(
{
"id": 1,
"type": "media_player/browse_media",
"entity_id": MEDIA_PLAYER_ENTITY,
}
)
response = await client.receive_json()
assert response["success"]
assert response["result"] == {
"title": "Applications",
"media_class": "directory",
"media_content_type": "apps",
"media_content_id": "apps",
"children_media_class": "app",
"can_play": False,
"can_expand": True,
"thumbnail": None,
"not_shown": 0,
"children": [
{
"title": "YouTube",
"media_class": "app",
"media_content_type": "app",
"media_content_id": "com.google.android.youtube.tv",
"children_media_class": None,
"can_play": False,
"can_expand": False,
"thumbnail": "https://www.youtube.com/icon.png",
},
{
"title": "Kodi",
"media_class": "app",
"media_content_type": "app",
"media_content_id": "org.xbmc.kodi",
"children_media_class": None,
"can_play": False,
"can_expand": False,
"thumbnail": "",
},
],
}
async def test_media_player_connection_closed(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test media_player raise HomeAssistantError if ConnectionClosed."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
mock_api.send_key_command.side_effect = ConnectionClosed()
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
"media_player",
"media_pause",
{"entity_id": MEDIA_PLAYER_ENTITY},
blocking=True,
)
mock_api.send_launch_app_command.side_effect = ConnectionClosed()
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
"media_player",
"play_media",
{
"entity_id": MEDIA_PLAYER_ENTITY,
"media_content_type": "channel",
"media_content_id": "1",
},
blocking=True,
)

View File

@@ -0,0 +1,207 @@
"""Tests for the Android TV Remote remote platform."""
from unittest.mock import MagicMock, call
from androidtvremote2 import ConnectionClosed
import pytest
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from tests.common import MockConfigEntry
REMOTE_ENTITY = "remote.my_android_tv"
async def test_remote_receives_push_updates(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test the Android TV Remote receives push updates and state is updated."""
new_options = {"apps": {"com.google.android.youtube.tv": {"app_name": "YouTube"}}}
mock_config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry(mock_config_entry, options=new_options)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
mock_api._on_is_on_updated(False)
assert hass.states.is_state(REMOTE_ENTITY, STATE_OFF)
mock_api._on_is_on_updated(True)
assert hass.states.is_state(REMOTE_ENTITY, STATE_ON)
mock_api._on_current_app_updated("activity1")
assert (
hass.states.get(REMOTE_ENTITY).attributes.get("current_activity") == "activity1"
)
mock_api._on_current_app_updated("com.google.android.youtube.tv")
assert (
hass.states.get(REMOTE_ENTITY).attributes.get("current_activity") == "YouTube"
)
mock_api._on_is_available_updated(False)
assert hass.states.is_state(REMOTE_ENTITY, STATE_UNAVAILABLE)
mock_api._on_is_available_updated(True)
assert hass.states.is_state(REMOTE_ENTITY, STATE_ON)
async def test_remote_toggles(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test the Android TV Remote toggles."""
new_options = {"apps": {"com.google.android.youtube.tv": {"app_name": "YouTube"}}}
mock_config_entry.add_to_hass(hass)
hass.config_entries.async_update_entry(mock_config_entry, options=new_options)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.services.async_call(
"remote",
"turn_off",
{"entity_id": REMOTE_ENTITY},
blocking=True,
)
mock_api._on_is_on_updated(False)
mock_api.send_key_command.assert_called_with("POWER", "SHORT")
await hass.services.async_call(
"remote",
"turn_on",
{"entity_id": REMOTE_ENTITY},
blocking=True,
)
mock_api._on_is_on_updated(True)
mock_api.send_key_command.assert_called_with("POWER", "SHORT")
assert mock_api.send_key_command.call_count == 2
await hass.services.async_call(
"remote",
"turn_on",
{"entity_id": REMOTE_ENTITY, "activity": "activity1"},
blocking=True,
)
mock_api.send_key_command.send_launch_app_command("activity1")
assert mock_api.send_key_command.call_count == 2
assert mock_api.send_launch_app_command.call_count == 1
await hass.services.async_call(
"remote",
"turn_on",
{"entity_id": REMOTE_ENTITY, "activity": "YouTube"},
blocking=True,
)
mock_api.send_key_command.send_launch_app_command("com.google.android.youtube.tv")
assert mock_api.send_key_command.call_count == 2
assert mock_api.send_launch_app_command.call_count == 2
async def test_remote_send_command(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test remote.send_command service."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.services.async_call(
"remote",
"send_command",
{
"entity_id": REMOTE_ENTITY,
"command": "DPAD_LEFT",
"num_repeats": 2,
"delay_secs": 0.01,
},
blocking=True,
)
mock_api.send_key_command.assert_called_with("DPAD_LEFT", "SHORT")
assert mock_api.send_key_command.call_count == 2
async def test_remote_send_command_multiple(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test remote.send_command service with multiple commands."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.services.async_call(
"remote",
"send_command",
{
"entity_id": REMOTE_ENTITY,
"command": ["DPAD_LEFT", "DPAD_UP"],
"delay_secs": 0.01,
},
blocking=True,
)
assert mock_api.send_key_command.mock_calls == [
call("DPAD_LEFT", "SHORT"),
call("DPAD_UP", "SHORT"),
]
async def test_remote_send_command_with_hold_secs(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test remote.send_command service with hold_secs."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
await hass.services.async_call(
"remote",
"send_command",
{
"entity_id": REMOTE_ENTITY,
"command": "DPAD_RIGHT",
"delay_secs": 0.01,
"hold_secs": 0.01,
},
blocking=True,
)
assert mock_api.send_key_command.mock_calls == [
call("DPAD_RIGHT", "START_LONG"),
call("DPAD_RIGHT", "END_LONG"),
]
async def test_remote_connection_closed(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_api: MagicMock
) -> None:
"""Test commands raise HomeAssistantError if ConnectionClosed."""
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
assert mock_config_entry.state is ConfigEntryState.LOADED
mock_api.send_key_command.side_effect = ConnectionClosed()
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
"remote",
"send_command",
{
"entity_id": REMOTE_ENTITY,
"command": "DPAD_LEFT",
"delay_secs": 0.01,
},
blocking=True,
)
assert mock_api.send_key_command.mock_calls == [call("DPAD_LEFT", "SHORT")]
mock_api.send_launch_app_command.side_effect = ConnectionClosed()
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
"remote",
"turn_on",
{"entity_id": REMOTE_ENTITY, "activity": "activity1"},
blocking=True,
)
assert mock_api.send_launch_app_command.mock_calls == [call("activity1")]