Deprecate template.attach (#124843)

This commit is contained in:
Erik Montnemery 2024-09-02 15:33:10 +02:00 committed by GitHub
parent baa876d4d9
commit df4bd721b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 8 deletions

View File

@ -81,6 +81,7 @@ from . import (
label_registry,
location as loc_helper,
)
from .deprecation import deprecated_function
from .singleton import singleton
from .translation import async_translate_state
from .typing import TemplateVarsType
@ -207,15 +208,24 @@ def async_setup(hass: HomeAssistant) -> bool:
@bind_hass
@deprecated_function(
"automatic setting of Template.hass introduced by HA Core PR #89242",
breaks_in_ha_version="2025.10",
)
def attach(hass: HomeAssistant, obj: Any) -> None:
"""Recursively attach hass to all template instances in list and dict."""
return _attach(hass, obj)
def _attach(hass: HomeAssistant, obj: Any) -> None:
"""Recursively attach hass to all template instances in list and dict."""
if isinstance(obj, list):
for child in obj:
attach(hass, child)
_attach(hass, child)
elif isinstance(obj, collections.abc.Mapping):
for child_key, child_value in obj.items():
attach(hass, child_key)
attach(hass, child_value)
_attach(hass, child_key)
_attach(hass, child_value)
elif isinstance(obj, Template):
obj.hass = hass

View File

@ -109,7 +109,6 @@ async def test_confirmable_notification(
assert len(mock_call_action.mock_calls) == 1
_hass, config, variables, _context = mock_call_action.mock_calls[0][1]
template.attach(hass, config)
rendered_config = template.render_complex(config, variables)
assert rendered_config == {

View File

@ -39,7 +39,6 @@ from homeassistant.helpers import (
device_registry as dr,
entity_registry as er,
service,
template,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.loader import async_get_integration
@ -565,9 +564,6 @@ async def test_not_mutate_input(hass: HomeAssistant) -> None:
config = cv.SERVICE_SCHEMA(config)
orig = cv.SERVICE_SCHEMA(orig)
# Only change after call is each template getting hass attached
template.attach(hass, orig)
await service.async_call_from_config(hass, config, validate_config=False)
assert orig == config