From 3a1389c3b4cbd97ae04b38ac65f8eef095ba5bc3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 27 May 2023 18:45:35 -0500 Subject: [PATCH] Switch some frequently called call_laters to call_at (#93659) * Switch some frequently called call_laters to call_at call_at is a bit faster than call_later since call_later is a wrapper around call_at. We call at lot of these at startup so it helps a bit when we are resource constrained * update test * update test --- homeassistant/helpers/entity.py | 4 ++-- homeassistant/helpers/entity_platform.py | 4 ++-- tests/helpers/test_entity_platform.py | 12 +++++++----- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index 00171350594..a1435a57277 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -770,8 +770,8 @@ class Entity(ABC): await self.parallel_updates.acquire() if warning: - update_warn = hass.loop.call_later( - SLOW_UPDATE_WARNING, self._async_slow_update_warning + update_warn = hass.loop.call_at( + hass.loop.time() + SLOW_UPDATE_WARNING, self._async_slow_update_warning ) try: diff --git a/homeassistant/helpers/entity_platform.py b/homeassistant/helpers/entity_platform.py index 372f541d4bd..4adb0f212b3 100644 --- a/homeassistant/helpers/entity_platform.py +++ b/homeassistant/helpers/entity_platform.py @@ -288,8 +288,8 @@ class EntityPlatform: ) logger.info("Setting up %s", full_name) - warn_task = hass.loop.call_later( - SLOW_SETUP_WARNING, + warn_task = hass.loop.call_at( + hass.loop.time() + SLOW_SETUP_WARNING, logger.warning, "Setup of %s platform %s is taking over %s seconds.", self.domain, diff --git a/tests/helpers/test_entity_platform.py b/tests/helpers/test_entity_platform.py index 56872fe5b4b..e6b864be09c 100644 --- a/tests/helpers/test_entity_platform.py +++ b/tests/helpers/test_entity_platform.py @@ -223,16 +223,18 @@ async def test_platform_warn_slow_setup(hass: HomeAssistant) -> None: component = EntityComponent(_LOGGER, DOMAIN, hass) - with patch.object(hass.loop, "call_later") as mock_call: + with patch.object(hass.loop, "call_at") as mock_call: await component.async_setup({DOMAIN: {"platform": "platform"}}) await hass.async_block_till_done() assert mock_call.called - # mock_calls[0] is the warning message for component setup - # mock_calls[4] is the warning message for platform setup - timeout, logger_method = mock_call.mock_calls[4][1][:2] + # mock_calls[3] is the warning message for component setup + # mock_calls[10] is the warning message for platform setup + timeout, logger_method = mock_call.mock_calls[10][1][:2] - assert timeout == entity_platform.SLOW_SETUP_WARNING + assert timeout - hass.loop.time() == pytest.approx( + entity_platform.SLOW_SETUP_WARNING, 0.5 + ) assert logger_method == _LOGGER.warning assert mock_call().cancel.called