diff --git a/homeassistant/components/mqtt/const.py b/homeassistant/components/mqtt/const.py index 5ab3f756311..c51cf84ef85 100644 --- a/homeassistant/components/mqtt/const.py +++ b/homeassistant/components/mqtt/const.py @@ -21,7 +21,7 @@ DATA_MQTT_CONFIG = "mqtt_config" DEFAULT_PREFIX = "homeassistant" DEFAULT_BIRTH_WILL_TOPIC = DEFAULT_PREFIX + "/status" -DEFAULT_DISCOVERY = False +DEFAULT_DISCOVERY = True DEFAULT_QOS = 0 DEFAULT_PAYLOAD_AVAILABLE = "online" DEFAULT_PAYLOAD_NOT_AVAILABLE = "offline" diff --git a/tests/components/mqtt/test_binary_sensor.py b/tests/components/mqtt/test_binary_sensor.py index 2a718d60777..d4c837de023 100644 --- a/tests/components/mqtt/test_binary_sensor.py +++ b/tests/components/mqtt/test_binary_sensor.py @@ -5,8 +5,7 @@ import json import pytest -from homeassistant.components import binary_sensor, mqtt -from homeassistant.components.mqtt.discovery import async_start +from homeassistant.components import binary_sensor from homeassistant.const import ( EVENT_STATE_CHANGED, STATE_OFF, @@ -162,9 +161,6 @@ async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor( hass, mqtt_mock, caplog ): """Test that binary_sensor with expire_after set behaves correctly on discovery and discovery update.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) - config = { "name": "Test", "state_topic": "test-topic", diff --git a/tests/components/mqtt/test_camera.py b/tests/components/mqtt/test_camera.py index 22f714fdcf7..1f3acee119e 100644 --- a/tests/components/mqtt/test_camera.py +++ b/tests/components/mqtt/test_camera.py @@ -3,8 +3,7 @@ import json import pytest -from homeassistant.components import camera, mqtt -from homeassistant.components.mqtt.discovery import async_start +from homeassistant.components import camera from homeassistant.setup import async_setup_component from .test_common import ( @@ -152,9 +151,6 @@ async def test_discovery_removal_camera(hass, mqtt_mock, caplog): async def test_discovery_update_camera(hass, mqtt_mock, caplog): """Test update of discovered camera.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) - data1 = '{ "name": "Beer", "topic": "test_topic"}' data2 = '{ "name": "Milk", "topic": "test_topic"}' @@ -177,9 +173,6 @@ async def test_discovery_update_unchanged_camera(hass, mqtt_mock, caplog): @pytest.mark.no_fail_on_log_exception async def test_discovery_broken(hass, mqtt_mock, caplog): """Test handling of bad discovery message.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) - data1 = '{ "name": "Beer" }' data2 = '{ "name": "Milk", "topic": "test_topic"}' diff --git a/tests/components/mqtt/test_common.py b/tests/components/mqtt/test_common.py index 27ee060ebfa..659d69c2f91 100644 --- a/tests/components/mqtt/test_common.py +++ b/tests/components/mqtt/test_common.py @@ -7,7 +7,6 @@ from unittest import mock from homeassistant.components import mqtt from homeassistant.components.mqtt import debug_info from homeassistant.components.mqtt.const import MQTT_DISCONNECTED -from homeassistant.components.mqtt.discovery import async_start from homeassistant.const import ATTR_ASSUMED_STATE, STATE_UNAVAILABLE from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.setup import async_setup_component @@ -287,8 +286,6 @@ async def help_test_discovery_update_availability( data2 = json.dumps(config2[domain]) data3 = json.dumps(config3[domain]) - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data1) await hass.async_block_till_done() @@ -451,8 +448,6 @@ async def help_test_discovery_update_attr(hass, mqtt_mock, caplog, domain, confi data1 = json.dumps(config1[domain]) data2 = json.dumps(config2[domain]) - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data1) await hass.async_block_till_done() async_fire_mqtt_message(hass, "attr-topic1", '{ "val": "100" }') @@ -486,9 +481,6 @@ async def help_test_discovery_removal(hass, mqtt_mock, caplog, domain, data): This is a test helper for the MqttDiscoveryUpdate mixin. """ - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) - async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data) await hass.async_block_till_done() @@ -517,9 +509,6 @@ async def help_test_discovery_update( This is a test helper for the MqttDiscoveryUpdate mixin. """ - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) - async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", discovery_data1) await hass.async_block_till_done() @@ -567,9 +556,6 @@ async def help_test_discovery_update_unchanged( This is a test helper for the MqttDiscoveryUpdate mixin. """ - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) - async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data1) await hass.async_block_till_done() @@ -585,9 +571,6 @@ async def help_test_discovery_update_unchanged( async def help_test_discovery_broken(hass, mqtt_mock, caplog, domain, data1, data2): """Test handling of bad discovery message.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) - async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data1) await hass.async_block_till_done() @@ -614,8 +597,6 @@ async def help_test_entity_device_info_with_identifier(hass, mqtt_mock, domain, config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps(config) @@ -641,8 +622,6 @@ async def help_test_entity_device_info_with_connection(hass, mqtt_mock, domain, config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_MAC) config["unique_id"] = "veryunique" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps(config) @@ -665,8 +644,6 @@ async def help_test_entity_device_info_remove(hass, mqtt_mock, domain, config): config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) dev_registry = await hass.helpers.device_registry.async_get_registry() ent_registry = await hass.helpers.entity_registry.async_get_registry() @@ -696,8 +673,6 @@ async def help_test_entity_device_info_update(hass, mqtt_mock, domain, config): config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps(config) @@ -772,8 +747,6 @@ async def help_test_entity_id_update_discovery_update( config[domain]["availability_topic"] = "avty-topic" topic = "avty-topic" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) ent_registry = mock_registry(hass, {}) data = json.dumps(config[domain]) @@ -812,8 +785,6 @@ async def help_test_entity_debug_info(hass, mqtt_mock, domain, config): config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps(config) @@ -847,8 +818,6 @@ async def help_test_entity_debug_info_max_messages(hass, mqtt_mock, domain, conf config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps(config) @@ -911,8 +880,6 @@ async def help_test_entity_debug_info_message( if payload is None: payload = "ON" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps(config) @@ -959,8 +926,6 @@ async def help_test_entity_debug_info_remove(hass, mqtt_mock, domain, config): config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps(config) @@ -1004,8 +969,6 @@ async def help_test_entity_debug_info_update_entity_id(hass, mqtt_mock, domain, config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) dev_registry = await hass.helpers.device_registry.async_get_registry() ent_registry = mock_registry(hass, {}) diff --git a/tests/components/mqtt/test_config_flow.py b/tests/components/mqtt/test_config_flow.py index ef6e7a7656c..41dc4745528 100644 --- a/tests/components/mqtt/test_config_flow.py +++ b/tests/components/mqtt/test_config_flow.py @@ -5,7 +5,6 @@ import voluptuous as vol from homeassistant import data_entry_flow from homeassistant.components import mqtt -from homeassistant.components.mqtt.discovery import async_start from homeassistant.setup import async_setup_component from tests.async_mock import patch @@ -45,7 +44,7 @@ async def test_user_connection_works(hass, mock_try_connection, mock_finish_setu assert result["result"].data == { "broker": "127.0.0.1", "port": 1883, - "discovery": False, + "discovery": True, } # Check we tried the connection assert len(mock_try_connection.mock_calls) == 1 @@ -154,7 +153,6 @@ async def test_option_flow(hass, mqtt_mock, mock_try_connection): """Test config flow options.""" mock_try_connection.return_value = True config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) config_entry.data = { mqtt.CONF_BROKER: "test-broker", mqtt.CONF_PORT: 1234, @@ -227,7 +225,6 @@ async def test_disable_birth_will(hass, mqtt_mock, mock_try_connection): """Test disabling birth and will.""" mock_try_connection.return_value = True config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) config_entry.data = { mqtt.CONF_BROKER: "test-broker", mqtt.CONF_PORT: 1234, @@ -310,7 +307,6 @@ async def test_option_flow_default_suggested_values( """Test config flow options has default/suggested values.""" mock_try_connection.return_value = True config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) config_entry.data = { mqtt.CONF_BROKER: "test-broker", mqtt.CONF_PORT: 1234, diff --git a/tests/components/mqtt/test_device_trigger.py b/tests/components/mqtt/test_device_trigger.py index 43b483c074b..844f34e8d4f 100644 --- a/tests/components/mqtt/test_device_trigger.py +++ b/tests/components/mqtt/test_device_trigger.py @@ -6,7 +6,6 @@ import pytest import homeassistant.components.automation as automation from homeassistant.components.mqtt import DOMAIN, debug_info from homeassistant.components.mqtt.device_trigger import async_attach_trigger -from homeassistant.components.mqtt.discovery import async_start from homeassistant.setup import async_setup_component from tests.common import ( @@ -39,9 +38,6 @@ def calls(hass): async def test_get_triggers(hass, device_reg, entity_reg, mqtt_mock): """Test we get the expected triggers from a discovered mqtt device.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data1 = ( '{ "automation_type":"trigger",' ' "device":{"identifiers":["0AFFD2"]},' @@ -70,9 +66,6 @@ async def test_get_triggers(hass, device_reg, entity_reg, mqtt_mock): async def test_get_unknown_triggers(hass, device_reg, entity_reg, mqtt_mock): """Test we don't get unknown triggers.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - # Discover a sensor (without device triggers) data1 = ( '{ "device":{"identifiers":["0AFFD2"]},' @@ -113,9 +106,6 @@ async def test_get_unknown_triggers(hass, device_reg, entity_reg, mqtt_mock): async def test_get_non_existing_triggers(hass, device_reg, entity_reg, mqtt_mock): """Test getting non existing triggers.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - # Discover a sensor (without device triggers) data1 = ( '{ "device":{"identifiers":["0AFFD2"]},' @@ -133,9 +123,6 @@ async def test_get_non_existing_triggers(hass, device_reg, entity_reg, mqtt_mock @pytest.mark.no_fail_on_log_exception async def test_discover_bad_triggers(hass, device_reg, entity_reg, mqtt_mock): """Test bad discovery message.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - # Test sending bad data data0 = ( '{ "automation_type":"trigger",' @@ -178,9 +165,6 @@ async def test_discover_bad_triggers(hass, device_reg, entity_reg, mqtt_mock): async def test_update_remove_triggers(hass, device_reg, entity_reg, mqtt_mock): """Test triggers can be updated and removed.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data1 = ( '{ "automation_type":"trigger",' ' "device":{"identifiers":["0AFFD2"]},' @@ -234,9 +218,6 @@ async def test_update_remove_triggers(hass, device_reg, entity_reg, mqtt_mock): async def test_if_fires_on_mqtt_message(hass, device_reg, calls, mqtt_mock): """Test triggers firing.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data1 = ( '{ "automation_type":"trigger",' ' "device":{"identifiers":["0AFFD2"]},' @@ -312,9 +293,6 @@ async def test_if_fires_on_mqtt_message_late_discover( hass, device_reg, calls, mqtt_mock ): """Test triggers firing of MQTT device triggers discovered after setup.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data0 = ( '{ "device":{"identifiers":["0AFFD2"]},' ' "state_topic": "foobar/sensor",' @@ -398,9 +376,6 @@ async def test_if_fires_on_mqtt_message_after_update( hass, device_reg, calls, mqtt_mock ): """Test triggers firing after update.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data1 = ( '{ "automation_type":"trigger",' ' "device":{"identifiers":["0AFFD2"]},' @@ -474,9 +449,6 @@ async def test_if_fires_on_mqtt_message_after_update( async def test_no_resubscribe_same_topic(hass, device_reg, mqtt_mock): """Test subscription to topics without change.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data1 = ( '{ "automation_type":"trigger",' ' "device":{"identifiers":["0AFFD2"]},' @@ -521,9 +493,6 @@ async def test_not_fires_on_mqtt_message_after_remove_by_mqtt( hass, device_reg, calls, mqtt_mock ): """Test triggers not firing after removal.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data1 = ( '{ "automation_type":"trigger",' ' "device":{"identifiers":["0AFFD2"]},' @@ -584,9 +553,6 @@ async def test_not_fires_on_mqtt_message_after_remove_from_registry( hass, device_reg, calls, mqtt_mock ): """Test triggers not firing after removal.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data1 = ( '{ "automation_type":"trigger",' ' "device":{"identifiers":["0AFFD2"]},' @@ -637,9 +603,6 @@ async def test_not_fires_on_mqtt_message_after_remove_from_registry( async def test_attach_remove(hass, device_reg, mqtt_mock): """Test attach and removal of trigger.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data1 = ( '{ "automation_type":"trigger",' ' "device":{"identifiers":["0AFFD2"]},' @@ -689,9 +652,6 @@ async def test_attach_remove(hass, device_reg, mqtt_mock): async def test_attach_remove_late(hass, device_reg, mqtt_mock): """Test attach and removal of trigger .""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data0 = ( '{ "device":{"identifiers":["0AFFD2"]},' ' "state_topic": "foobar/sensor",' @@ -749,9 +709,6 @@ async def test_attach_remove_late(hass, device_reg, mqtt_mock): async def test_attach_remove_late2(hass, device_reg, mqtt_mock): """Test attach and removal of trigger .""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data0 = ( '{ "device":{"identifiers":["0AFFD2"]},' ' "state_topic": "foobar/sensor",' @@ -803,8 +760,6 @@ async def test_attach_remove_late2(hass, device_reg, mqtt_mock): async def test_entity_device_info_with_connection(hass, mqtt_mock): """Test MQTT device registry integration.""" - entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps( @@ -836,8 +791,6 @@ async def test_entity_device_info_with_connection(hass, mqtt_mock): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT device registry integration.""" - entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps( @@ -869,8 +822,6 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock): async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() config = { @@ -908,9 +859,6 @@ async def test_entity_device_info_update(hass, mqtt_mock): async def test_cleanup_trigger(hass, device_reg, entity_reg, mqtt_mock): """Test trigger discovery topic is cleaned when device is removed from registry.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - config = { "automation_type": "trigger", "topic": "test-topic", @@ -946,9 +894,6 @@ async def test_cleanup_trigger(hass, device_reg, entity_reg, mqtt_mock): async def test_cleanup_device(hass, device_reg, entity_reg, mqtt_mock): """Test removal from device registry when trigger is removed.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - config = { "automation_type": "trigger", "topic": "test-topic", @@ -978,9 +923,6 @@ async def test_cleanup_device(hass, device_reg, entity_reg, mqtt_mock): async def test_cleanup_device_several_triggers(hass, device_reg, entity_reg, mqtt_mock): """Test removal from device registry when the last trigger is removed.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - config1 = { "automation_type": "trigger", "topic": "test-topic", @@ -1037,9 +979,6 @@ async def test_cleanup_device_with_entity1(hass, device_reg, entity_reg, mqtt_mo Trigger removed first, then entity. """ - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - config1 = { "automation_type": "trigger", "topic": "test-topic", @@ -1092,9 +1031,6 @@ async def test_cleanup_device_with_entity2(hass, device_reg, entity_reg, mqtt_mo Entity removed first, then trigger. """ - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - config1 = { "automation_type": "trigger", "topic": "test-topic", @@ -1147,8 +1083,6 @@ async def test_trigger_debug_info(hass, mqtt_mock): This is a test helper for MQTT debug_info. """ - entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() config = { diff --git a/tests/components/mqtt/test_discovery.py b/tests/components/mqtt/test_discovery.py index 59687aaff62..42229691a0f 100644 --- a/tests/components/mqtt/test_discovery.py +++ b/tests/components/mqtt/test_discovery.py @@ -28,6 +28,10 @@ def entity_reg(hass): return mock_registry(hass) +@pytest.mark.parametrize( + "mqtt_config", + [{mqtt.CONF_BROKER: "mock-broker", mqtt.CONF_DISCOVERY: False}], +) async def test_subscribing_config_topic(hass, mqtt_mock): """Test setting up discovery.""" entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] @@ -46,10 +50,7 @@ async def test_invalid_topic(hass, mqtt_mock): with patch( "homeassistant.components.mqtt.discovery.async_dispatcher_send" ) as mock_dispatcher_send: - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - mock_dispatcher_send = AsyncMock(return_value=None) - await async_start(hass, "homeassistant", entry) async_fire_mqtt_message( hass, "homeassistant/binary_sensor/bla/not_config", "{}" @@ -63,10 +64,8 @@ async def test_invalid_json(hass, mqtt_mock, caplog): with patch( "homeassistant.components.mqtt.discovery.async_dispatcher_send" ) as mock_dispatcher_send: - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] mock_dispatcher_send = AsyncMock(return_value=None) - await async_start(hass, "homeassistant", entry) async_fire_mqtt_message( hass, "homeassistant/binary_sensor/bla/config", "not json" @@ -81,12 +80,10 @@ async def test_only_valid_components(hass, mqtt_mock, caplog): with patch( "homeassistant.components.mqtt.discovery.async_dispatcher_send" ) as mock_dispatcher_send: - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] invalid_component = "timer" mock_dispatcher_send = AsyncMock(return_value=None) - await async_start(hass, "homeassistant", entry) async_fire_mqtt_message( hass, f"homeassistant/{invalid_component}/bla/config", "{}" @@ -101,10 +98,6 @@ async def test_only_valid_components(hass, mqtt_mock, caplog): async def test_correct_config_discovery(hass, mqtt_mock, caplog): """Test sending in correct JSON.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - - await async_start(hass, "homeassistant", entry) - async_fire_mqtt_message( hass, "homeassistant/binary_sensor/bla/config", @@ -121,10 +114,6 @@ async def test_correct_config_discovery(hass, mqtt_mock, caplog): async def test_discover_fan(hass, mqtt_mock, caplog): """Test discovering an MQTT fan.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - - await async_start(hass, "homeassistant", entry) - async_fire_mqtt_message( hass, "homeassistant/fan/bla/config", @@ -141,10 +130,6 @@ async def test_discover_fan(hass, mqtt_mock, caplog): async def test_discover_climate(hass, mqtt_mock, caplog): """Test discovering an MQTT climate component.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - - await async_start(hass, "homeassistant", entry) - data = ( '{ "name": "ClimateTest",' ' "current_temperature_topic": "climate/bla/current_temp",' @@ -163,10 +148,6 @@ async def test_discover_climate(hass, mqtt_mock, caplog): async def test_discover_alarm_control_panel(hass, mqtt_mock, caplog): """Test discovering an MQTT alarm control panel component.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - - await async_start(hass, "homeassistant", entry) - data = ( '{ "name": "AlarmControlPanelTest",' ' "state_topic": "test_topic",' @@ -185,10 +166,6 @@ async def test_discover_alarm_control_panel(hass, mqtt_mock, caplog): async def test_discovery_incl_nodeid(hass, mqtt_mock, caplog): """Test sending in correct JSON with optional node_id included.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - - await async_start(hass, "homeassistant", entry) - async_fire_mqtt_message( hass, "homeassistant/binary_sensor/my_node_id/bla/config", @@ -205,10 +182,6 @@ async def test_discovery_incl_nodeid(hass, mqtt_mock, caplog): async def test_non_duplicate_discovery(hass, mqtt_mock, caplog): """Test for a non duplicate component.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - - await async_start(hass, "homeassistant", entry) - async_fire_mqtt_message( hass, "homeassistant/binary_sensor/bla/config", @@ -232,10 +205,6 @@ async def test_non_duplicate_discovery(hass, mqtt_mock, caplog): async def test_removal(hass, mqtt_mock, caplog): """Test removal of component through empty discovery message.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - - await async_start(hass, "homeassistant", entry) - async_fire_mqtt_message( hass, "homeassistant/binary_sensor/bla/config", @@ -253,10 +222,6 @@ async def test_removal(hass, mqtt_mock, caplog): async def test_rediscover(hass, mqtt_mock, caplog): """Test rediscover of removed component.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - - await async_start(hass, "homeassistant", entry) - async_fire_mqtt_message( hass, "homeassistant/binary_sensor/bla/config", @@ -283,10 +248,6 @@ async def test_rediscover(hass, mqtt_mock, caplog): async def test_duplicate_removal(hass, mqtt_mock, caplog): """Test for a non duplicate component.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - - await async_start(hass, "homeassistant", entry) - async_fire_mqtt_message( hass, "homeassistant/binary_sensor/bla/config", @@ -305,9 +266,6 @@ async def test_duplicate_removal(hass, mqtt_mock, caplog): async def test_cleanup_device(hass, device_reg, entity_reg, mqtt_mock): """Test discvered device is cleaned up when removed from registry.""" - config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data = ( '{ "device":{"identifiers":["0AFFD2"]},' ' "state_topic": "foobar/sensor",' @@ -348,10 +306,6 @@ async def test_cleanup_device(hass, device_reg, entity_reg, mqtt_mock): async def test_discovery_expansion(hass, mqtt_mock, caplog): """Test expansion of abbreviated discovery payload.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - - await async_start(hass, "homeassistant", entry) - data = ( '{ "~": "some/base/topic",' ' "name": "DiscoveryExpansionTest1",' @@ -440,10 +394,6 @@ async def test_missing_discover_abbreviations(hass, mqtt_mock, caplog): async def test_no_implicit_state_topic_switch(hass, mqtt_mock, caplog): """Test no implicit state topic for switch.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - - await async_start(hass, "homeassistant", entry) - data = '{ "name": "Test1",' ' "command_topic": "cmnd"' "}" async_fire_mqtt_message(hass, "homeassistant/switch/bla/config", data) @@ -463,12 +413,17 @@ async def test_no_implicit_state_topic_switch(hass, mqtt_mock, caplog): assert state.state == "off" +@pytest.mark.parametrize( + "mqtt_config", + [ + { + mqtt.CONF_BROKER: "mock-broker", + mqtt.CONF_DISCOVERY_PREFIX: "my_home/homeassistant/register", + } + ], +) async def test_complex_discovery_topic_prefix(hass, mqtt_mock, caplog): """Tests handling of discovery topic prefix with multiple slashes.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - - await async_start(hass, "my_home/homeassistant/register", entry) - async_fire_mqtt_message( hass, ("my_home/homeassistant/register/binary_sensor/node1/object1/config"), diff --git a/tests/components/mqtt/test_init.py b/tests/components/mqtt/test_init.py index b94dd123d8d..7b634714a5b 100644 --- a/tests/components/mqtt/test_init.py +++ b/tests/components/mqtt/test_init.py @@ -9,7 +9,6 @@ import voluptuous as vol from homeassistant.components import mqtt, websocket_api from homeassistant.components.mqtt import debug_info -from homeassistant.components.mqtt.discovery import async_start from homeassistant.const import ( ATTR_DOMAIN, ATTR_SERVICE, @@ -625,6 +624,10 @@ async def test_not_calling_unsubscribe_with_active_subscribers( assert not mqtt_client_mock.unsubscribe.called +@pytest.mark.parametrize( + "mqtt_config", + [{mqtt.CONF_BROKER: "mock-broker", mqtt.CONF_DISCOVERY: False}], +) async def test_restore_subscriptions_on_reconnect(hass, mqtt_client_mock, mqtt_mock): """Test subscriptions are restored on reconnect.""" # Fake that the client is connected @@ -640,6 +643,10 @@ async def test_restore_subscriptions_on_reconnect(hass, mqtt_client_mock, mqtt_m assert mqtt_client_mock.subscribe.call_count == 2 +@pytest.mark.parametrize( + "mqtt_config", + [{mqtt.CONF_BROKER: "mock-broker", mqtt.CONF_DISCOVERY: False}], +) async def test_restore_all_active_subscriptions_on_reconnect( hass, mqtt_client_mock, mqtt_mock ): @@ -840,7 +847,13 @@ async def test_no_will_message(hass, mqtt_client_mock, mqtt_mock): @pytest.mark.parametrize( "mqtt_config", - [{mqtt.CONF_BROKER: "mock-broker", mqtt.CONF_BIRTH_MESSAGE: {}}], + [ + { + mqtt.CONF_BROKER: "mock-broker", + mqtt.CONF_BIRTH_MESSAGE: {}, + mqtt.CONF_DISCOVERY: False, + } + ], ) async def test_mqtt_subscribes_topics_on_connect(hass, mqtt_client_mock, mqtt_mock): """Test subscription to topic on connect.""" @@ -933,9 +946,6 @@ async def test_mqtt_ws_remove_discovered_device( hass, device_reg, entity_reg, hass_ws_client, mqtt_mock ): """Test MQTT websocket device removal.""" - config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data = ( '{ "device":{"identifiers":["0AFFD2"]},' ' "state_topic": "foobar/sensor",' @@ -965,9 +975,6 @@ async def test_mqtt_ws_remove_discovered_device_twice( hass, device_reg, hass_ws_client, mqtt_mock ): """Test MQTT websocket device removal.""" - config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data = ( '{ "device":{"identifiers":["0AFFD2"]},' ' "state_topic": "foobar/sensor",' @@ -999,9 +1006,6 @@ async def test_mqtt_ws_remove_discovered_device_same_topic( hass, device_reg, hass_ws_client, mqtt_mock ): """Test MQTT websocket device removal.""" - config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - data = ( '{ "device":{"identifiers":["0AFFD2"]},' ' "state_topic": "foobar/sensor",' @@ -1056,9 +1060,6 @@ async def test_mqtt_ws_get_device_debug_info( hass, device_reg, hass_ws_client, mqtt_mock ): """Test MQTT websocket device debug info.""" - config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - config = { "device": {"identifiers": ["0AFFD2"]}, "platform": "mqtt", @@ -1141,8 +1142,6 @@ async def test_debug_info_multiple_devices(hass, mqtt_mock): }, ] - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() for d in devices: @@ -1222,8 +1221,6 @@ async def test_debug_info_multiple_entities_triggers(hass, mqtt_mock): }, ] - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() for c in config: @@ -1299,8 +1296,6 @@ async def test_debug_info_wildcard(hass, mqtt_mock): "unique_id": "veryunique", } - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps(config) @@ -1347,8 +1342,6 @@ async def test_debug_info_filter_same(hass, mqtt_mock): "unique_id": "veryunique", } - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps(config) @@ -1408,8 +1401,6 @@ async def test_debug_info_same_topic(hass, mqtt_mock): "unique_id": "veryunique", } - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps(config) @@ -1461,8 +1452,6 @@ async def test_debug_info_qos_retain(hass, mqtt_mock): "unique_id": "veryunique", } - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps(config) diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index 23c9a15a7dc..30084a310fd 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -159,8 +159,7 @@ from os import path import pytest from homeassistant import config as hass_config -from homeassistant.components import light, mqtt -from homeassistant.components.mqtt.discovery import async_start +from homeassistant.components import light from homeassistant.const import ATTR_ASSUMED_STATE, SERVICE_RELOAD, STATE_OFF, STATE_ON import homeassistant.core as ha from homeassistant.setup import async_setup_component @@ -1455,8 +1454,6 @@ async def test_discovery_removal_light(hass, mqtt_mock, caplog): async def test_discovery_deprecated(hass, mqtt_mock, caplog): """Test discovery of mqtt light with deprecated platform option.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) data = ( '{ "name": "Beer",' ' "platform": "mqtt",' ' "command_topic": "test_topic"}' ) diff --git a/tests/components/mqtt/test_sensor.py b/tests/components/mqtt/test_sensor.py index 73c22b05726..7e92579753b 100644 --- a/tests/components/mqtt/test_sensor.py +++ b/tests/components/mqtt/test_sensor.py @@ -5,8 +5,6 @@ import json import pytest -from homeassistant.components import mqtt -from homeassistant.components.mqtt.discovery import async_start import homeassistant.components.sensor as sensor from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE import homeassistant.core as ha @@ -560,9 +558,6 @@ async def test_entity_id_update_discovery_update(hass, mqtt_mock): async def test_entity_device_info_with_hub(hass, mqtt_mock): """Test MQTT sensor device registry integration.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) - registry = await hass.helpers.device_registry.async_get_registry() hub = registry.async_get_or_create( config_entry_id="123", diff --git a/tests/components/mqtt/test_tag.py b/tests/components/mqtt/test_tag.py index fa041fb58ad..bed05ac6384 100644 --- a/tests/components/mqtt/test_tag.py +++ b/tests/components/mqtt/test_tag.py @@ -4,9 +4,6 @@ import json import pytest -from homeassistant.components.mqtt import DOMAIN -from homeassistant.components.mqtt.discovery import async_start - from tests.async_mock import ANY, patch from tests.common import ( async_fire_mqtt_message, @@ -63,9 +60,6 @@ async def test_discover_bad_tag(hass, device_reg, entity_reg, mqtt_mock, tag_moc """Test bad discovery message.""" config1 = copy.deepcopy(DEFAULT_CONFIG_DEVICE) - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - # Test sending bad data data0 = '{ "device":{"identifiers":["0AFFD2"]}, "topics": "foobar/tag_scanned" }' async_fire_mqtt_message(hass, "homeassistant/tag/bla/config", data0) @@ -89,9 +83,6 @@ async def test_if_fires_on_mqtt_message_with_device( """Test tag scanning, with device.""" config = copy.deepcopy(DEFAULT_CONFIG_DEVICE) - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) await hass.async_block_till_done() device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}, set()) @@ -108,9 +99,6 @@ async def test_if_fires_on_mqtt_message_without_device( """Test tag scanning, without device.""" config = copy.deepcopy(DEFAULT_CONFIG) - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) await hass.async_block_till_done() @@ -126,9 +114,6 @@ async def test_if_fires_on_mqtt_message_with_template( """Test tag scanning, with device.""" config = copy.deepcopy(DEFAULT_CONFIG_JSON) - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) await hass.async_block_till_done() device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}, set()) @@ -143,9 +128,6 @@ async def test_strip_tag_id(hass, device_reg, mqtt_mock, tag_mock): """Test strip whitespace from tag_id.""" config = copy.deepcopy(DEFAULT_CONFIG) - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) await hass.async_block_till_done() @@ -163,9 +145,6 @@ async def test_if_fires_on_mqtt_message_after_update_with_device( config2 = copy.deepcopy(DEFAULT_CONFIG_DEVICE) config2["topic"] = "foobar/tag_scanned2" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config1)) await hass.async_block_till_done() device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}, set()) @@ -210,9 +189,6 @@ async def test_if_fires_on_mqtt_message_after_update_without_device( config2 = copy.deepcopy(DEFAULT_CONFIG) config2["topic"] = "foobar/tag_scanned2" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config1)) await hass.async_block_till_done() @@ -257,9 +233,6 @@ async def test_if_fires_on_mqtt_message_after_update_with_template( config2["value_template"] = "{{ value_json.RDM6300.UID }}" tag_scan_2 = '{"Time":"2020-09-28T17:02:10","RDM6300":{"UID":"E9F35959", "DATA":"ILOVETASMOTA"}}' - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config1)) await hass.async_block_till_done() device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}, set()) @@ -300,9 +273,6 @@ async def test_no_resubscribe_same_topic(hass, device_reg, mqtt_mock): """Test subscription to topics without change.""" config = copy.deepcopy(DEFAULT_CONFIG_DEVICE) - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) await hass.async_block_till_done() assert device_reg.async_get_device({("mqtt", "0AFFD2")}, set()) @@ -319,9 +289,6 @@ async def test_not_fires_on_mqtt_message_after_remove_by_mqtt_with_device( """Test tag scanning after removal.""" config = copy.deepcopy(DEFAULT_CONFIG_DEVICE) - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) await hass.async_block_till_done() device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}, set()) @@ -355,9 +322,6 @@ async def test_not_fires_on_mqtt_message_after_remove_by_mqtt_without_device( """Test tag scanning not firing after removal.""" config = copy.deepcopy(DEFAULT_CONFIG) - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) await hass.async_block_till_done() @@ -393,9 +357,6 @@ async def test_not_fires_on_mqtt_message_after_remove_from_registry( """Test tag scanning after removal.""" config = copy.deepcopy(DEFAULT_CONFIG_DEVICE) - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) await hass.async_block_till_done() device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}, set()) @@ -417,8 +378,6 @@ async def test_not_fires_on_mqtt_message_after_remove_from_registry( async def test_entity_device_info_with_connection(hass, mqtt_mock): """Test MQTT device registry integration.""" - entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps( @@ -447,8 +406,6 @@ async def test_entity_device_info_with_connection(hass, mqtt_mock): async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT device registry integration.""" - entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps( @@ -477,8 +434,6 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock): async def test_entity_device_info_update(hass, mqtt_mock): """Test device registry update.""" - entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", entry) registry = await hass.helpers.device_registry.async_get_registry() config = { @@ -513,9 +468,6 @@ async def test_entity_device_info_update(hass, mqtt_mock): async def test_cleanup_tag(hass, device_reg, entity_reg, mqtt_mock): """Test tag discovery topic is cleaned when device is removed from registry.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - config = { "topic": "test-topic", "device": {"identifiers": ["helloworld"]}, @@ -545,9 +497,6 @@ async def test_cleanup_tag(hass, device_reg, entity_reg, mqtt_mock): async def test_cleanup_device(hass, device_reg, entity_reg, mqtt_mock): """Test removal from device registry when tag is removed.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - config = { "topic": "test-topic", "device": {"identifiers": ["helloworld"]}, @@ -573,9 +522,6 @@ async def test_cleanup_device_several_tags( hass, device_reg, entity_reg, mqtt_mock, tag_mock ): """Test removal from device registry when the last tag is removed.""" - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - config1 = { "topic": "test-topic1", "device": {"identifiers": ["helloworld"]}, @@ -623,9 +569,6 @@ async def test_cleanup_device_with_entity_and_trigger_1( Tag removed first, then trigger and entity. """ - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - config1 = { "topic": "test-topic", "device": {"identifiers": ["helloworld"]}, @@ -686,9 +629,6 @@ async def test_cleanup_device_with_entity2(hass, device_reg, entity_reg, mqtt_mo Trigger and entity removed first, then tag. """ - config_entry = hass.config_entries.async_entries(DOMAIN)[0] - await async_start(hass, "homeassistant", config_entry) - config1 = { "topic": "test-topic", "device": {"identifiers": ["helloworld"]},