diff --git a/homeassistant/components/arcam_fmj/device_trigger.py b/homeassistant/components/arcam_fmj/device_trigger.py index d62e764f47a..ef83217ee26 100644 --- a/homeassistant/components/arcam_fmj/device_trigger.py +++ b/homeassistant/components/arcam_fmj/device_trigger.py @@ -3,7 +3,9 @@ from __future__ import annotations import voluptuous as vol -from homeassistant.components.device_automation import DEVICE_TRIGGER_BASE_SCHEMA +from homeassistant.components.device_automation import ( + DEVICE_TRIGGER_BASE_SCHEMA, +) from homeassistant.const import ( ATTR_ENTITY_ID, CONF_DEVICE_ID, @@ -22,7 +24,7 @@ from .const import DOMAIN, EVENT_TURN_ON TRIGGER_TYPES = {"turn_on"} TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend( { - vol.Required(CONF_ENTITY_ID): cv.entity_id, + vol.Required(CONF_ENTITY_ID): cv.entity_id_or_uuid, vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES), } ) @@ -43,7 +45,7 @@ async def async_get_triggers( CONF_PLATFORM: "device", CONF_DEVICE_ID: device_id, CONF_DOMAIN: DOMAIN, - CONF_ENTITY_ID: entry.entity_id, + CONF_ENTITY_ID: entry.id, CONF_TYPE: "turn_on", } ) @@ -62,7 +64,8 @@ async def async_attach_trigger( job = HassJob(action) if config[CONF_TYPE] == "turn_on": - entity_id = config[CONF_ENTITY_ID] + registry = er.async_get(hass) + entity_id = er.async_resolve_entity_id(registry, config[ATTR_ENTITY_ID]) @callback def _handle_event(event: Event) -> None: @@ -74,6 +77,7 @@ async def async_attach_trigger( **trigger_data, **config, "description": f"{DOMAIN} - {entity_id}", + "entity_id": entity_id, } }, event.context, diff --git a/tests/components/arcam_fmj/test_device_trigger.py b/tests/components/arcam_fmj/test_device_trigger.py index 012bc3a20af..7caba687ff2 100644 --- a/tests/components/arcam_fmj/test_device_trigger.py +++ b/tests/components/arcam_fmj/test_device_trigger.py @@ -38,7 +38,7 @@ async def test_get_triggers( config_entry_id=config_entry.entry_id, identifiers={(DOMAIN, "host", 1234)}, ) - entity_registry.async_get_or_create( + entity_entry = entity_registry.async_get_or_create( "media_player", DOMAIN, "5678", device_id=device_entry.id ) expected_triggers = [ @@ -47,7 +47,7 @@ async def test_get_triggers( "domain": DOMAIN, "type": "turn_on", "device_id": device_entry.id, - "entity_id": "media_player.arcam_fmj_5678", + "entity_id": entity_entry.id, "metadata": {"secondary": False}, }, ] @@ -66,9 +66,11 @@ async def test_get_triggers( async def test_if_fires_on_turn_on_request( - hass: HomeAssistant, calls, player_setup, state + hass: HomeAssistant, entity_registry: er.EntityRegistry, calls, player_setup, state ) -> None: """Test for turn_on and turn_off triggers firing.""" + entry = entity_registry.async_get(player_setup) + state.get_power.return_value = None assert await async_setup_component( @@ -81,7 +83,53 @@ async def test_if_fires_on_turn_on_request( "platform": "device", "domain": DOMAIN, "device_id": "", - "entity_id": player_setup, + "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": player_setup}, + blocking=True, + ) + + await hass.async_block_till_done() + assert len(calls) == 1 + assert calls[0].data["some"] == player_setup + assert calls[0].data["id"] == 0 + + +async def test_if_fires_on_turn_on_request_legacy( + hass: HomeAssistant, entity_registry: er.EntityRegistry, calls, player_setup, state +) -> None: + """Test for turn_on and turn_off triggers firing.""" + entry = entity_registry.async_get(player_setup) + + state.get_power.return_value = None + + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: [ + { + "trigger": { + "platform": "device", + "domain": DOMAIN, + "device_id": "", + "entity_id": entry.entity_id, "type": "turn_on", }, "action": {