Run debouncer tasks eagerly to avoid scheduling on the event loop (#112789)

This commit is contained in:
J. Nick Koston 2024-03-10 08:37:10 -10:00 committed by GitHub
parent c608d1cb85
commit a2318c26c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 4 deletions

View file

@ -109,7 +109,7 @@ class Debouncer(Generic[_R_co]):
assert self._job is not None
try:
if task := self.hass.async_run_hass_job(self._job):
if task := self.hass.async_run_hass_job(self._job, eager_start=True):
await task
finally:
self._schedule_timer()
@ -130,7 +130,7 @@ class Debouncer(Generic[_R_co]):
return
try:
if task := self.hass.async_run_hass_job(self._job):
if task := self.hass.async_run_hass_job(self._job, eager_start=True):
await task
except Exception: # pylint: disable=broad-except
self.logger.exception("Unexpected exception from %s", self.function)

View file

@ -330,7 +330,7 @@ async def test_second_poll_needed(
inject_bluetooth_service_info(hass, GENERIC_BLUETOOTH_SERVICE_INFO_2)
await hass.async_block_till_done()
assert async_handle_update.mock_calls[-1] == call({"testdata": 1})
assert async_handle_update.mock_calls[1] == call({"testdata": 1})
cancel()

View file

@ -413,6 +413,8 @@ async def test_group_removed(hass: HomeAssistant, mock_bridge_v1) -> None:
await hass.services.async_call(
"light", "turn_on", {"entity_id": "light.group_1"}, blocking=True
)
# Wait for the group to be updated
await hass.async_block_till_done()
# 2x group update, 1x light update, 1 turn on request
assert len(mock_bridge_v1.mock_requests) == 4
@ -440,6 +442,8 @@ async def test_light_removed(hass: HomeAssistant, mock_bridge_v1) -> None:
await hass.services.async_call(
"light", "turn_on", {"entity_id": "light.hue_lamp_1"}, blocking=True
)
# Wait for the light to be updated
await hass.async_block_till_done()
# 2x light update, 1 group update, 1 turn on request
assert len(mock_bridge_v1.mock_requests) == 4

View file

@ -1,6 +1,7 @@
"""Test the Shelly config flow."""
from dataclasses import replace
from datetime import timedelta
from ipaddress import ip_address
from typing import Any
from unittest.mock import AsyncMock, Mock, patch
@ -21,13 +22,15 @@ from homeassistant.components.shelly.const import (
DOMAIN,
BLEScannerMode,
)
from homeassistant.components.shelly.coordinator import ENTRY_RELOAD_COOLDOWN
from homeassistant.config_entries import SOURCE_REAUTH
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from . import init_integration
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, async_fire_time_changed
from tests.typing import WebSocketGenerator
DISCOVERY_INFO = zeroconf.ZeroconfServiceInfo(
@ -1030,6 +1033,9 @@ async def test_zeroconf_already_configured_triggers_refresh_mac_in_name(
monkeypatch.setattr(mock_rpc_device, "connected", False)
mock_rpc_device.mock_disconnected()
async_fire_time_changed(
hass, dt_util.utcnow() + timedelta(seconds=ENTRY_RELOAD_COOLDOWN)
)
await hass.async_block_till_done()
assert len(mock_rpc_device.initialize.mock_calls) == 2
@ -1062,6 +1068,9 @@ async def test_zeroconf_already_configured_triggers_refresh(
monkeypatch.setattr(mock_rpc_device, "connected", False)
mock_rpc_device.mock_disconnected()
async_fire_time_changed(
hass, dt_util.utcnow() + timedelta(seconds=ENTRY_RELOAD_COOLDOWN)
)
await hass.async_block_till_done()
assert len(mock_rpc_device.initialize.mock_calls) == 2
@ -1100,6 +1109,9 @@ async def test_zeroconf_sleeping_device_not_triggers_refresh(
monkeypatch.setattr(mock_rpc_device, "connected", False)
mock_rpc_device.mock_disconnected()
async_fire_time_changed(
hass, dt_util.utcnow() + timedelta(seconds=ENTRY_RELOAD_COOLDOWN)
)
await hass.async_block_till_done()
assert len(mock_rpc_device.initialize.mock_calls) == 0
assert "device did not update" not in caplog.text