mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Name the caller in the reload_config_entry target deprecation warning (#193810)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e2c9aeb827
commit
22b7ece217
@@ -21,7 +21,12 @@ from homeassistant.core import (
|
||||
split_entity_id,
|
||||
)
|
||||
from homeassistant.exceptions import HomeAssistantError, Unauthorized, UnknownUser
|
||||
from homeassistant.helpers import config_validation as cv, recorder, restore_state
|
||||
from homeassistant.helpers import (
|
||||
config_validation as cv,
|
||||
entity_registry as er,
|
||||
recorder,
|
||||
restore_state,
|
||||
)
|
||||
from homeassistant.helpers.entity_component import async_update_entity
|
||||
from homeassistant.helpers.service import (
|
||||
async_extract_config_entry_ids,
|
||||
@@ -32,6 +37,7 @@ from homeassistant.helpers.target import (
|
||||
async_extract_referenced_entity_ids,
|
||||
)
|
||||
from homeassistant.helpers.template import async_load_custom_templates
|
||||
from homeassistant.helpers.trace import trace_id_get
|
||||
|
||||
from .const import (
|
||||
DATA_STOP_HANDLER,
|
||||
@@ -239,6 +245,23 @@ async def _async_handle_reload_templates(call: ServiceCall) -> None:
|
||||
await async_load_custom_templates(hass)
|
||||
|
||||
|
||||
async def _async_get_caller(call: ServiceCall) -> str:
|
||||
"""Return the automation, script or user that runs the current action."""
|
||||
hass = call.hass
|
||||
if (trace_id := trace_id_get()) is not None:
|
||||
# The trace key is the domain and the unique ID of the automation or script
|
||||
domain, _, unique_id = trace_id[0].partition(".")
|
||||
entity_registry = er.async_get(hass)
|
||||
if entity_id := entity_registry.async_get_entity_id(domain, domain, unique_id):
|
||||
return f"from {entity_id}"
|
||||
return "from an automation or script without an ID"
|
||||
if (user_id := call.context.user_id) and (
|
||||
user := await hass.auth.async_get_user(user_id)
|
||||
):
|
||||
return f"by user {user.name} outside of automations and scripts"
|
||||
return "from an action outside of automations and scripts"
|
||||
|
||||
|
||||
async def _async_handle_reload_config_entry(call: ServiceCall) -> None:
|
||||
"""Service handler for reloading a config entry."""
|
||||
hass = call.hass
|
||||
@@ -249,7 +272,10 @@ async def _async_handle_reload_config_entry(call: ServiceCall) -> None:
|
||||
_LOGGER.warning(
|
||||
"Reloading a config entry by target is deprecated and will stop "
|
||||
"working in Home Assistant 2027.4, please specify the config entry "
|
||||
"to reload in the 'entry_id' parameter instead"
|
||||
"to reload in the 'entry_id' parameter instead. Called %s with "
|
||||
"target %s",
|
||||
await _async_get_caller(call),
|
||||
{key: value for key, value in call.data.items() if key in cv.TARGET_FIELDS},
|
||||
)
|
||||
reload_entries.update(await async_extract_config_entry_ids(call))
|
||||
if not reload_entries:
|
||||
|
||||
@@ -476,7 +476,9 @@ async def test_reload_config_entry_by_entity_id(
|
||||
assert (
|
||||
"Reloading a config entry by target is deprecated and will stop working in "
|
||||
"Home Assistant 2027.4, please specify the config entry to reload in the "
|
||||
"'entry_id' parameter instead"
|
||||
"'entry_id' parameter instead. Called from an action outside of automations "
|
||||
"and scripts with target {'entity_id': "
|
||||
f"['{reg_entity1.entity_id}', '{reg_entity2.entity_id}']}}"
|
||||
) in caplog.text
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
@@ -488,6 +490,137 @@ async def test_reload_config_entry_by_entity_id(
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("domain", "config", "entity_id", "service", "caller"),
|
||||
[
|
||||
pytest.param(
|
||||
"automation",
|
||||
{
|
||||
"automation": {
|
||||
"id": "kitchen_reload_id",
|
||||
"alias": "Kitchen reload",
|
||||
"triggers": {"trigger": "event", "event_type": "test_event"},
|
||||
"actions": {
|
||||
"action": "homeassistant.reload_config_entry",
|
||||
"target": {"entity_id": "binary_sensor.kitchen"},
|
||||
},
|
||||
}
|
||||
},
|
||||
"automation.kitchen_reload",
|
||||
"trigger",
|
||||
"from automation.kitchen_reload",
|
||||
id="automation",
|
||||
),
|
||||
pytest.param(
|
||||
"automation",
|
||||
{
|
||||
"automation": {
|
||||
"alias": "Kitchen reload",
|
||||
"triggers": {"trigger": "event", "event_type": "test_event"},
|
||||
"actions": {
|
||||
"action": "homeassistant.reload_config_entry",
|
||||
"target": {"entity_id": "binary_sensor.kitchen"},
|
||||
},
|
||||
}
|
||||
},
|
||||
"automation.kitchen_reload",
|
||||
"trigger",
|
||||
"from an automation or script without an ID",
|
||||
id="automation_without_id",
|
||||
),
|
||||
pytest.param(
|
||||
"script",
|
||||
{
|
||||
"script": {
|
||||
"reload_kitchen": {
|
||||
"sequence": {
|
||||
"action": "homeassistant.reload_config_entry",
|
||||
"target": {"entity_id": "binary_sensor.kitchen"},
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"script.reload_kitchen",
|
||||
"turn_on",
|
||||
"from script.reload_kitchen",
|
||||
id="script",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_reload_config_entry_by_target_logs_caller(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
domain: str,
|
||||
config: dict[str, Any],
|
||||
entity_id: str,
|
||||
service: str,
|
||||
caller: str,
|
||||
) -> None:
|
||||
"""Test the deprecation warning names the automation or script."""
|
||||
await async_setup_component(hass, DOMAIN, {})
|
||||
entry = MockConfigEntry(domain="mockdomain")
|
||||
entry.add_to_hass(hass)
|
||||
entity_registry.async_get_or_create(
|
||||
"binary_sensor",
|
||||
"mockdomain",
|
||||
"kitchen",
|
||||
suggested_object_id="kitchen",
|
||||
config_entry=entry,
|
||||
)
|
||||
assert await async_setup_component(hass, domain, config)
|
||||
|
||||
with patch(
|
||||
"homeassistant.config_entries.ConfigEntries.async_reload",
|
||||
return_value=None,
|
||||
) as mock_reload:
|
||||
await hass.services.async_call(
|
||||
domain, service, {"entity_id": entity_id}, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
mock_reload.assert_called_once_with(entry.entry_id)
|
||||
assert (
|
||||
f"Called {caller} with target {{'entity_id': ['binary_sensor.kitchen']}}"
|
||||
) in caplog.text
|
||||
|
||||
|
||||
async def test_reload_config_entry_by_target_logs_user(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
hass_admin_user: MockUser,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test the deprecation warning names the user of a direct action call."""
|
||||
await async_setup_component(hass, DOMAIN, {})
|
||||
entry = MockConfigEntry(domain="mockdomain")
|
||||
entry.add_to_hass(hass)
|
||||
entity_registry.async_get_or_create(
|
||||
"binary_sensor",
|
||||
"mockdomain",
|
||||
"kitchen",
|
||||
suggested_object_id="kitchen",
|
||||
config_entry=entry,
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.config_entries.ConfigEntries.async_reload",
|
||||
return_value=None,
|
||||
):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
"reload_config_entry",
|
||||
{"entity_id": "binary_sensor.kitchen"},
|
||||
blocking=True,
|
||||
context=ha.Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
|
||||
assert (
|
||||
"Called by user Mock User outside of automations and scripts with target "
|
||||
"{'entity_id': ['binary_sensor.kitchen']}"
|
||||
) in caplog.text
|
||||
|
||||
|
||||
async def test_reload_config_entry_by_entry_id(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
|
||||
Reference in New Issue
Block a user