"""The tests for Arcam FMJ Receiver control device triggers."""

from arcam.fmj.state import State

from homeassistant.components import automation
from homeassistant.components.arcam_fmj.const import DOMAIN
from homeassistant.components.device_automation import DeviceAutomationType
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.setup import async_setup_component

from .conftest import MOCK_ENTITY_ID

from tests.common import MockConfigEntry, async_get_device_automations


async def test_get_triggers(
    hass: HomeAssistant,
    device_registry: dr.DeviceRegistry,
    entity_registry: er.EntityRegistry,
) -> None:
    """Test we get the expected triggers from a arcam_fmj."""
    config_entry = MockConfigEntry(domain=DOMAIN, data={})
    config_entry.add_to_hass(hass)
    device_entry = device_registry.async_get_or_create(
        config_entry_id=config_entry.entry_id,
        identifiers={(DOMAIN, "host", 1234)},
    )
    entity_entry = entity_registry.async_get_or_create(
        "media_player", DOMAIN, "5678", device_id=device_entry.id
    )
    expected_triggers = [
        {
            "platform": "device",
            "domain": DOMAIN,
            "type": "turn_on",
            "device_id": device_entry.id,
            "entity_id": entity_entry.id,
            "metadata": {"secondary": False},
        },
    ]
    triggers = await async_get_device_automations(
        hass, DeviceAutomationType.TRIGGER, device_entry.id
    )

    # Test triggers are either arcam_fmj specific or media_player entity triggers
    triggers = await async_get_device_automations(
        hass, DeviceAutomationType.TRIGGER, device_entry.id
    )
    for expected_trigger in expected_triggers:
        assert expected_trigger in triggers
    for trigger in triggers:
        assert trigger in expected_triggers or trigger["domain"] == "media_player"


async def test_if_fires_on_turn_on_request(
    hass: HomeAssistant,
    entity_registry: er.EntityRegistry,
    service_calls: list[ServiceCall],
    player_setup,
    state_1: State,
) -> None:
    """Test for turn_on and turn_off triggers firing."""
    entry = entity_registry.async_get(MOCK_ENTITY_ID)

    state_1.get_power.return_value = None

    assert await async_setup_component(
        hass,
        automation.DOMAIN,
        {
            automation.DOMAIN: [
                {
                    "trigger": {
                        "platform": "device",
                        "domain": DOMAIN,
                        "device_id": entry.device_id,
                        "entity_id": entry.id,
                        "type": "turn_on",
                    },
                    "action": {
                        "service": "test.automation",
                        "data_template": {
                            "some": "{{ trigger.entity_id }}",
                            "id": "{{ trigger.id }}",
                        },
                    },
                }
            ]
        },
    )

    await hass.services.async_call(
        "media_player",
        "turn_on",
        {"entity_id": MOCK_ENTITY_ID},
        blocking=True,
    )

    await hass.async_block_till_done()
    assert len(service_calls) == 2
    assert service_calls[1].data["some"] == MOCK_ENTITY_ID
    assert service_calls[1].data["id"] == 0


async def test_if_fires_on_turn_on_request_legacy(
    hass: HomeAssistant,
    entity_registry: er.EntityRegistry,
    service_calls: list[ServiceCall],
    player_setup,
    state_1: State,
) -> None:
    """Test for turn_on and turn_off triggers firing."""
    entry = entity_registry.async_get(MOCK_ENTITY_ID)

    state_1.get_power.return_value = None

    assert await async_setup_component(
        hass,
        automation.DOMAIN,
        {
            automation.DOMAIN: [
                {
                    "trigger": {
                        "platform": "device",
                        "domain": DOMAIN,
                        "device_id": entry.device_id,
                        "entity_id": entry.entity_id,
                        "type": "turn_on",
                    },
                    "action": {
                        "service": "test.automation",
                        "data_template": {
                            "some": "{{ trigger.entity_id }}",
                            "id": "{{ trigger.id }}",
                        },
                    },
                }
            ]
        },
    )

    await hass.services.async_call(
        "media_player",
        "turn_on",
        {"entity_id": MOCK_ENTITY_ID},
        blocking=True,
    )

    await hass.async_block_till_done()
    assert len(service_calls) == 2
    assert service_calls[1].data["some"] == MOCK_ENTITY_ID
    assert service_calls[1].data["id"] == 0
