Run debouncer tasks eagerly to avoid scheduling on the event loop (#112789)
This commit is contained in:
parent
c608d1cb85
commit
a2318c26c9
4 changed files with 20 additions and 4 deletions
|
@ -109,7 +109,7 @@ class Debouncer(Generic[_R_co]):
|
||||||
|
|
||||||
assert self._job is not None
|
assert self._job is not None
|
||||||
try:
|
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
|
await task
|
||||||
finally:
|
finally:
|
||||||
self._schedule_timer()
|
self._schedule_timer()
|
||||||
|
@ -130,7 +130,7 @@ class Debouncer(Generic[_R_co]):
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
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
|
await task
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # pylint: disable=broad-except
|
||||||
self.logger.exception("Unexpected exception from %s", self.function)
|
self.logger.exception("Unexpected exception from %s", self.function)
|
||||||
|
|
|
@ -330,7 +330,7 @@ async def test_second_poll_needed(
|
||||||
inject_bluetooth_service_info(hass, GENERIC_BLUETOOTH_SERVICE_INFO_2)
|
inject_bluetooth_service_info(hass, GENERIC_BLUETOOTH_SERVICE_INFO_2)
|
||||||
|
|
||||||
await hass.async_block_till_done()
|
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()
|
cancel()
|
||||||
|
|
||||||
|
|
|
@ -413,6 +413,8 @@ async def test_group_removed(hass: HomeAssistant, mock_bridge_v1) -> None:
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
"light", "turn_on", {"entity_id": "light.group_1"}, blocking=True
|
"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
|
# 2x group update, 1x light update, 1 turn on request
|
||||||
assert len(mock_bridge_v1.mock_requests) == 4
|
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(
|
await hass.services.async_call(
|
||||||
"light", "turn_on", {"entity_id": "light.hue_lamp_1"}, blocking=True
|
"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
|
# 2x light update, 1 group update, 1 turn on request
|
||||||
assert len(mock_bridge_v1.mock_requests) == 4
|
assert len(mock_bridge_v1.mock_requests) == 4
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"""Test the Shelly config flow."""
|
"""Test the Shelly config flow."""
|
||||||
|
|
||||||
from dataclasses import replace
|
from dataclasses import replace
|
||||||
|
from datetime import timedelta
|
||||||
from ipaddress import ip_address
|
from ipaddress import ip_address
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from unittest.mock import AsyncMock, Mock, patch
|
from unittest.mock import AsyncMock, Mock, patch
|
||||||
|
@ -21,13 +22,15 @@ from homeassistant.components.shelly.const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
BLEScannerMode,
|
BLEScannerMode,
|
||||||
)
|
)
|
||||||
|
from homeassistant.components.shelly.coordinator import ENTRY_RELOAD_COOLDOWN
|
||||||
from homeassistant.config_entries import SOURCE_REAUTH
|
from homeassistant.config_entries import SOURCE_REAUTH
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from . import init_integration
|
from . import init_integration
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||||
from tests.typing import WebSocketGenerator
|
from tests.typing import WebSocketGenerator
|
||||||
|
|
||||||
DISCOVERY_INFO = zeroconf.ZeroconfServiceInfo(
|
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)
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
mock_rpc_device.mock_disconnected()
|
mock_rpc_device.mock_disconnected()
|
||||||
|
async_fire_time_changed(
|
||||||
|
hass, dt_util.utcnow() + timedelta(seconds=ENTRY_RELOAD_COOLDOWN)
|
||||||
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(mock_rpc_device.initialize.mock_calls) == 2
|
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)
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
mock_rpc_device.mock_disconnected()
|
mock_rpc_device.mock_disconnected()
|
||||||
|
async_fire_time_changed(
|
||||||
|
hass, dt_util.utcnow() + timedelta(seconds=ENTRY_RELOAD_COOLDOWN)
|
||||||
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(mock_rpc_device.initialize.mock_calls) == 2
|
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)
|
monkeypatch.setattr(mock_rpc_device, "connected", False)
|
||||||
mock_rpc_device.mock_disconnected()
|
mock_rpc_device.mock_disconnected()
|
||||||
|
async_fire_time_changed(
|
||||||
|
hass, dt_util.utcnow() + timedelta(seconds=ENTRY_RELOAD_COOLDOWN)
|
||||||
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert len(mock_rpc_device.initialize.mock_calls) == 0
|
assert len(mock_rpc_device.initialize.mock_calls) == 0
|
||||||
assert "device did not update" not in caplog.text
|
assert "device did not update" not in caplog.text
|
||||||
|
|
Loading…
Add table
Reference in a new issue