Files
core/tests/components/webostv/test_trigger.py
T

274 lines
8.0 KiB
Python

"""The tests for LG webOS TV automation triggers."""
from typing import Any
from unittest.mock import MagicMock, patch
import probatio
import pytest
from pytest_homeassistant.common import MockEntity, MockEntityPlatform
from homeassistant.components import automation
from homeassistant.components.webostv import DOMAIN
from homeassistant.const import ATTR_DEVICE_ID, ATTR_ENTITY_ID, SERVICE_RELOAD
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.trigger import async_validate_trigger_config
from homeassistant.setup import async_setup_component
from . import setup_webostv
from .const import ENTITY_ID, FAKE_UUID
async def test_webostv_turn_on_trigger_device_id(
hass: HomeAssistant,
service_calls: list[ServiceCall],
device_registry: dr.DeviceRegistry,
client: MagicMock,
) -> None:
"""Test for turn_on triggers by device_id firing."""
client.mac_addresses = {}
entry = await setup_webostv(hass)
device = device_registry.async_get_device_by_identifier(
(DOMAIN, FAKE_UUID), entry.entry_id
)
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "webostv.turn_on",
"device_id": device.id,
},
"action": {
"service": "test.automation",
"data_template": {
"some": device.id,
"id": "{{ trigger.id }}",
},
},
},
],
},
)
await hass.services.async_call(
"media_player",
"turn_on",
{"entity_id": ENTITY_ID},
blocking=True,
)
assert len(service_calls) == 2
assert service_calls[1].data["some"] == device.id
assert service_calls[1].data["id"] == 0
with patch("homeassistant.config.load_yaml_dict", return_value={}):
await hass.services.async_call(automation.DOMAIN, SERVICE_RELOAD, blocking=True)
service_calls.clear()
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
"media_player",
"turn_on",
{"entity_id": ENTITY_ID},
blocking=True,
)
assert len(service_calls) == 1
async def test_webostv_turn_on_trigger_entity_id(
hass: HomeAssistant, service_calls: list[ServiceCall], client
) -> None:
"""Test for turn_on triggers by entity_id firing."""
await setup_webostv(hass)
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "webostv.turn_on",
"entity_id": ENTITY_ID,
},
"action": {
"service": "test.automation",
"data_template": {
"some": ENTITY_ID,
"id": "{{ trigger.id }}",
},
},
},
],
},
)
await hass.services.async_call(
"media_player",
"turn_on",
{"entity_id": ENTITY_ID},
blocking=True,
)
assert len(service_calls) == 2
assert service_calls[1].data["some"] == ENTITY_ID
assert service_calls[1].data["id"] == 0
@pytest.mark.parametrize("option", [ATTR_DEVICE_ID, ATTR_ENTITY_ID])
@pytest.mark.usefixtures("client")
async def test_webostv_turn_on_trigger_options(
hass: HomeAssistant,
service_calls: list[ServiceCall],
device_registry: dr.DeviceRegistry,
option: str,
) -> None:
"""Test the options form the automation editor saves."""
entry = await setup_webostv(hass)
device = device_registry.async_get_device_by_identifier(
(DOMAIN, FAKE_UUID), entry.entry_id
)
value = {ATTR_DEVICE_ID: device.id, ATTR_ENTITY_ID: ENTITY_ID}[option]
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"trigger": "webostv.turn_on",
"options": {option: [value]},
},
"action": {
"service": "test.automation",
"data_template": {"some": "{{ trigger.device_id }}"},
},
},
],
},
)
await hass.services.async_call(
"media_player",
"turn_on",
{"entity_id": ENTITY_ID},
blocking=True,
)
assert len(service_calls) == 2
assert service_calls[1].data["some"] == device.id
async def test_unknown_trigger_platform_type(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, client
) -> None:
"""Test unknown trigger platform type."""
await setup_webostv(hass)
await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "webostv.unknown",
"entity_id": ENTITY_ID,
},
"action": {
"service": "test.automation",
"data_template": {
"some": ENTITY_ID,
"id": "{{ trigger.id }}",
},
},
},
],
},
)
assert "Invalid trigger 'webostv.unknown' specified" in caplog.text
async def test_trigger_invalid_entity_id(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, client
) -> None:
"""Test turn on trigger using invalid entity_id."""
await setup_webostv(hass)
platform = MockEntityPlatform(hass)
invalid_entity = f"{DOMAIN}.invalid"
await platform.async_add_entities([MockEntity(name=invalid_entity)])
await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "webostv.turn_on",
"entity_id": invalid_entity,
},
"action": {
"service": "test.automation",
"data_template": {
"some": ENTITY_ID,
"id": "{{ trigger.id }}",
},
},
},
],
},
)
assert f"Entity {invalid_entity} is not a valid webOS TV entity" in caplog.text
@pytest.mark.parametrize(
("config", "expected_errors"),
[
pytest.param(
{"platform": "webostv.turn_on", "entity_id": "bad id"},
[
(
[0, "entity_id"],
"Entity ID bad id is an invalid entity ID at '[0].entity_id'",
)
],
id="top_level",
),
pytest.param(
{"platform": "webostv.turn_on", "options": {"entity_id": "bad id"}},
[
(
[0, "options", "entity_id"],
"Entity ID bad id is an invalid entity ID at '[0].options.entity_id'",
)
],
id="options",
),
],
)
async def test_turn_on_trigger_error_paths(
hass: HomeAssistant,
config: dict[str, Any],
expected_errors: list[tuple[list[Any], str]],
) -> None:
"""Test errors are reported where the fields were set."""
with pytest.raises(probatio.MultipleInvalid) as exc_info:
await async_validate_trigger_config(hass, [config])
assert [(err.path, str(err)) for err in exc_info.value.errors] == expected_errors