From a6a39103d74fef1cb9e98128dfc496313407fa19 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 10 Jul 2025 09:44:56 +0000 Subject: [PATCH] Add tests --- homeassistant/helpers/deprecation.py | 4 +-- homeassistant/helpers/service.py | 4 +-- tests/helpers/test_deprecation.py | 39 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/homeassistant/helpers/deprecation.py b/homeassistant/helpers/deprecation.py index 922adec92c9..d7d54fd1a93 100644 --- a/homeassistant/helpers/deprecation.py +++ b/homeassistant/helpers/deprecation.py @@ -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__, diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index ca4d8561b2e..2ec25eb0542 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -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, diff --git a/tests/helpers/test_deprecation.py b/tests/helpers/test_deprecation.py index d45c9ce1546..4a5d4b8d685 100644 --- a/tests/helpers/test_deprecation.py +++ b/tests/helpers/test_deprecation.py @@ -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