From 5512e8b7891eeaf2d61d7d6ebc840b6c187d65fb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" <nick@koston.org> Date: Thu, 14 Mar 2024 09:06:55 -1000 Subject: [PATCH] Deprecate async_run_job and async_add_job (#113260) --- homeassistant/core.py | 22 ++++++++++++++++++++++ tests/test_core.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/homeassistant/core.py b/homeassistant/core.py index ae02961c6a4..629206e75f1 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -583,6 +583,17 @@ class HomeAssistant: target: target to call. args: parameters for method to call. """ + # late import to avoid circular imports + from .helpers import frame # pylint: disable=import-outside-toplevel + + frame.report( + "calls `async_add_job`, which is deprecated and will be removed in Home " + "Assistant 2025.4; Please review " + "https://developers.home-assistant.io/blog/2024/03/13/deprecate_add_run_job" + " for replacement options", + error_if_core=False, + ) + if target is None: raise ValueError("Don't call async_add_job with None") @@ -844,6 +855,17 @@ class HomeAssistant: target: target to call. args: parameters for method to call. """ + # late import to avoid circular imports + from .helpers import frame # pylint: disable=import-outside-toplevel + + frame.report( + "calls `async_run_job`, which is deprecated and will be removed in Home " + "Assistant 2025.4; Please review " + "https://developers.home-assistant.io/blog/2024/03/13/deprecate_add_run_job" + " for replacement options", + error_if_core=False, + ) + if asyncio.iscoroutine(target): return self.async_create_task(target, eager_start=True) diff --git a/tests/test_core.py b/tests/test_core.py index d30ccdd018b..abebcef7121 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -3119,3 +3119,37 @@ async def test_async_add_import_executor_job(hass: HomeAssistant) -> None: assert await future is evt assert hass.import_executor._max_workers == 1 + + +async def test_async_run_job_deprecated( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test async_run_job warns about its deprecation.""" + + async def _test(): + pass + + hass.async_run_job(_test) + assert ( + "Detected code that calls `async_run_job`, which is deprecated " + "and will be removed in Home Assistant 2025.4; Please review " + "https://developers.home-assistant.io/blog/2024/03/13/deprecate_add_run_job" + " for replacement options" + ) in caplog.text + + +async def test_async_add_job_deprecated( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test async_add_job warns about its deprecation.""" + + async def _test(): + pass + + hass.async_add_job(_test) + assert ( + "Detected code that calls `async_add_job`, which is deprecated " + "and will be removed in Home Assistant 2025.4; Please review " + "https://developers.home-assistant.io/blog/2024/03/13/deprecate_add_run_job" + " for replacement options" + ) in caplog.text -- GitLab