Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,20 @@ async def _extract_entities_from_target(
return entities


def _get_action_service(config: dict[str, Any]) -> str | None:
"""Return the service/action name configured for an action."""
service = config.get("service", config.get("action"))
return service if isinstance(service, str) else None


def _should_skip_service_data_value(
service: str | None,
key: str,
) -> bool:
"""Return if a service data value should not be scanned for entity IDs."""
return service is not None and service.startswith("notify.") and key == "target"


async def _extract_entities_from_service_data(
hass: HomeAssistant, config: dict[str, Any]
) -> set[str]:
Expand All @@ -204,8 +218,11 @@ async def _extract_entities_from_service_data(
# data field is a template string itself
entities.update(await extract_entities_from_value(hass, data_value))
elif isinstance(data_value, dict):
service = _get_action_service(config)
# data field is a dictionary, process all its values
for value in data_value.values():
for key, value in data_value.items():
if _should_skip_service_data_value(service, key):
continue
entities.update(await extract_entities_from_value(hass, value))
return entities

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,50 @@ async def test_action_target_block(hass: HomeAssistant) -> None:
async def test_action_data_dict(hass: HomeAssistant) -> None:
"""Entities buried inside ``data`` values are captured."""
config = {
"service": "notify.notify",
"service": "light.turn_on",
"data": {"message": "hello", "target": "person.alice"},
}
assert await extract_entities_from_action_config(hass, config) == {"person.alice"}


async def test_notify_action_data_target_is_not_an_entity_reference(
hass: HomeAssistant,
) -> None:
"""Notify ``data.target`` values are service targets, not entity references."""
config = {
"action": "notify.mobile_app_phone",
"data": {
"target": "notify.old_tablet",
"message": "{{ states('sensor.temperature') }}",
},
}
assert await extract_entities_from_action_config(hass, config) == {
"sensor.temperature"
}


async def test_notify_service_data_target_is_not_an_entity_reference(
hass: HomeAssistant,
) -> None:
"""Legacy service syntax follows the same notify target rule."""
config = {
"service": "notify.mobile_app_phone",
"data": {"target": ["notify.old_tablet", "notify.old_phone"]},
}
assert await extract_entities_from_action_config(hass, config) == set()


async def test_non_notify_action_data_target_remains_entity_reference(
hass: HomeAssistant,
) -> None:
"""Non-notify service data is still scanned for entity IDs."""
config = {
"action": "calendar.create_event",
"data": {"target": "person.alice"},
}
assert await extract_entities_from_action_config(hass, config) == {"person.alice"}


async def test_action_data_as_template_string(hass: HomeAssistant) -> None:
"""A template string assigned directly to ``data`` is parsed."""
config = {
Expand Down
Loading