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
This commit is contained in:
J. Nick Koston 2023-05-27 18:45:35 -05:00 committed by GitHub
parent 94ad9643b5
commit 3a1389c3b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 9 deletions

View File

@ -770,8 +770,8 @@ class Entity(ABC):
await self.parallel_updates.acquire() await self.parallel_updates.acquire()
if warning: if warning:
update_warn = hass.loop.call_later( update_warn = hass.loop.call_at(
SLOW_UPDATE_WARNING, self._async_slow_update_warning hass.loop.time() + SLOW_UPDATE_WARNING, self._async_slow_update_warning
) )
try: try:

View File

@ -288,8 +288,8 @@ class EntityPlatform:
) )
logger.info("Setting up %s", full_name) logger.info("Setting up %s", full_name)
warn_task = hass.loop.call_later( warn_task = hass.loop.call_at(
SLOW_SETUP_WARNING, hass.loop.time() + SLOW_SETUP_WARNING,
logger.warning, logger.warning,
"Setup of %s platform %s is taking over %s seconds.", "Setup of %s platform %s is taking over %s seconds.",
self.domain, self.domain,

View File

@ -223,16 +223,18 @@ async def test_platform_warn_slow_setup(hass: HomeAssistant) -> None:
component = EntityComponent(_LOGGER, DOMAIN, hass) 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 component.async_setup({DOMAIN: {"platform": "platform"}})
await hass.async_block_till_done() await hass.async_block_till_done()
assert mock_call.called assert mock_call.called
# mock_calls[0] is the warning message for component setup # mock_calls[3] is the warning message for component setup
# mock_calls[4] is the warning message for platform setup # mock_calls[10] is the warning message for platform setup
timeout, logger_method = mock_call.mock_calls[4][1][:2] 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 logger_method == _LOGGER.warning
assert mock_call().cancel.called assert mock_call().cancel.called