Use entity registry id in kodi device triggers (#95392)

This commit is contained in:
Erik Montnemery 2023-06-27 18:24:53 +02:00 committed by GitHub
parent 1fec407a24
commit 5a90a44233
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 10 deletions

View File

@ -23,7 +23,7 @@ TRIGGER_TYPES = {"turn_on", "turn_off"}
TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend( 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), vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
} }
) )
@ -44,7 +44,7 @@ async def async_get_triggers(
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id, CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN, CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id, CONF_ENTITY_ID: entry.id,
CONF_TYPE: "turn_on", CONF_TYPE: "turn_on",
} }
) )
@ -53,7 +53,7 @@ async def async_get_triggers(
CONF_PLATFORM: "device", CONF_PLATFORM: "device",
CONF_DEVICE_ID: device_id, CONF_DEVICE_ID: device_id,
CONF_DOMAIN: DOMAIN, CONF_DOMAIN: DOMAIN,
CONF_ENTITY_ID: entry.entity_id, CONF_ENTITY_ID: entry.id,
CONF_TYPE: "turn_off", CONF_TYPE: "turn_off",
} }
) )
@ -69,15 +69,24 @@ def _attach_trigger(
event_type, event_type,
trigger_info: TriggerInfo, 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"] trigger_data = trigger_info["trigger_data"]
job = HassJob(action) job = HassJob(action)
@callback @callback
def _handle_event(event: Event): 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( hass.async_run_hass_job(
job, job,
{"trigger": {**trigger_data, **config, "description": event_type}}, {
"trigger": {
**trigger_data,
**config,
"description": event_type,
"entity_id": entity_id,
}
},
event.context, event.context,
) )

View File

@ -48,7 +48,7 @@ async def test_get_triggers(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
identifiers={(DOMAIN, "host", 1234)}, 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 MP_DOMAIN, DOMAIN, "5678", device_id=device_entry.id
) )
expected_triggers = [ expected_triggers = [
@ -57,7 +57,7 @@ async def test_get_triggers(
"domain": DOMAIN, "domain": DOMAIN,
"type": trigger, "type": trigger,
"device_id": device_entry.id, "device_id": device_entry.id,
"entity_id": f"{MP_DOMAIN}.kodi_5678", "entity_id": entity_entry.id,
"metadata": {"secondary": False}, "metadata": {"secondary": False},
} }
for trigger in ["turn_off", "turn_on"] for trigger in ["turn_off", "turn_on"]
@ -74,9 +74,11 @@ async def test_get_triggers(
async def test_if_fires_on_state_change( 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: ) -> None:
"""Test for turn_on and turn_off triggers firing.""" """Test for turn_on and turn_off triggers firing."""
entry = entity_registry.async_get(kodi_media_player)
assert await async_setup_component( assert await async_setup_component(
hass, hass,
automation.DOMAIN, automation.DOMAIN,
@ -87,7 +89,7 @@ async def test_if_fires_on_state_change(
"platform": "device", "platform": "device",
"domain": DOMAIN, "domain": DOMAIN,
"device_id": "", "device_id": "",
"entity_id": kodi_media_player, "entity_id": entry.id,
"type": "turn_on", "type": "turn_on",
}, },
"action": { "action": {
@ -104,7 +106,7 @@ async def test_if_fires_on_state_change(
"platform": "device", "platform": "device",
"domain": DOMAIN, "domain": DOMAIN,
"device_id": "", "device_id": "",
"entity_id": kodi_media_player, "entity_id": entry.id,
"type": "turn_off", "type": "turn_off",
}, },
"action": { "action": {
@ -142,3 +144,48 @@ async def test_if_fires_on_state_change(
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(calls) == 2 assert len(calls) == 2
assert calls[1].data["some"] == f"turn_off - {kodi_media_player} - 0" 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"