From 51b4d15c8cb83bb715222841aa48e83f77ef38ff Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 13 Jun 2022 10:17:10 -1000 Subject: [PATCH] Speed up mqtt tests (#73423) Co-authored-by: jbouwh Co-authored-by: Jan Bouwhuis --- .../mqtt/test_alarm_control_panel.py | 10 ++++++++ tests/components/mqtt/test_binary_sensor.py | 8 ++++++ tests/components/mqtt/test_button.py | 14 ++++++++++- tests/components/mqtt/test_camera.py | 8 ++++++ tests/components/mqtt/test_climate.py | 9 ++++++- tests/components/mqtt/test_cover.py | 8 ++++++ tests/components/mqtt/test_device_tracker.py | 11 +++++++- .../mqtt/test_device_tracker_discovery.py | 11 +++++++- tests/components/mqtt/test_device_trigger.py | 12 +++++++++ tests/components/mqtt/test_diagnostics.py | 13 +++++++++- tests/components/mqtt/test_discovery.py | 25 +++++++++++++++++++ tests/components/mqtt/test_fan.py | 8 ++++++ tests/components/mqtt/test_humidifier.py | 8 ++++++ tests/components/mqtt/test_init.py | 14 +++++++++++ tests/components/mqtt/test_legacy_vacuum.py | 9 ++++++- tests/components/mqtt/test_light.py | 8 ++++++ tests/components/mqtt/test_light_json.py | 8 ++++++ tests/components/mqtt/test_light_template.py | 8 ++++++ tests/components/mqtt/test_lock.py | 8 ++++++ tests/components/mqtt/test_number.py | 8 ++++++ tests/components/mqtt/test_scene.py | 9 ++++++- tests/components/mqtt/test_select.py | 14 ++++++++++- tests/components/mqtt/test_sensor.py | 8 ++++++ tests/components/mqtt/test_siren.py | 8 ++++++ tests/components/mqtt/test_state_vacuum.py | 8 ++++++ tests/components/mqtt/test_subscription.py | 11 +++++++- tests/components/mqtt/test_switch.py | 8 ++++++ tests/components/mqtt/test_tag.py | 8 ++++++ tests/components/mqtt/test_trigger.py | 9 ++++++- 29 files changed, 281 insertions(+), 10 deletions(-) diff --git a/tests/components/mqtt/test_alarm_control_panel.py b/tests/components/mqtt/test_alarm_control_panel.py index 4ea41aa4c64..f4a72829046 100644 --- a/tests/components/mqtt/test_alarm_control_panel.py +++ b/tests/components/mqtt/test_alarm_control_panel.py @@ -30,6 +30,7 @@ from homeassistant.const import ( STATE_ALARM_PENDING, STATE_ALARM_TRIGGERED, STATE_UNKNOWN, + Platform, ) from homeassistant.setup import async_setup_component @@ -112,6 +113,15 @@ DEFAULT_CONFIG_REMOTE_CODE_TEXT = { } +@pytest.fixture(autouse=True) +def alarm_control_panel_platform_only(): + """Only setup the alarm_control_panel platform to speed up tests.""" + with patch( + "homeassistant.components.mqtt.PLATFORMS", [Platform.ALARM_CONTROL_PANEL] + ): + yield + + async def test_fail_setup_without_state_topic(hass, mqtt_mock_entry_no_yaml_config): """Test for failing with no state topic.""" with assert_setup_component(0, alarm_control_panel.DOMAIN) as config: diff --git a/tests/components/mqtt/test_binary_sensor.py b/tests/components/mqtt/test_binary_sensor.py index 0561e9fe056..20037a88d1c 100644 --- a/tests/components/mqtt/test_binary_sensor.py +++ b/tests/components/mqtt/test_binary_sensor.py @@ -13,6 +13,7 @@ from homeassistant.const import ( STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN, + Platform, ) import homeassistant.core as ha from homeassistant.setup import async_setup_component @@ -62,6 +63,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def binary_sensor_platform_only(): + """Only setup the binary_sensor platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]): + yield + + async def test_setting_sensor_value_expires_availability_topic( hass, mqtt_mock_entry_with_yaml_config, caplog ): diff --git a/tests/components/mqtt/test_button.py b/tests/components/mqtt/test_button.py index 9fa16aa33bb..8748ef3be4d 100644 --- a/tests/components/mqtt/test_button.py +++ b/tests/components/mqtt/test_button.py @@ -5,7 +5,12 @@ from unittest.mock import patch import pytest from homeassistant.components import button -from homeassistant.const import ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, STATE_UNKNOWN +from homeassistant.const import ( + ATTR_ENTITY_ID, + ATTR_FRIENDLY_NAME, + STATE_UNKNOWN, + Platform, +) from homeassistant.setup import async_setup_component from .test_common import ( @@ -41,6 +46,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def button_platform_only(): + """Only setup the button platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BUTTON]): + yield + + @pytest.mark.freeze_time("2021-11-08 13:31:44+00:00") async def test_sending_mqtt_commands(hass, mqtt_mock_entry_with_yaml_config): """Test the sending MQTT commands.""" diff --git a/tests/components/mqtt/test_camera.py b/tests/components/mqtt/test_camera.py index f219178859b..84bf4181a2c 100644 --- a/tests/components/mqtt/test_camera.py +++ b/tests/components/mqtt/test_camera.py @@ -9,6 +9,7 @@ import pytest from homeassistant.components import camera from homeassistant.components.mqtt.camera import MQTT_CAMERA_ATTRIBUTES_BLOCKED +from homeassistant.const import Platform from homeassistant.setup import async_setup_component from .test_common import ( @@ -46,6 +47,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def camera_platform_only(): + """Only setup the camera platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.CAMERA]): + yield + + async def test_run_camera_setup( hass, hass_client_no_auth, mqtt_mock_entry_with_yaml_config ): diff --git a/tests/components/mqtt/test_climate.py b/tests/components/mqtt/test_climate.py index 35eec9aa1ff..c633f267e76 100644 --- a/tests/components/mqtt/test_climate.py +++ b/tests/components/mqtt/test_climate.py @@ -26,7 +26,7 @@ from homeassistant.components.climate.const import ( HVACMode, ) from homeassistant.components.mqtt.climate import MQTT_CLIMATE_ATTRIBUTES_BLOCKED -from homeassistant.const import ATTR_TEMPERATURE +from homeassistant.const import ATTR_TEMPERATURE, Platform from homeassistant.setup import async_setup_component from .test_common import ( @@ -106,6 +106,13 @@ DEFAULT_LEGACY_CONFIG = { } +@pytest.fixture(autouse=True) +def climate_platform_only(): + """Only setup the climate platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.CLIMATE]): + yield + + async def test_setup_params(hass, mqtt_mock_entry_with_yaml_config): """Test the initial parameters.""" assert await async_setup_component(hass, CLIMATE_DOMAIN, DEFAULT_CONFIG) diff --git a/tests/components/mqtt/test_cover.py b/tests/components/mqtt/test_cover.py index c2406aef9b4..c0d63cec1b4 100644 --- a/tests/components/mqtt/test_cover.py +++ b/tests/components/mqtt/test_cover.py @@ -43,6 +43,7 @@ from homeassistant.const import ( STATE_OPEN, STATE_OPENING, STATE_UNKNOWN, + Platform, ) from homeassistant.setup import async_setup_component @@ -83,6 +84,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def cover_platform_only(): + """Only setup the cover platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.COVER]): + yield + + async def test_state_via_state_topic(hass, mqtt_mock_entry_with_yaml_config): """Test the controlling state via topic.""" assert await async_setup_component( diff --git a/tests/components/mqtt/test_device_tracker.py b/tests/components/mqtt/test_device_tracker.py index c731280f575..a9eb9b20825 100644 --- a/tests/components/mqtt/test_device_tracker.py +++ b/tests/components/mqtt/test_device_tracker.py @@ -1,8 +1,10 @@ """The tests for the MQTT device tracker platform using configuration.yaml.""" from unittest.mock import patch +import pytest + from homeassistant.components.device_tracker.const import DOMAIN, SOURCE_TYPE_BLUETOOTH -from homeassistant.const import CONF_PLATFORM, STATE_HOME, STATE_NOT_HOME +from homeassistant.const import CONF_PLATFORM, STATE_HOME, STATE_NOT_HOME, Platform from homeassistant.setup import async_setup_component from .test_common import help_test_setup_manual_entity_from_yaml @@ -10,6 +12,13 @@ from .test_common import help_test_setup_manual_entity_from_yaml from tests.common import async_fire_mqtt_message +@pytest.fixture(autouse=True) +def device_tracker_platform_only(): + """Only setup the device_tracker platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.DEVICE_TRACKER]): + yield + + # Deprecated in HA Core 2022.6 async def test_legacy_ensure_device_tracker_platform_validation( hass, mqtt_mock_entry_with_yaml_config diff --git a/tests/components/mqtt/test_device_tracker_discovery.py b/tests/components/mqtt/test_device_tracker_discovery.py index 31853ad1dee..ac4058c9372 100644 --- a/tests/components/mqtt/test_device_tracker_discovery.py +++ b/tests/components/mqtt/test_device_tracker_discovery.py @@ -1,11 +1,13 @@ """The tests for the MQTT device_tracker discovery platform.""" +from unittest.mock import patch + import pytest from homeassistant.components import device_tracker from homeassistant.components.mqtt.const import DOMAIN as MQTT_DOMAIN from homeassistant.components.mqtt.discovery import ALREADY_DISCOVERED -from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_UNKNOWN +from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_UNKNOWN, Platform from homeassistant.setup import async_setup_component from .test_common import help_test_setting_blocked_attribute_via_mqtt_json_message @@ -21,6 +23,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def device_tracker_platform_only(): + """Only setup the device_tracker platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.DEVICE_TRACKER]): + yield + + @pytest.fixture def device_reg(hass): """Return an empty, loaded, registry.""" diff --git a/tests/components/mqtt/test_device_trigger.py b/tests/components/mqtt/test_device_trigger.py index fe08c85a853..842e1dc4106 100644 --- a/tests/components/mqtt/test_device_trigger.py +++ b/tests/components/mqtt/test_device_trigger.py @@ -1,11 +1,13 @@ """The tests for MQTT device triggers.""" import json +from unittest.mock import patch import pytest import homeassistant.components.automation as automation from homeassistant.components.device_automation import DeviceAutomationType from homeassistant.components.mqtt import _LOGGER, DOMAIN, debug_info +from homeassistant.const import Platform from homeassistant.helpers import device_registry as dr from homeassistant.helpers.trigger import async_initialize_triggers from homeassistant.setup import async_setup_component @@ -39,6 +41,16 @@ def calls(hass): return async_mock_service(hass, "test", "automation") +@pytest.fixture(autouse=True) +def binary_sensor_and_sensor_only(): + """Only setup the binary_sensor and sensor platform to speed up tests.""" + with patch( + "homeassistant.components.mqtt.PLATFORMS", + [Platform.BINARY_SENSOR, Platform.SENSOR], + ): + yield + + async def test_get_triggers( hass, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config ): diff --git a/tests/components/mqtt/test_diagnostics.py b/tests/components/mqtt/test_diagnostics.py index 65399a22f70..8cc5d0b1070 100644 --- a/tests/components/mqtt/test_diagnostics.py +++ b/tests/components/mqtt/test_diagnostics.py @@ -1,11 +1,12 @@ """Test MQTT diagnostics.""" import json -from unittest.mock import ANY +from unittest.mock import ANY, patch import pytest from homeassistant.components import mqtt +from homeassistant.const import Platform from tests.common import async_fire_mqtt_message, mock_device_registry from tests.components.diagnostics import ( @@ -31,6 +32,16 @@ default_config = { } +@pytest.fixture(autouse=True) +def device_tracker_sensor_only(): + """Only setup the device_tracker and sensor platforms to speed up tests.""" + with patch( + "homeassistant.components.mqtt.PLATFORMS", + [Platform.DEVICE_TRACKER, Platform.SENSOR], + ): + yield + + @pytest.fixture def device_reg(hass): """Return an empty, loaded, registry.""" diff --git a/tests/components/mqtt/test_discovery.py b/tests/components/mqtt/test_discovery.py index df20dc031d0..d185d3334d0 100644 --- a/tests/components/mqtt/test_discovery.py +++ b/tests/components/mqtt/test_discovery.py @@ -18,6 +18,7 @@ from homeassistant.const import ( STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN, + Platform, ) import homeassistant.core as ha from homeassistant.setup import async_setup_component @@ -65,6 +66,7 @@ async def test_subscribing_config_topic(hass, mqtt_mock_entry_no_yaml_config): assert discovery_topic + "/+/+/+/config" in topics +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) @pytest.mark.parametrize( "topic, log", [ @@ -92,6 +94,7 @@ async def test_invalid_topic(hass, mqtt_mock_entry_no_yaml_config, caplog, topic caplog.clear() +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) async def test_invalid_json(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test sending in invalid JSON.""" await mqtt_mock_entry_no_yaml_config() @@ -131,6 +134,7 @@ async def test_only_valid_components(hass, mqtt_mock_entry_no_yaml_config, caplo assert not mock_dispatcher_send.called +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) async def test_correct_config_discovery(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test sending in correct JSON.""" await mqtt_mock_entry_no_yaml_config() @@ -148,6 +152,7 @@ async def test_correct_config_discovery(hass, mqtt_mock_entry_no_yaml_config, ca assert ("binary_sensor", "bla") in hass.data[ALREADY_DISCOVERED] +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.FAN]) async def test_discover_fan(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test discovering an MQTT fan.""" await mqtt_mock_entry_no_yaml_config() @@ -165,6 +170,7 @@ async def test_discover_fan(hass, mqtt_mock_entry_no_yaml_config, caplog): assert ("fan", "bla") in hass.data[ALREADY_DISCOVERED] +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.CLIMATE]) async def test_discover_climate(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test discovering an MQTT climate component.""" await mqtt_mock_entry_no_yaml_config() @@ -184,6 +190,7 @@ async def test_discover_climate(hass, mqtt_mock_entry_no_yaml_config, caplog): assert ("climate", "bla") in hass.data[ALREADY_DISCOVERED] +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.ALARM_CONTROL_PANEL]) async def test_discover_alarm_control_panel( hass, mqtt_mock_entry_no_yaml_config, caplog ): @@ -365,6 +372,7 @@ async def test_discovery_with_object_id( assert (domain, "object bla") in hass.data[ALREADY_DISCOVERED] +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) async def test_discovery_incl_nodeid(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test sending in correct JSON with optional node_id included.""" await mqtt_mock_entry_no_yaml_config() @@ -382,6 +390,7 @@ async def test_discovery_incl_nodeid(hass, mqtt_mock_entry_no_yaml_config, caplo assert ("binary_sensor", "my_node_id bla") in hass.data[ALREADY_DISCOVERED] +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) async def test_non_duplicate_discovery(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test for a non duplicate component.""" await mqtt_mock_entry_no_yaml_config() @@ -406,6 +415,7 @@ async def test_non_duplicate_discovery(hass, mqtt_mock_entry_no_yaml_config, cap assert "Component has already been discovered: binary_sensor bla" in caplog.text +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) async def test_removal(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test removal of component through empty discovery message.""" await mqtt_mock_entry_no_yaml_config() @@ -424,6 +434,7 @@ async def test_removal(hass, mqtt_mock_entry_no_yaml_config, caplog): assert state is None +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) async def test_rediscover(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test rediscover of removed component.""" await mqtt_mock_entry_no_yaml_config() @@ -451,6 +462,7 @@ async def test_rediscover(hass, mqtt_mock_entry_no_yaml_config, caplog): assert state is not None +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) async def test_rapid_rediscover(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test immediate rediscover of removed component.""" await mqtt_mock_entry_no_yaml_config() @@ -500,6 +512,7 @@ async def test_rapid_rediscover(hass, mqtt_mock_entry_no_yaml_config, caplog): assert events[4].data["old_state"] is None +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) async def test_rapid_rediscover_unique(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test immediate rediscover of removed component.""" await mqtt_mock_entry_no_yaml_config() @@ -559,6 +572,7 @@ async def test_rapid_rediscover_unique(hass, mqtt_mock_entry_no_yaml_config, cap assert events[3].data["old_state"] is None +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) async def test_rapid_reconfigure(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test immediate reconfigure of added component.""" await mqtt_mock_entry_no_yaml_config() @@ -611,6 +625,7 @@ async def test_rapid_reconfigure(hass, mqtt_mock_entry_no_yaml_config, caplog): assert events[2].data["new_state"].attributes["friendly_name"] == "Wine" +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) async def test_duplicate_removal(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test for a non duplicate component.""" await mqtt_mock_entry_no_yaml_config() @@ -688,6 +703,7 @@ async def test_cleanup_device( ) +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SENSOR]) async def test_cleanup_device_mqtt( hass, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config ): @@ -730,6 +746,7 @@ async def test_cleanup_device_mqtt( mqtt_mock.async_publish.assert_not_called() +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SENSOR]) async def test_cleanup_device_multiple_config_entries( hass, hass_ws_client, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config ): @@ -905,6 +922,7 @@ async def test_cleanup_device_multiple_config_entries_mqtt( mqtt_mock.async_publish.assert_not_called() +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SWITCH]) async def test_discovery_expansion(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test expansion of abbreviated discovery payload.""" await mqtt_mock_entry_no_yaml_config() @@ -963,6 +981,7 @@ async def test_discovery_expansion(hass, mqtt_mock_entry_no_yaml_config, caplog) assert state.state == STATE_UNAVAILABLE +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SWITCH]) async def test_discovery_expansion_2(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test expansion of abbreviated discovery payload.""" await mqtt_mock_entry_no_yaml_config() @@ -1003,6 +1022,7 @@ async def test_discovery_expansion_2(hass, mqtt_mock_entry_no_yaml_config, caplo assert state.state == STATE_UNKNOWN +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SWITCH]) @pytest.mark.no_fail_on_log_exception async def test_discovery_expansion_3(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test expansion of broken discovery payload.""" @@ -1084,6 +1104,7 @@ async def test_discovery_expansion_without_encoding_and_value_template_1( assert state.state == STATE_UNAVAILABLE +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SWITCH]) async def test_discovery_expansion_without_encoding_and_value_template_2( hass, mqtt_mock_entry_no_yaml_config, caplog ): @@ -1190,6 +1211,7 @@ async def test_missing_discover_abbreviations( assert not missing +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SWITCH]) async def test_no_implicit_state_topic_switch( hass, mqtt_mock_entry_no_yaml_config, caplog ): @@ -1214,6 +1236,7 @@ async def test_no_implicit_state_topic_switch( assert state.state == STATE_UNKNOWN +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]) @pytest.mark.parametrize( "mqtt_config", [ @@ -1242,6 +1265,7 @@ async def test_complex_discovery_topic_prefix( assert ("binary_sensor", "node1 object1") in hass.data[ALREADY_DISCOVERED] +@patch("homeassistant.components.mqtt.PLATFORMS", []) async def test_mqtt_integration_discovery_subscribe_unsubscribe( hass, mqtt_client_mock, mqtt_mock_entry_no_yaml_config ): @@ -1283,6 +1307,7 @@ async def test_mqtt_integration_discovery_subscribe_unsubscribe( assert not mqtt_client_mock.unsubscribe.called +@patch("homeassistant.components.mqtt.PLATFORMS", []) async def test_mqtt_discovery_unsubscribe_once( hass, mqtt_client_mock, mqtt_mock_entry_no_yaml_config ): diff --git a/tests/components/mqtt/test_fan.py b/tests/components/mqtt/test_fan.py index 6a4920ef45a..b9ca5e3888d 100644 --- a/tests/components/mqtt/test_fan.py +++ b/tests/components/mqtt/test_fan.py @@ -28,6 +28,7 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_UNKNOWN, + Platform, ) from homeassistant.setup import async_setup_component @@ -74,6 +75,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def fan_platform_only(): + """Only setup the fan platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.FAN]): + yield + + async def test_fail_setup_if_no_command_topic( hass, caplog, mqtt_mock_entry_no_yaml_config ): diff --git a/tests/components/mqtt/test_humidifier.py b/tests/components/mqtt/test_humidifier.py index cbbf69e2947..e1e757762df 100644 --- a/tests/components/mqtt/test_humidifier.py +++ b/tests/components/mqtt/test_humidifier.py @@ -29,6 +29,7 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_UNKNOWN, + Platform, ) from homeassistant.setup import async_setup_component @@ -75,6 +76,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def humidifer_platform_only(): + """Only setup the humidifer platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.HUMIDIFIER]): + yield + + async def async_turn_on( hass, entity_id=ENTITY_MATCH_ALL, diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index 474bc03f876..b75def64834 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -22,6 +22,7 @@ from homeassistant.const import ( EVENT_HOMEASSISTANT_STARTED, EVENT_HOMEASSISTANT_STOP, TEMP_CELSIUS, + Platform, ) import homeassistant.core as ha from homeassistant.core import CoreState, HomeAssistant, callback @@ -51,6 +52,16 @@ class RecordCallsPartial(partial): __name__ = "RecordCallPartialTest" +@pytest.fixture(autouse=True) +def sensor_platforms_only(): + """Only setup the sensor platforms to speed up tests.""" + with patch( + "homeassistant.components.mqtt.PLATFORMS", + [Platform.SENSOR, Platform.BINARY_SENSOR], + ): + yield + + @pytest.fixture(autouse=True) def mock_storage(hass_storage): """Autouse hass_storage for the TestCase tests.""" @@ -1362,6 +1373,7 @@ async def test_setup_override_configuration(hass, caplog, tmp_path): assert calls_username_password_set[0][1] == "somepassword" +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT]) async def test_setup_manual_mqtt_with_platform_key(hass, caplog): """Test set up a manual MQTT item with a platform key.""" config = {"platform": "mqtt", "name": "test", "command_topic": "test-topic"} @@ -1372,6 +1384,7 @@ async def test_setup_manual_mqtt_with_platform_key(hass, caplog): ) +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT]) async def test_setup_manual_mqtt_with_invalid_config(hass, caplog): """Test set up a manual MQTT item with an invalid config.""" config = {"name": "test"} @@ -2013,6 +2026,7 @@ async def test_mqtt_ws_get_device_debug_info( assert response["result"] == expected_result +@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.CAMERA]) async def test_mqtt_ws_get_device_debug_info_binary( hass, device_reg, hass_ws_client, mqtt_mock_entry_no_yaml_config ): diff --git a/tests/components/mqtt/test_legacy_vacuum.py b/tests/components/mqtt/test_legacy_vacuum.py index 0371153d750..224e81781cf 100644 --- a/tests/components/mqtt/test_legacy_vacuum.py +++ b/tests/components/mqtt/test_legacy_vacuum.py @@ -29,7 +29,7 @@ from homeassistant.components.vacuum import ( ATTR_STATUS, VacuumEntityFeature, ) -from homeassistant.const import CONF_NAME, CONF_PLATFORM, STATE_OFF, STATE_ON +from homeassistant.const import CONF_NAME, CONF_PLATFORM, STATE_OFF, STATE_ON, Platform from homeassistant.setup import async_setup_component from .test_common import ( @@ -89,6 +89,13 @@ DEFAULT_CONFIG = { DEFAULT_CONFIG_2 = {vacuum.DOMAIN: {"platform": "mqtt", "name": "test"}} +@pytest.fixture(autouse=True) +def vacuum_platform_only(): + """Only setup the vacuum platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.VACUUM]): + yield + + async def test_default_supported_features(hass, mqtt_mock_entry_with_yaml_config): """Test that the correct supported features.""" assert await async_setup_component( diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index 7ca10683ed7..4d8d8f24a3c 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -209,6 +209,7 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_UNKNOWN, + Platform, ) import homeassistant.core as ha from homeassistant.setup import async_setup_component @@ -251,6 +252,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def light_platform_only(): + """Only setup the light platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT]): + yield + + async def test_fail_setup_if_no_command_topic(hass, mqtt_mock_entry_no_yaml_config): """Test if command fails with command topic.""" assert await async_setup_component( diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index 3e1eb10d717..64733b4f0f7 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -103,6 +103,7 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_UNKNOWN, + Platform, ) import homeassistant.core as ha from homeassistant.setup import async_setup_component @@ -150,6 +151,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def light_platform_only(): + """Only setup the light platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT]): + yield + + class JsonValidator: """Helper to compare JSON.""" diff --git a/tests/components/mqtt/test_light_template.py b/tests/components/mqtt/test_light_template.py index fa2ef113976..6e271d08651 100644 --- a/tests/components/mqtt/test_light_template.py +++ b/tests/components/mqtt/test_light_template.py @@ -41,6 +41,7 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_UNKNOWN, + Platform, ) import homeassistant.core as ha from homeassistant.setup import async_setup_component @@ -90,6 +91,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def light_platform_only(): + """Only setup the light platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT]): + yield + + @pytest.mark.parametrize( "test_config", [ diff --git a/tests/components/mqtt/test_lock.py b/tests/components/mqtt/test_lock.py index 0353702152f..1bf4183e60f 100644 --- a/tests/components/mqtt/test_lock.py +++ b/tests/components/mqtt/test_lock.py @@ -18,6 +18,7 @@ from homeassistant.const import ( ATTR_ASSUMED_STATE, ATTR_ENTITY_ID, ATTR_SUPPORTED_FEATURES, + Platform, ) from homeassistant.setup import async_setup_component @@ -58,6 +59,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def lock_platform_only(): + """Only setup the lock platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LOCK]): + yield + + async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_config): """Test the controlling state via topic.""" assert await async_setup_component( diff --git a/tests/components/mqtt/test_number.py b/tests/components/mqtt/test_number.py index 648499b8751..ea79c5cd7aa 100644 --- a/tests/components/mqtt/test_number.py +++ b/tests/components/mqtt/test_number.py @@ -23,6 +23,7 @@ from homeassistant.const import ( ATTR_ASSUMED_STATE, ATTR_ENTITY_ID, ATTR_UNIT_OF_MEASUREMENT, + Platform, ) import homeassistant.core as ha from homeassistant.setup import async_setup_component @@ -64,6 +65,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def number_platform_only(): + """Only setup the number platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.NUMBER]): + yield + + async def test_run_number_setup(hass, mqtt_mock_entry_with_yaml_config): """Test that it fetches the given payload.""" topic = "test/number" diff --git a/tests/components/mqtt/test_scene.py b/tests/components/mqtt/test_scene.py index 305389ba72d..3036565dad5 100644 --- a/tests/components/mqtt/test_scene.py +++ b/tests/components/mqtt/test_scene.py @@ -5,7 +5,7 @@ from unittest.mock import patch import pytest from homeassistant.components import scene -from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON, STATE_UNKNOWN +from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_ON, STATE_UNKNOWN, Platform import homeassistant.core as ha from homeassistant.setup import async_setup_component @@ -34,6 +34,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def scene_platform_only(): + """Only setup the scene platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SCENE]): + yield + + async def test_sending_mqtt_commands(hass, mqtt_mock_entry_with_yaml_config): """Test the sending MQTT commands.""" fake_state = ha.State("scene.test", STATE_UNKNOWN) diff --git a/tests/components/mqtt/test_select.py b/tests/components/mqtt/test_select.py index 5b02c4f3a31..c22bd43b86f 100644 --- a/tests/components/mqtt/test_select.py +++ b/tests/components/mqtt/test_select.py @@ -13,7 +13,12 @@ from homeassistant.components.select import ( DOMAIN as SELECT_DOMAIN, SERVICE_SELECT_OPTION, ) -from homeassistant.const import ATTR_ASSUMED_STATE, ATTR_ENTITY_ID, STATE_UNKNOWN +from homeassistant.const import ( + ATTR_ASSUMED_STATE, + ATTR_ENTITY_ID, + STATE_UNKNOWN, + Platform, +) import homeassistant.core as ha from homeassistant.setup import async_setup_component @@ -59,6 +64,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def select_platform_only(): + """Only setup the select platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SELECT]): + yield + + async def test_run_select_setup(hass, mqtt_mock_entry_with_yaml_config): """Test that it fetches the given payload.""" topic = "test/select" diff --git a/tests/components/mqtt/test_sensor.py b/tests/components/mqtt/test_sensor.py index 0cc275c9d1f..f30bcf43392 100644 --- a/tests/components/mqtt/test_sensor.py +++ b/tests/components/mqtt/test_sensor.py @@ -14,6 +14,7 @@ from homeassistant.const import ( STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT, + Platform, ) import homeassistant.core as ha from homeassistant.helpers import device_registry as dr @@ -72,6 +73,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def sensor_platform_only(): + """Only setup the sensor platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SENSOR]): + yield + + async def test_setting_sensor_value_via_mqtt_message( hass, mqtt_mock_entry_with_yaml_config ): diff --git a/tests/components/mqtt/test_siren.py b/tests/components/mqtt/test_siren.py index 1dce382421e..c3916acb34b 100644 --- a/tests/components/mqtt/test_siren.py +++ b/tests/components/mqtt/test_siren.py @@ -15,6 +15,7 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_UNKNOWN, + Platform, ) from homeassistant.setup import async_setup_component @@ -55,6 +56,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def siren_platform_only(): + """Only setup the siren platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SIREN]): + yield + + async def async_turn_on(hass, entity_id=ENTITY_MATCH_ALL, parameters={}) -> None: """Turn all or specified siren on.""" data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} diff --git a/tests/components/mqtt/test_state_vacuum.py b/tests/components/mqtt/test_state_vacuum.py index 64545d2c140..b0b89c28646 100644 --- a/tests/components/mqtt/test_state_vacuum.py +++ b/tests/components/mqtt/test_state_vacuum.py @@ -31,6 +31,7 @@ from homeassistant.const import ( CONF_PLATFORM, ENTITY_MATCH_ALL, STATE_UNKNOWN, + Platform, ) from homeassistant.setup import async_setup_component @@ -87,6 +88,13 @@ DEFAULT_CONFIG_2 = { } +@pytest.fixture(autouse=True) +def vacuum_platform_only(): + """Only setup the vacuum platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.VACUUM]): + yield + + async def test_default_supported_features(hass, mqtt_mock_entry_with_yaml_config): """Test that the correct supported features.""" assert await async_setup_component( diff --git a/tests/components/mqtt/test_subscription.py b/tests/components/mqtt/test_subscription.py index 7c1663b9c09..3be66f0aa90 100644 --- a/tests/components/mqtt/test_subscription.py +++ b/tests/components/mqtt/test_subscription.py @@ -1,5 +1,7 @@ """The tests for the MQTT subscription component.""" -from unittest.mock import ANY +from unittest.mock import ANY, patch + +import pytest from homeassistant.components.mqtt.subscription import ( async_prepare_subscribe_topics, @@ -11,6 +13,13 @@ from homeassistant.core import callback from tests.common import async_fire_mqtt_message +@pytest.fixture(autouse=True) +def no_platforms(): + """Skip platform setup to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", []): + yield + + async def test_subscribe_topics(hass, mqtt_mock_entry_no_yaml_config, caplog): """Test subscription to topics.""" await mqtt_mock_entry_no_yaml_config() diff --git a/tests/components/mqtt/test_switch.py b/tests/components/mqtt/test_switch.py index 1ed8db34d8d..ba23efc859c 100644 --- a/tests/components/mqtt/test_switch.py +++ b/tests/components/mqtt/test_switch.py @@ -11,6 +11,7 @@ from homeassistant.const import ( STATE_OFF, STATE_ON, STATE_UNKNOWN, + Platform, ) import homeassistant.core as ha from homeassistant.setup import async_setup_component @@ -53,6 +54,13 @@ DEFAULT_CONFIG = { } +@pytest.fixture(autouse=True) +def switch_platform_only(): + """Only setup the switch platform to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SWITCH]): + yield + + async def test_controlling_state_via_topic(hass, mqtt_mock_entry_with_yaml_config): """Test the controlling state via topic.""" assert await async_setup_component( diff --git a/tests/components/mqtt/test_tag.py b/tests/components/mqtt/test_tag.py index 09be31011f2..f06dd6f5244 100644 --- a/tests/components/mqtt/test_tag.py +++ b/tests/components/mqtt/test_tag.py @@ -7,6 +7,7 @@ import pytest from homeassistant.components.device_automation import DeviceAutomationType from homeassistant.components.mqtt.const import DOMAIN as MQTT_DOMAIN +from homeassistant.const import Platform from homeassistant.helpers import device_registry as dr from homeassistant.setup import async_setup_component @@ -42,6 +43,13 @@ DEFAULT_TAG_SCAN_JSON = ( ) +@pytest.fixture(autouse=True) +def binary_sensor_only(): + """Only setup the binary_sensor platform to speed up test.""" + with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR]): + yield + + @pytest.fixture def device_reg(hass): """Return an empty, loaded, registry.""" diff --git a/tests/components/mqtt/test_trigger.py b/tests/components/mqtt/test_trigger.py index a4079558c34..4c0a70707eb 100644 --- a/tests/components/mqtt/test_trigger.py +++ b/tests/components/mqtt/test_trigger.py @@ -1,5 +1,5 @@ """The tests for the MQTT automation.""" -from unittest.mock import ANY +from unittest.mock import ANY, patch import pytest @@ -17,6 +17,13 @@ def calls(hass): return async_mock_service(hass, "test", "automation") +@pytest.fixture(autouse=True) +def no_platforms(): + """Skip platform setup to speed up tests.""" + with patch("homeassistant.components.mqtt.PLATFORMS", []): + yield + + @pytest.fixture(autouse=True) async def setup_comp(hass, mqtt_mock_entry_no_yaml_config): """Initialize components."""