Use async_capture_events to avoid running in executor (#47111)
This commit is contained in:
parent
b1898cc176
commit
7c2545af6e
8 changed files with 39 additions and 51 deletions
|
@ -26,7 +26,7 @@ from homeassistant.components.media_player.const import (
|
||||||
import homeassistant.components.vacuum as vacuum
|
import homeassistant.components.vacuum as vacuum
|
||||||
from homeassistant.config import async_process_ha_core_config
|
from homeassistant.config import async_process_ha_core_config
|
||||||
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
|
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
|
||||||
from homeassistant.core import Context, callback
|
from homeassistant.core import Context
|
||||||
from homeassistant.helpers import entityfilter
|
from homeassistant.helpers import entityfilter
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
@ -42,17 +42,13 @@ from . import (
|
||||||
reported_properties,
|
reported_properties,
|
||||||
)
|
)
|
||||||
|
|
||||||
from tests.common import async_mock_service
|
from tests.common import async_capture_events, async_mock_service
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def events(hass):
|
def events(hass):
|
||||||
"""Fixture that catches alexa events."""
|
"""Fixture that catches alexa events."""
|
||||||
events = []
|
return async_capture_events(hass, smart_home.EVENT_ALEXA_SMART_HOME)
|
||||||
hass.bus.async_listen(
|
|
||||||
smart_home.EVENT_ALEXA_SMART_HOME, callback(lambda e: events.append(e))
|
|
||||||
)
|
|
||||||
yield events
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|
|
@ -30,7 +30,12 @@ from homeassistant.exceptions import HomeAssistantError, Unauthorized
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from tests.common import assert_setup_component, async_mock_service, mock_restore_cache
|
from tests.common import (
|
||||||
|
assert_setup_component,
|
||||||
|
async_capture_events,
|
||||||
|
async_mock_service,
|
||||||
|
mock_restore_cache,
|
||||||
|
)
|
||||||
from tests.components.logbook.test_init import MockLazyEventPartialState
|
from tests.components.logbook.test_init import MockLazyEventPartialState
|
||||||
|
|
||||||
|
|
||||||
|
@ -496,10 +501,7 @@ async def test_reload_config_service(hass, calls, hass_admin_user, hass_read_onl
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
assert calls[0].data.get("event") == "test_event"
|
assert calls[0].data.get("event") == "test_event"
|
||||||
|
|
||||||
test_reload_event = []
|
test_reload_event = async_capture_events(hass, EVENT_AUTOMATION_RELOADED)
|
||||||
hass.bus.async_listen(
|
|
||||||
EVENT_AUTOMATION_RELOADED, lambda event: test_reload_event.append(event)
|
|
||||||
)
|
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.config.load_yaml_config_file",
|
"homeassistant.config.load_yaml_config_file",
|
||||||
|
|
|
@ -12,7 +12,7 @@ from homeassistant.core import callback
|
||||||
from homeassistant.helpers import discovery
|
from homeassistant.helpers import discovery
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.common import assert_setup_component
|
from tests.common import assert_setup_component, async_capture_events
|
||||||
|
|
||||||
CONFIG = {notify.DOMAIN: {"platform": "demo"}}
|
CONFIG = {notify.DOMAIN: {"platform": "demo"}}
|
||||||
|
|
||||||
|
@ -20,9 +20,7 @@ CONFIG = {notify.DOMAIN: {"platform": "demo"}}
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def events(hass):
|
def events(hass):
|
||||||
"""Fixture that catches notify events."""
|
"""Fixture that catches notify events."""
|
||||||
events = []
|
return async_capture_events(hass, demo.EVENT_NOTIFY)
|
||||||
hass.bus.async_listen(demo.EVENT_NOTIFY, callback(lambda e: events.append(e)))
|
|
||||||
yield events
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|
|
@ -30,7 +30,12 @@ from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from . import BASIC_CONFIG, MockConfig
|
from . import BASIC_CONFIG, MockConfig
|
||||||
|
|
||||||
from tests.common import mock_area_registry, mock_device_registry, mock_registry
|
from tests.common import (
|
||||||
|
async_capture_events,
|
||||||
|
mock_area_registry,
|
||||||
|
mock_device_registry,
|
||||||
|
mock_registry,
|
||||||
|
)
|
||||||
|
|
||||||
REQ_ID = "ff36a3cc-ec34-11e6-b1a0-64510650abcf"
|
REQ_ID = "ff36a3cc-ec34-11e6-b1a0-64510650abcf"
|
||||||
|
|
||||||
|
@ -77,8 +82,7 @@ async def test_sync_message(hass):
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
events = []
|
events = async_capture_events(hass, EVENT_SYNC_RECEIVED)
|
||||||
hass.bus.async_listen(EVENT_SYNC_RECEIVED, events.append)
|
|
||||||
|
|
||||||
result = await sh.async_handle_message(
|
result = await sh.async_handle_message(
|
||||||
hass,
|
hass,
|
||||||
|
@ -192,8 +196,7 @@ async def test_sync_in_area(area_on_device, hass, registries):
|
||||||
|
|
||||||
config = MockConfig(should_expose=lambda _: True, entity_config={})
|
config = MockConfig(should_expose=lambda _: True, entity_config={})
|
||||||
|
|
||||||
events = []
|
events = async_capture_events(hass, EVENT_SYNC_RECEIVED)
|
||||||
hass.bus.async_listen(EVENT_SYNC_RECEIVED, events.append)
|
|
||||||
|
|
||||||
result = await sh.async_handle_message(
|
result = await sh.async_handle_message(
|
||||||
hass,
|
hass,
|
||||||
|
@ -295,8 +298,7 @@ async def test_query_message(hass):
|
||||||
light3.entity_id = "light.color_temp_light"
|
light3.entity_id = "light.color_temp_light"
|
||||||
await light3.async_update_ha_state()
|
await light3.async_update_ha_state()
|
||||||
|
|
||||||
events = []
|
events = async_capture_events(hass, EVENT_QUERY_RECEIVED)
|
||||||
hass.bus.async_listen(EVENT_QUERY_RECEIVED, events.append)
|
|
||||||
|
|
||||||
result = await sh.async_handle_message(
|
result = await sh.async_handle_message(
|
||||||
hass,
|
hass,
|
||||||
|
@ -387,11 +389,8 @@ async def test_execute(hass):
|
||||||
"light", "turn_off", {"entity_id": "light.ceiling_lights"}, blocking=True
|
"light", "turn_off", {"entity_id": "light.ceiling_lights"}, blocking=True
|
||||||
)
|
)
|
||||||
|
|
||||||
events = []
|
events = async_capture_events(hass, EVENT_COMMAND_RECEIVED)
|
||||||
hass.bus.async_listen(EVENT_COMMAND_RECEIVED, events.append)
|
service_events = async_capture_events(hass, EVENT_CALL_SERVICE)
|
||||||
|
|
||||||
service_events = []
|
|
||||||
hass.bus.async_listen(EVENT_CALL_SERVICE, service_events.append)
|
|
||||||
|
|
||||||
result = await sh.async_handle_message(
|
result = await sh.async_handle_message(
|
||||||
hass,
|
hass,
|
||||||
|
@ -570,8 +569,7 @@ async def test_raising_error_trait(hass):
|
||||||
{ATTR_MIN_TEMP: 15, ATTR_MAX_TEMP: 30, ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS},
|
{ATTR_MIN_TEMP: 15, ATTR_MAX_TEMP: 30, ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS},
|
||||||
)
|
)
|
||||||
|
|
||||||
events = []
|
events = async_capture_events(hass, EVENT_COMMAND_RECEIVED)
|
||||||
hass.bus.async_listen(EVENT_COMMAND_RECEIVED, events.append)
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
result = await sh.async_handle_message(
|
result = await sh.async_handle_message(
|
||||||
|
@ -660,8 +658,7 @@ async def test_unavailable_state_does_sync(hass):
|
||||||
light._available = False # pylint: disable=protected-access
|
light._available = False # pylint: disable=protected-access
|
||||||
await light.async_update_ha_state()
|
await light.async_update_ha_state()
|
||||||
|
|
||||||
events = []
|
events = async_capture_events(hass, EVENT_SYNC_RECEIVED)
|
||||||
hass.bus.async_listen(EVENT_SYNC_RECEIVED, events.append)
|
|
||||||
|
|
||||||
result = await sh.async_handle_message(
|
result = await sh.async_handle_message(
|
||||||
hass,
|
hass,
|
||||||
|
|
|
@ -54,7 +54,7 @@ from homeassistant.util import color
|
||||||
|
|
||||||
from . import BASIC_CONFIG, MockConfig
|
from . import BASIC_CONFIG, MockConfig
|
||||||
|
|
||||||
from tests.common import async_mock_service
|
from tests.common import async_capture_events, async_mock_service
|
||||||
|
|
||||||
REQ_ID = "ff36a3cc-ec34-11e6-b1a0-64510650abcf"
|
REQ_ID = "ff36a3cc-ec34-11e6-b1a0-64510650abcf"
|
||||||
|
|
||||||
|
@ -84,8 +84,7 @@ async def test_brightness_light(hass):
|
||||||
|
|
||||||
assert trt.query_attributes() == {"brightness": 95}
|
assert trt.query_attributes() == {"brightness": 95}
|
||||||
|
|
||||||
events = []
|
events = async_capture_events(hass, EVENT_CALL_SERVICE)
|
||||||
hass.bus.async_listen(EVENT_CALL_SERVICE, events.append)
|
|
||||||
|
|
||||||
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
|
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
|
||||||
await trt.execute(
|
await trt.execute(
|
||||||
|
|
|
@ -8,17 +8,14 @@ from homeassistant.components.homeassistant import scene as ha_scene
|
||||||
from homeassistant.components.homeassistant.scene import EVENT_SCENE_RELOADED
|
from homeassistant.components.homeassistant.scene import EVENT_SCENE_RELOADED
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.common import async_mock_service
|
from tests.common import async_capture_events, async_mock_service
|
||||||
|
|
||||||
|
|
||||||
async def test_reload_config_service(hass):
|
async def test_reload_config_service(hass):
|
||||||
"""Test the reload config service."""
|
"""Test the reload config service."""
|
||||||
assert await async_setup_component(hass, "scene", {})
|
assert await async_setup_component(hass, "scene", {})
|
||||||
|
|
||||||
test_reloaded_event = []
|
test_reloaded_event = async_capture_events(hass, EVENT_SCENE_RELOADED)
|
||||||
hass.bus.async_listen(
|
|
||||||
EVENT_SCENE_RELOADED, lambda event: test_reloaded_event.append(event)
|
|
||||||
)
|
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.config.load_yaml_config_file",
|
"homeassistant.config.load_yaml_config_file",
|
||||||
|
|
|
@ -5,7 +5,8 @@ from pyhap.accessory_driver import AccessoryDriver
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.homekit.const import EVENT_HOMEKIT_CHANGED
|
from homeassistant.components.homekit.const import EVENT_HOMEKIT_CHANGED
|
||||||
from homeassistant.core import callback as ha_callback
|
|
||||||
|
from tests.common import async_capture_events
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -24,8 +25,4 @@ def hk_driver(loop):
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def events(hass):
|
def events(hass):
|
||||||
"""Yield caught homekit_changed events."""
|
"""Yield caught homekit_changed events."""
|
||||||
events = []
|
return async_capture_events(hass, EVENT_HOMEKIT_CHANGED)
|
||||||
hass.bus.async_listen(
|
|
||||||
EVENT_HOMEKIT_CHANGED, ha_callback(lambda e: events.append(e))
|
|
||||||
)
|
|
||||||
yield events
|
|
||||||
|
|
|
@ -10,10 +10,14 @@ from homeassistant.components.shelly.const import (
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
EVENT_SHELLY_CLICK,
|
EVENT_SHELLY_CLICK,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback as ha_callback
|
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, async_mock_service, mock_device_registry
|
from tests.common import (
|
||||||
|
MockConfigEntry,
|
||||||
|
async_capture_events,
|
||||||
|
async_mock_service,
|
||||||
|
mock_device_registry,
|
||||||
|
)
|
||||||
|
|
||||||
MOCK_SETTINGS = {
|
MOCK_SETTINGS = {
|
||||||
"name": "Test name",
|
"name": "Test name",
|
||||||
|
@ -81,9 +85,7 @@ def calls(hass):
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def events(hass):
|
def events(hass):
|
||||||
"""Yield caught shelly_click events."""
|
"""Yield caught shelly_click events."""
|
||||||
ha_events = []
|
return async_capture_events(hass, EVENT_SHELLY_CLICK)
|
||||||
hass.bus.async_listen(EVENT_SHELLY_CLICK, ha_callback(ha_events.append))
|
|
||||||
yield ha_events
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue