Add tests

This commit is contained in:
epenet 2025-07-10 09:44:56 +00:00
parent 415122a074
commit a6a39103d7
3 changed files with 43 additions and 4 deletions

View File

@ -138,7 +138,7 @@ def deprecated_function[**_P, _R](
return deprecated_decorator
def deprecate_hass_binding[**_P, _T](
def deprecated_hass_binding[**_P, _T](
breaks_in_ha_version: str | None = None,
) -> Callable[[Callable[_P, _T]], Callable[_P, _T]]:
"""Decorate function to indicate that first argument hass will be ignored."""
@ -148,7 +148,7 @@ def deprecate_hass_binding[**_P, _T](
def _inner(*args: _P.args, **kwargs: _P.kwargs) -> _T:
from homeassistant.core import HomeAssistant # noqa: PLC0415
if isinstance(args[0], HomeAssistant):
if len(args) > 0 and isinstance(args[0], HomeAssistant):
_print_deprecation_warning_internal(
"hass",
func.__module__,

View File

@ -57,7 +57,7 @@ from . import (
template,
translation,
)
from .deprecation import deprecate_hass_binding, deprecated_class, deprecated_function
from .deprecation import deprecated_class, deprecated_function, deprecated_hass_binding
from .selector import TargetSelector
from .typing import ConfigType, TemplateVarsType, VolDictType, VolSchemaType
@ -986,7 +986,7 @@ def async_register_admin_service(
)
@deprecate_hass_binding(breaks_in_ha_version="2026.2")
@deprecated_hass_binding(breaks_in_ha_version="2026.2")
@callback
def verify_domain_control(
domain: str,

View File

@ -17,6 +17,7 @@ from homeassistant.helpers.deprecation import (
check_if_deprecated_constant,
deprecated_class,
deprecated_function,
deprecated_hass_binding,
deprecated_substitute,
dir_with_deprecated_constants,
get_deprecated,
@ -638,3 +639,41 @@ def test_enum_with_deprecated_members_integration_not_found(
TestEnum.DOGS # noqa: B018
assert len(caplog.record_tuples) == 0
@pytest.mark.parametrize(
("breaks_in_ha_version", "extra_msg"),
[
(None, ""),
("2099.1", " It will be removed in HA Core 2099.1."),
],
)
def test_deprecated_hass_binding(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
breaks_in_ha_version: str | None,
extra_msg: str,
) -> None:
"""Test deprecated_hass_binding decorator."""
calls = []
@deprecated_hass_binding(breaks_in_ha_version=breaks_in_ha_version)
def mock_deprecated_function():
calls.append("called")
mock_deprecated_function()
assert (
"The deprecated argument hass was passed to mock_deprecated_function."
f"{extra_msg}"
" Use mock_deprecated_function without hass argument instead"
) not in caplog.text
assert len(calls) == 1
mock_deprecated_function(hass)
assert (
"The deprecated argument hass was passed to mock_deprecated_function."
f"{extra_msg}"
" Use mock_deprecated_function without hass argument instead"
) in caplog.text
assert len(calls) == 2