From 5a90a44233ececb83bc0f9f5281816dd21090f77 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 27 Jun 2023 18:24:53 +0200 Subject: [PATCH] Use entity registry id in kodi device triggers (#95392) --- .../components/kodi/device_trigger.py | 19 +++++-- tests/components/kodi/test_device_trigger.py | 57 +++++++++++++++++-- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/kodi/device_trigger.py b/homeassistant/components/kodi/device_trigger.py index c15c415bd9c3..3f931d1e2641 100644 --- a/homeassistant/components/kodi/device_trigger.py +++ b/homeassistant/components/kodi/device_trigger.py @@ -23,7 +23,7 @@ TRIGGER_TYPES = {"turn_on", "turn_off"} 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), } ) @@ -44,7 +44,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", } ) @@ -53,7 +53,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_off", } ) @@ -69,15 +69,24 @@ def _attach_trigger( event_type, trigger_info: TriggerInfo, ): + registry = er.async_get(hass) + entity_id = er.async_resolve_entity_id(registry, config[ATTR_ENTITY_ID]) trigger_data = trigger_info["trigger_data"] job = HassJob(action) @callback def _handle_event(event: Event): - if event.data[ATTR_ENTITY_ID] == config[CONF_ENTITY_ID]: + if event.data[ATTR_ENTITY_ID] == entity_id: hass.async_run_hass_job( job, - {"trigger": {**trigger_data, **config, "description": event_type}}, + { + "trigger": { + **trigger_data, + **config, + "description": event_type, + "entity_id": entity_id, + } + }, event.context, ) diff --git a/tests/components/kodi/test_device_trigger.py b/tests/components/kodi/test_device_trigger.py index 59d8a5148cf0..3181f9781128 100644 --- a/tests/components/kodi/test_device_trigger.py +++ b/tests/components/kodi/test_device_trigger.py @@ -48,7 +48,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( MP_DOMAIN, DOMAIN, "5678", device_id=device_entry.id ) expected_triggers = [ @@ -57,7 +57,7 @@ async def test_get_triggers( "domain": DOMAIN, "type": trigger, "device_id": device_entry.id, - "entity_id": f"{MP_DOMAIN}.kodi_5678", + "entity_id": entity_entry.id, "metadata": {"secondary": False}, } for trigger in ["turn_off", "turn_on"] @@ -74,9 +74,11 @@ async def test_get_triggers( async def test_if_fires_on_state_change( - hass: HomeAssistant, calls, kodi_media_player + hass: HomeAssistant, entity_registry: er.EntityRegistry, calls, kodi_media_player ) -> None: """Test for turn_on and turn_off triggers firing.""" + entry = entity_registry.async_get(kodi_media_player) + assert await async_setup_component( hass, automation.DOMAIN, @@ -87,7 +89,7 @@ async def test_if_fires_on_state_change( "platform": "device", "domain": DOMAIN, "device_id": "", - "entity_id": kodi_media_player, + "entity_id": entry.id, "type": "turn_on", }, "action": { @@ -104,7 +106,7 @@ async def test_if_fires_on_state_change( "platform": "device", "domain": DOMAIN, "device_id": "", - "entity_id": kodi_media_player, + "entity_id": entry.id, "type": "turn_off", }, "action": { @@ -142,3 +144,48 @@ async def test_if_fires_on_state_change( await hass.async_block_till_done() assert len(calls) == 2 assert calls[1].data["some"] == f"turn_off - {kodi_media_player} - 0" + + +async def test_if_fires_on_state_change_legacy( + hass: HomeAssistant, entity_registry: er.EntityRegistry, calls, kodi_media_player +) -> None: + """Test for turn_on and turn_off triggers firing.""" + entry = entity_registry.async_get(kodi_media_player) + + 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": { + "service": "test.automation", + "data_template": { + "some": ( + "turn_on - {{ trigger.entity_id }} - {{ trigger.id}}" + ) + }, + }, + }, + ] + }, + ) + await hass.async_block_till_done() + + await hass.services.async_call( + MP_DOMAIN, + "turn_on", + {"entity_id": kodi_media_player}, + blocking=True, + ) + + await hass.async_block_till_done() + assert len(calls) == 1 + assert calls[0].data["some"] == f"turn_on - {kodi_media_player} - 0"