Use entity registry id in arcam_fmj device triggers (#95391)

This commit is contained in:
Erik Montnemery 2023-06-27 18:25:17 +02:00 committed by GitHub
parent 5a90a44233
commit bff4b0c79c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 8 deletions

View file

@ -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,

View file

@ -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": {