From d5da0a053b59d5aa43ec36e69596a92fb594376c Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Sun, 31 Mar 2024 11:44:11 +0200 Subject: [PATCH] Deprecate `hass.helpers` (#114484) * Deprecate hass.helpers * Patch * Patch _REPORTED_INTEGRATIONS set in test * Fix test * Update version --- homeassistant/loader.py | 14 ++++++++++++++ tests/test_loader.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/homeassistant/loader.py b/homeassistant/loader.py index f462ea16886..722f3fd83c7 100644 --- a/homeassistant/loader.py +++ b/homeassistant/loader.py @@ -1550,6 +1550,20 @@ class Helpers: def __getattr__(self, helper_name: str) -> ModuleWrapper: """Fetch a helper.""" helper = importlib.import_module(f"homeassistant.helpers.{helper_name}") + + # Local import to avoid circular dependencies + from .helpers.frame import report # pylint: disable=import-outside-toplevel + + report( + ( + f"accesses hass.helpers.{helper_name}." + " This is deprecated and will stop working in Home Assistant 2024.11, it" + f" should be updated to import functions used from {helper_name} directly" + ), + error_if_core=False, + log_custom_component_only=True, + ) + wrapped = ModuleWrapper(self._hass, helper) setattr(self, helper_name, wrapped) return wrapped diff --git a/tests/test_loader.py b/tests/test_loader.py index 9e191ee9e00..4442fe5fd82 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -1779,3 +1779,34 @@ async def test_has_services(hass: HomeAssistant, enable_custom_integrations) -> assert integration.has_services is False integration = await loader.async_get_integration(hass, "test_with_services") assert integration.has_services is True + + +async def test_hass_helpers_use_reported( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_integration_frame: Mock +) -> None: + """Test that use of hass.components is reported.""" + integration_frame = frame.IntegrationFrame( + custom_integration=True, + _frame=mock_integration_frame, + integration="test_integration_frame", + module="custom_components.test_integration_frame", + relative_filename="custom_components/test_integration_frame/__init__.py", + ) + + with ( + patch.object(frame, "_REPORTED_INTEGRATIONS", new=set()), + patch( + "homeassistant.helpers.frame.get_integration_frame", + return_value=integration_frame, + ), + patch( + "homeassistant.helpers.aiohttp_client.async_get_clientsession", + return_value=None, + ), + ): + hass.helpers.aiohttp_client.async_get_clientsession() + + assert ( + "Detected that custom integration 'test_integration_frame' " + "accesses hass.helpers.aiohttp_client. This is deprecated" + ) in caplog.text