diff --git a/tests/common.py b/tests/common.py index 8324abc730d..eb7c7ba63ab 100644 --- a/tests/common.py +++ b/tests/common.py @@ -30,11 +30,10 @@ from homeassistant.auth import ( providers as auth_providers, ) from homeassistant.auth.permissions import system_policies -from homeassistant.components import device_automation, recorder +from homeassistant.components import device_automation from homeassistant.components.device_automation import ( # noqa: F401 _async_get_device_automation_capabilities as async_get_device_automation_capabilities, ) -from homeassistant.components.mqtt.models import ReceiveMessage from homeassistant.config import async_process_component_config from homeassistant.const import ( DEVICE_DEFAULT_NAME, @@ -372,6 +371,10 @@ def async_mock_intent(hass, intent_typ): @ha.callback def async_fire_mqtt_message(hass, topic, payload, qos=0, retain=False): """Fire the MQTT message.""" + # Local import to avoid processing MQTT modules when running a testcase + # which does not use MQTT. + from homeassistant.components.mqtt.models import ReceiveMessage + if isinstance(payload, str): payload = payload.encode("utf-8") msg = ReceiveMessage(topic, payload, qos, retain) @@ -966,11 +969,15 @@ def assert_setup_component(count, domain=None): ), f"setup_component failed, expected {count} got {res_len}: {res}" -SetupRecorderInstanceT = Callable[..., Awaitable[recorder.Recorder]] +SetupRecorderInstanceT = Callable[..., Awaitable[Any]] def init_recorder_component(hass, add_config=None, db_url="sqlite://"): """Initialize the recorder.""" + # Local import to avoid processing recorder and SQLite modules when running a + # testcase which does not use the recorder. + from homeassistant.components import recorder + config = dict(add_config) if add_config else {} if recorder.CONF_DB_URL not in config: config[recorder.CONF_DB_URL] = db_url diff --git a/tests/conftest.py b/tests/conftest.py index 740387b8a10..307f6626ba8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -34,7 +34,6 @@ from homeassistant import core as ha, loader, runner, util from homeassistant.auth.const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY from homeassistant.auth.models import Credentials from homeassistant.auth.providers import homeassistant, legacy_api_password -from homeassistant.components import mqtt, recorder from homeassistant.components.network.models import Adapter, IPv4ConfiguredAddress from homeassistant.components.websocket_api.auth import ( TYPE_AUTH, @@ -737,6 +736,10 @@ async def mqtt_mock( @asynccontextmanager async def _mqtt_mock_entry(hass, mqtt_client_mock, mqtt_config_entry_data): """Fixture to mock a delayed setup of the MQTT config entry.""" + # Local import to avoid processing MQTT modules when running a testcase + # which does not use MQTT. + from homeassistant.components import mqtt + if mqtt_config_entry_data is None: mqtt_config_entry_data = { mqtt.CONF_BROKER: "mock-broker", @@ -956,6 +959,10 @@ def hass_recorder( hass_storage, ): """Home Assistant fixture with in-memory recorder.""" + # Local import to avoid processing recorder and SQLite modules when running a + # testcase which does not use the recorder. + from homeassistant.components import recorder + original_tz = dt_util.DEFAULT_TIME_ZONE hass = get_test_home_assistant() @@ -997,6 +1004,10 @@ def hass_recorder( async def _async_init_recorder_component(hass, add_config=None, db_url=None): """Initialize the recorder asynchronously.""" + # Local import to avoid processing recorder and SQLite modules when running a + # testcase which does not use the recorder. + from homeassistant.components import recorder + config = dict(add_config) if add_config else {} if recorder.CONF_DB_URL not in config: config[recorder.CONF_DB_URL] = db_url @@ -1027,6 +1038,10 @@ async def async_setup_recorder_instance( """Yield callable to setup recorder instance.""" assert not hass_fixture_setup + # Local import to avoid processing recorder and SQLite modules when running a + # testcase which does not use the recorder. + from homeassistant.components import recorder + nightly = recorder.Recorder.async_nightly_tasks if enable_nightly_purge else None stats = recorder.Recorder.async_periodic_statistics if enable_statistics else None stats_validate = (