mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
Log deprecation warning when template.Template
is created without hass
(#125142)
* Log deprecation warning when template.Template is created without hass * Improve docstring
This commit is contained in:
parent
70b811096c
commit
54cf52069e
@ -495,10 +495,26 @@ class Template:
|
||||
)
|
||||
|
||||
def __init__(self, template: str, hass: HomeAssistant | None = None) -> None:
|
||||
"""Instantiate a template."""
|
||||
"""Instantiate a template.
|
||||
|
||||
Note: A valid hass instance should always be passed in. The hass parameter
|
||||
will be non optional in Home Assistant Core 2025.10.
|
||||
"""
|
||||
# pylint: disable-next=import-outside-toplevel
|
||||
from .frame import report
|
||||
|
||||
if not isinstance(template, str):
|
||||
raise TypeError("Expected template to be a string")
|
||||
|
||||
if not hass:
|
||||
report(
|
||||
(
|
||||
"creates a template object without passing hass, "
|
||||
"which will stop working in HA Core 2025.10"
|
||||
),
|
||||
error_if_core=False,
|
||||
)
|
||||
|
||||
self.template: str = template.strip()
|
||||
self._compiled_code: CodeType | None = None
|
||||
self._compiled: jinja2.Template | None = None
|
||||
|
@ -6281,3 +6281,20 @@ def test_unzip(hass: HomeAssistant, col, expected) -> None:
|
||||
).async_render({"col": col})
|
||||
== expected
|
||||
)
|
||||
|
||||
|
||||
def test_warn_no_hass(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> None:
|
||||
"""Test deprecation warning when instantiating Template without hass."""
|
||||
|
||||
message = "Detected code that creates a template object without passing hass"
|
||||
template.Template("blah")
|
||||
assert message in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
template.Template("blah", None)
|
||||
assert message in caplog.text
|
||||
caplog.clear()
|
||||
|
||||
template.Template("blah", hass)
|
||||
assert message not in caplog.text
|
||||
caplog.clear()
|
||||
|
Loading…
x
Reference in New Issue
Block a user