From 280f49540e0ff97e303584d90a1776f985be4de2 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 22 Jun 2020 23:59:50 +0200 Subject: [PATCH] Drop use of async_mock_mqtt_component (#37011) --- tests/components/automation/test_mqtt.py | 22 ++----- tests/components/axis/test_device.py | 12 +--- tests/components/mqtt/conftest.py | 51 --------------- .../mqtt/test_alarm_control_panel.py | 4 +- tests/components/mqtt/test_binary_sensor.py | 4 +- tests/components/mqtt/test_camera.py | 9 ++- tests/components/mqtt/test_climate.py | 4 +- tests/components/mqtt/test_common.py | 19 ++---- tests/components/mqtt/test_cover.py | 4 +- tests/components/mqtt/test_device_trigger.py | 6 +- tests/components/mqtt/test_fan.py | 4 +- tests/components/mqtt/test_legacy_vacuum.py | 2 +- tests/components/mqtt/test_light.py | 4 +- tests/components/mqtt/test_light_json.py | 4 +- tests/components/mqtt/test_light_template.py | 4 +- tests/components/mqtt/test_lock.py | 4 +- tests/components/mqtt/test_sensor.py | 4 +- tests/components/mqtt/test_state_vacuum.py | 2 +- tests/components/mqtt/test_subscription.py | 13 ++-- tests/components/mqtt/test_switch.py | 26 +++----- .../mqtt_json/test_device_tracker.py | 5 +- tests/components/mqtt_room/test_sensor.py | 6 +- .../owntracks/test_device_tracker.py | 10 +-- tests/components/snips/test_init.py | 63 +++++-------------- tests/conftest.py | 49 ++++++++++++++- 25 files changed, 126 insertions(+), 209 deletions(-) diff --git a/tests/components/automation/test_mqtt.py b/tests/components/automation/test_mqtt.py index 0a07c5aac48..f0dd76ff1b4 100644 --- a/tests/components/automation/test_mqtt.py +++ b/tests/components/automation/test_mqtt.py @@ -6,12 +6,7 @@ import pytest import homeassistant.components.automation as automation from homeassistant.setup import async_setup_component -from tests.common import ( - async_fire_mqtt_message, - async_mock_mqtt_component, - async_mock_service, - mock_component, -) +from tests.common import async_fire_mqtt_message, async_mock_service, mock_component from tests.components.automation import common @@ -22,10 +17,9 @@ def calls(hass): @pytest.fixture(autouse=True) -def setup_comp(hass): +def setup_comp(hass, mqtt_mock): """Initialize components.""" mock_component(hass, "group") - hass.loop.run_until_complete(async_mock_mqtt_component(hass)) async def test_if_fires_on_topic_match(hass, calls): @@ -104,10 +98,8 @@ async def test_if_not_fires_on_topic_but_no_payload_match(hass, calls): assert len(calls) == 0 -async def test_encoding_default(hass, calls): +async def test_encoding_default(hass, calls, mqtt_mock): """Test default encoding.""" - mock_mqtt = await async_mock_mqtt_component(hass) - assert await async_setup_component( hass, automation.DOMAIN, @@ -119,15 +111,13 @@ async def test_encoding_default(hass, calls): }, ) - mock_mqtt.async_subscribe.assert_called_once_with( + mqtt_mock.async_subscribe.assert_called_once_with( "test-topic", mock.ANY, 0, "utf-8" ) -async def test_encoding_custom(hass, calls): +async def test_encoding_custom(hass, calls, mqtt_mock): """Test default encoding.""" - mock_mqtt = await async_mock_mqtt_component(hass) - assert await async_setup_component( hass, automation.DOMAIN, @@ -139,4 +129,4 @@ async def test_encoding_custom(hass, calls): }, ) - mock_mqtt.async_subscribe.assert_called_once_with("test-topic", mock.ANY, 0, None) + mqtt_mock.async_subscribe.assert_called_once_with("test-topic", mock.ANY, 0, None) diff --git a/tests/components/axis/test_device.py b/tests/components/axis/test_device.py index a4a40c18af1..4350764c486 100644 --- a/tests/components/axis/test_device.py +++ b/tests/components/axis/test_device.py @@ -38,11 +38,7 @@ from homeassistant.const import ( ) from tests.async_mock import Mock, patch -from tests.common import ( - MockConfigEntry, - async_fire_mqtt_message, - async_mock_mqtt_component, -) +from tests.common import MockConfigEntry, async_fire_mqtt_message MAC = "00408C12345" MODEL = "model" @@ -266,17 +262,15 @@ async def test_device_info(hass): assert device.api.vapix.serial_number == "00408C12345" -async def test_device_support_mqtt(hass): +async def test_device_support_mqtt(hass, mqtt_mock): """Successful setup.""" api_discovery = deepcopy(API_DISCOVERY_RESPONSE) api_discovery["data"]["apiList"].append(API_DISCOVERY_MQTT) - mock_mqtt = await async_mock_mqtt_component(hass) - with patch.dict(API_DISCOVERY_RESPONSE, api_discovery): await setup_axis_integration(hass) - mock_mqtt.async_subscribe.assert_called_with(f"{MAC}/#", mock.ANY, 0, "utf-8") + mqtt_mock.async_subscribe.assert_called_with(f"{MAC}/#", mock.ANY, 0, "utf-8") topic = f"{MAC}/event/tns:onvif/Device/tns:axis/Sensor/PIR/$source/sensor/0" message = b'{"timestamp": 1590258472044, "topic": "onvif:Device/axis:Sensor/PIR", "message": {"source": {"sensor": "0"}, "key": {}, "data": {"state": "1"}}}' diff --git a/tests/components/mqtt/conftest.py b/tests/components/mqtt/conftest.py index 2c5eaf3e54e..895327d1756 100644 --- a/tests/components/mqtt/conftest.py +++ b/tests/components/mqtt/conftest.py @@ -1,52 +1 @@ """Test fixtures for mqtt component.""" -import pytest - -from homeassistant import core as ha -from homeassistant.components import mqtt -from homeassistant.setup import async_setup_component - -from tests.async_mock import MagicMock, patch -from tests.common import async_fire_mqtt_message - - -@pytest.fixture -def mqtt_config(): - """Fixture to allow overriding MQTT config.""" - return None - - -@pytest.fixture -def mqtt_client_mock(hass): - """Fixture to mock MQTT client.""" - - @ha.callback - def _async_fire_mqtt_message(topic, payload, qos, retain): - async_fire_mqtt_message(hass, topic, payload, qos, retain) - - with patch("paho.mqtt.client.Client") as mock_client: - mock_client = mock_client.return_value - mock_client.connect.return_value = 0 - mock_client.subscribe.return_value = (0, 0) - mock_client.unsubscribe.return_value = (0, 0) - mock_client.publish.side_effect = _async_fire_mqtt_message - yield mock_client - - -@pytest.fixture -async def mqtt_mock(hass, mqtt_client_mock, mqtt_config): - """Fixture to mock MQTT component.""" - if mqtt_config is None: - mqtt_config = {mqtt.CONF_BROKER: "mock-broker"} - - result = await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: mqtt_config}) - assert result - await hass.async_block_till_done() - - mqtt_component_mock = MagicMock(spec_set=hass.data["mqtt"], wraps=hass.data["mqtt"]) - hass.data["mqtt"].connected = mqtt_component_mock.connected - mqtt_component_mock._mqttc = mqtt_client_mock - - hass.data["mqtt"] = mqtt_component_mock - component = hass.data["mqtt"] - component.reset_mock() - return component diff --git a/tests/components/mqtt/test_alarm_control_panel.py b/tests/components/mqtt/test_alarm_control_panel.py index 29ecb277928..1aa26cf0334 100644 --- a/tests/components/mqtt/test_alarm_control_panel.py +++ b/tests/components/mqtt/test_alarm_control_panel.py @@ -533,7 +533,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ) -async def test_unique_id(hass): +async def test_unique_id(hass, mqtt_mock): """Test unique id option only creates one alarm per unique_id.""" config = { alarm_control_panel.DOMAIN: [ @@ -553,7 +553,7 @@ async def test_unique_id(hass): }, ] } - await help_test_unique_id(hass, alarm_control_panel.DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, alarm_control_panel.DOMAIN, config) async def test_discovery_removal_alarm(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_binary_sensor.py b/tests/components/mqtt/test_binary_sensor.py index c0a09e088e5..67bb6704339 100644 --- a/tests/components/mqtt/test_binary_sensor.py +++ b/tests/components/mqtt/test_binary_sensor.py @@ -476,7 +476,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ) -async def test_unique_id(hass): +async def test_unique_id(hass, mqtt_mock): """Test unique id option only creates one sensor per unique_id.""" config = { binary_sensor.DOMAIN: [ @@ -494,7 +494,7 @@ async def test_unique_id(hass): }, ] } - await help_test_unique_id(hass, binary_sensor.DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, binary_sensor.DOMAIN, config) async def test_discovery_removal_binary_sensor(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_camera.py b/tests/components/mqtt/test_camera.py index d7f07175b32..6869b530668 100644 --- a/tests/components/mqtt/test_camera.py +++ b/tests/components/mqtt/test_camera.py @@ -30,17 +30,16 @@ from .test_common import ( help_test_update_with_json_attrs_not_dict, ) -from tests.common import async_fire_mqtt_message, async_mock_mqtt_component +from tests.common import async_fire_mqtt_message DEFAULT_CONFIG = { camera.DOMAIN: {"platform": "mqtt", "name": "test", "topic": "test_topic"} } -async def test_run_camera_setup(hass, aiohttp_client): +async def test_run_camera_setup(hass, aiohttp_client, mqtt_mock): """Test that it fetches the given payload.""" topic = "test/camera" - await async_mock_mqtt_component(hass) await async_setup_component( hass, "camera", @@ -122,7 +121,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ) -async def test_unique_id(hass): +async def test_unique_id(hass, mqtt_mock): """Test unique id option only creates one camera per unique_id.""" config = { camera.DOMAIN: [ @@ -140,7 +139,7 @@ async def test_unique_id(hass): }, ] } - await help_test_unique_id(hass, camera.DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, camera.DOMAIN, config) async def test_discovery_removal_camera(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_climate.py b/tests/components/mqtt/test_climate.py index 1736174d232..6ccb38eda2d 100644 --- a/tests/components/mqtt/test_climate.py +++ b/tests/components/mqtt/test_climate.py @@ -870,7 +870,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ) -async def test_unique_id(hass): +async def test_unique_id(hass, mqtt_mock): """Test unique id option only creates one climate per unique_id.""" config = { CLIMATE_DOMAIN: [ @@ -890,7 +890,7 @@ async def test_unique_id(hass): }, ] } - await help_test_unique_id(hass, CLIMATE_DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, CLIMATE_DOMAIN, config) async def test_discovery_removal_climate(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_common.py b/tests/components/mqtt/test_common.py index f39000a834d..7da0a09f024 100644 --- a/tests/components/mqtt/test_common.py +++ b/tests/components/mqtt/test_common.py @@ -12,12 +12,7 @@ from homeassistant.const import ATTR_ASSUMED_STATE, STATE_UNAVAILABLE from homeassistant.helpers.dispatcher import async_dispatcher_send from tests.async_mock import ANY -from tests.common import ( - async_fire_mqtt_message, - async_mock_mqtt_component, - async_setup_component, - mock_registry, -) +from tests.common import async_fire_mqtt_message, async_setup_component, mock_registry DEFAULT_CONFIG_DEVICE_INFO_ID = { "identifiers": ["helloworld"], @@ -272,9 +267,8 @@ async def help_test_discovery_update_attr(hass, mqtt_mock, caplog, domain, confi assert state.attributes.get("val") == "75" -async def help_test_unique_id(hass, domain, config): +async def help_test_unique_id(hass, mqtt_mock, domain, config): """Test unique id option only creates one entity per unique_id.""" - await async_mock_mqtt_component(hass) assert await async_setup_component(hass, domain, config) await hass.async_block_till_done() assert len(hass.states.async_entity_ids(domain)) == 1 @@ -478,16 +472,15 @@ async def help_test_entity_id_update_subscriptions( topics = ["avty-topic", "test-topic"] assert len(topics) > 0 registry = mock_registry(hass, {}) - mock_mqtt = await async_mock_mqtt_component(hass) assert await async_setup_component(hass, domain, config,) await hass.async_block_till_done() state = hass.states.get(f"{domain}.test") assert state is not None - assert mock_mqtt.async_subscribe.call_count == len(topics) + assert mqtt_mock.async_subscribe.call_count == len(topics) for topic in topics: - mock_mqtt.async_subscribe.assert_any_call(topic, ANY, ANY, ANY) - mock_mqtt.async_subscribe.reset_mock() + mqtt_mock.async_subscribe.assert_any_call(topic, ANY, ANY, ANY) + mqtt_mock.async_subscribe.reset_mock() registry.async_update_entity(f"{domain}.test", new_entity_id=f"{domain}.milk") await hass.async_block_till_done() @@ -498,7 +491,7 @@ async def help_test_entity_id_update_subscriptions( state = hass.states.get(f"{domain}.milk") assert state is not None for topic in topics: - mock_mqtt.async_subscribe.assert_any_call(topic, ANY, ANY, ANY) + mqtt_mock.async_subscribe.assert_any_call(topic, ANY, ANY, ANY) async def help_test_entity_id_update_discovery_update( diff --git a/tests/components/mqtt/test_cover.py b/tests/components/mqtt/test_cover.py index 9ef74565206..c3f00badef8 100644 --- a/tests/components/mqtt/test_cover.py +++ b/tests/components/mqtt/test_cover.py @@ -1839,7 +1839,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ) -async def test_unique_id(hass): +async def test_unique_id(hass, mqtt_mock): """Test unique_id option only creates one cover per id.""" config = { cover.DOMAIN: [ @@ -1857,7 +1857,7 @@ async def test_unique_id(hass): }, ] } - await help_test_unique_id(hass, cover.DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, cover.DOMAIN, config) async def test_discovery_removal_cover(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_device_trigger.py b/tests/components/mqtt/test_device_trigger.py index bc05786a59d..43b483c074b 100644 --- a/tests/components/mqtt/test_device_trigger.py +++ b/tests/components/mqtt/test_device_trigger.py @@ -13,7 +13,6 @@ from tests.common import ( assert_lists_same, async_fire_mqtt_message, async_get_device_automations, - async_mock_mqtt_component, async_mock_service, mock_device_registry, mock_registry, @@ -475,7 +474,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.""" - mock_mqtt = await async_mock_mqtt_component(hass) config_entry = hass.config_entries.async_entries(DOMAIN)[0] await async_start(hass, "homeassistant", config_entry) @@ -513,10 +511,10 @@ async def test_no_resubscribe_same_topic(hass, device_reg, mqtt_mock): }, ) - call_count = mock_mqtt.async_subscribe.call_count + call_count = mqtt_mock.async_subscribe.call_count async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) await hass.async_block_till_done() - assert mock_mqtt.async_subscribe.call_count == call_count + assert mqtt_mock.async_subscribe.call_count == call_count async def test_not_fires_on_mqtt_message_after_remove_by_mqtt( diff --git a/tests/components/mqtt/test_fan.py b/tests/components/mqtt/test_fan.py index bec771b6004..6114fe48ff4 100644 --- a/tests/components/mqtt/test_fan.py +++ b/tests/components/mqtt/test_fan.py @@ -664,7 +664,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ) -async def test_unique_id(hass): +async def test_unique_id(hass, mqtt_mock): """Test unique_id option only creates one fan per id.""" config = { fan.DOMAIN: [ @@ -684,7 +684,7 @@ async def test_unique_id(hass): }, ] } - await help_test_unique_id(hass, fan.DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, fan.DOMAIN, config) async def test_discovery_removal_fan(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_legacy_vacuum.py b/tests/components/mqtt/test_legacy_vacuum.py index 9d02295db2e..893c1b78f1e 100644 --- a/tests/components/mqtt/test_legacy_vacuum.py +++ b/tests/components/mqtt/test_legacy_vacuum.py @@ -625,7 +625,7 @@ async def test_unique_id(hass, mqtt_mock): }, ] } - await help_test_unique_id(hass, vacuum.DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, vacuum.DOMAIN, config) async def test_discovery_removal_vacuum(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_light.py b/tests/components/mqtt/test_light.py index 0e4f8673aed..5fa8fa181e5 100644 --- a/tests/components/mqtt/test_light.py +++ b/tests/components/mqtt/test_light.py @@ -1386,7 +1386,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ) -async def test_unique_id(hass): +async def test_unique_id(hass, mqtt_mock): """Test unique id option only creates one light per unique_id.""" config = { light.DOMAIN: [ @@ -1406,7 +1406,7 @@ async def test_unique_id(hass): }, ] } - await help_test_unique_id(hass, light.DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, light.DOMAIN, config) async def test_discovery_removal_light(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_light_json.py b/tests/components/mqtt/test_light_json.py index 19c531ecb30..7bb3763654e 100644 --- a/tests/components/mqtt/test_light_json.py +++ b/tests/components/mqtt/test_light_json.py @@ -1129,7 +1129,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ) -async def test_unique_id(hass): +async def test_unique_id(hass, mqtt_mock): """Test unique id option only creates one light per unique_id.""" config = { light.DOMAIN: [ @@ -1151,7 +1151,7 @@ async def test_unique_id(hass): }, ] } - await help_test_unique_id(hass, light.DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, light.DOMAIN, config) async def test_discovery_removal(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_light_template.py b/tests/components/mqtt/test_light_template.py index d1e27d4c516..f0e226d2095 100644 --- a/tests/components/mqtt/test_light_template.py +++ b/tests/components/mqtt/test_light_template.py @@ -861,7 +861,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ) -async def test_unique_id(hass): +async def test_unique_id(hass, mqtt_mock): """Test unique id option only creates one light per unique_id.""" config = { light.DOMAIN: [ @@ -885,7 +885,7 @@ async def test_unique_id(hass): }, ] } - await help_test_unique_id(hass, light.DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, light.DOMAIN, config) async def test_discovery_removal(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_lock.py b/tests/components/mqtt/test_lock.py index abea215f837..ff130077a95 100644 --- a/tests/components/mqtt/test_lock.py +++ b/tests/components/mqtt/test_lock.py @@ -336,7 +336,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ) -async def test_unique_id(hass): +async def test_unique_id(hass, mqtt_mock): """Test unique id option only creates one lock per unique_id.""" config = { LOCK_DOMAIN: [ @@ -356,7 +356,7 @@ async def test_unique_id(hass): }, ] } - await help_test_unique_id(hass, LOCK_DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, LOCK_DOMAIN, config) async def test_discovery_removal_lock(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_sensor.py b/tests/components/mqtt/test_sensor.py index 9a6ccfb84fd..f54a27e8805 100644 --- a/tests/components/mqtt/test_sensor.py +++ b/tests/components/mqtt/test_sensor.py @@ -337,7 +337,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ) -async def test_unique_id(hass): +async def test_unique_id(hass, mqtt_mock): """Test unique id option only creates one sensor per unique_id.""" config = { sensor.DOMAIN: [ @@ -355,7 +355,7 @@ async def test_unique_id(hass): }, ] } - await help_test_unique_id(hass, sensor.DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, sensor.DOMAIN, config) async def test_discovery_removal_sensor(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_state_vacuum.py b/tests/components/mqtt/test_state_vacuum.py index 1a59e84c2be..c8ca7d3691b 100644 --- a/tests/components/mqtt/test_state_vacuum.py +++ b/tests/components/mqtt/test_state_vacuum.py @@ -405,7 +405,7 @@ async def test_unique_id(hass, mqtt_mock): }, ] } - await help_test_unique_id(hass, vacuum.DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, vacuum.DOMAIN, config) async def test_discovery_removal_vacuum(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt/test_subscription.py b/tests/components/mqtt/test_subscription.py index 6c906cda1d1..05f14107384 100644 --- a/tests/components/mqtt/test_subscription.py +++ b/tests/components/mqtt/test_subscription.py @@ -7,7 +7,7 @@ from homeassistant.components.mqtt.subscription import ( ) from homeassistant.core import callback -from tests.common import async_fire_mqtt_message, async_mock_mqtt_component +from tests.common import async_fire_mqtt_message async def test_subscribe_topics(hass, mqtt_mock, caplog): @@ -119,7 +119,6 @@ async def test_modify_topics(hass, mqtt_mock, caplog): async def test_qos_encoding_default(hass, mqtt_mock, caplog): """Test default qos and encoding.""" - mock_mqtt = await async_mock_mqtt_component(hass) @callback def msg_callback(*args): @@ -132,14 +131,13 @@ async def test_qos_encoding_default(hass, mqtt_mock, caplog): sub_state, {"test_topic1": {"topic": "test-topic1", "msg_callback": msg_callback}}, ) - mock_mqtt.async_subscribe.assert_called_once_with( + mqtt_mock.async_subscribe.assert_called_once_with( "test-topic1", mock.ANY, 0, "utf-8" ) async def test_qos_encoding_custom(hass, mqtt_mock, caplog): """Test custom qos and encoding.""" - mock_mqtt = await async_mock_mqtt_component(hass) @callback def msg_callback(*args): @@ -159,14 +157,13 @@ async def test_qos_encoding_custom(hass, mqtt_mock, caplog): } }, ) - mock_mqtt.async_subscribe.assert_called_once_with( + mqtt_mock.async_subscribe.assert_called_once_with( "test-topic1", mock.ANY, 1, "utf-16" ) async def test_no_change(hass, mqtt_mock, caplog): """Test subscription to topics without change.""" - mock_mqtt = await async_mock_mqtt_component(hass) @callback def msg_callback(*args): @@ -179,10 +176,10 @@ async def test_no_change(hass, mqtt_mock, caplog): sub_state, {"test_topic1": {"topic": "test-topic1", "msg_callback": msg_callback}}, ) - call_count = mock_mqtt.async_subscribe.call_count + call_count = mqtt_mock.async_subscribe.call_count sub_state = await async_subscribe_topics( hass, sub_state, {"test_topic1": {"topic": "test-topic1", "msg_callback": msg_callback}}, ) - assert call_count == mock_mqtt.async_subscribe.call_count + assert call_count == mqtt_mock.async_subscribe.call_count diff --git a/tests/components/mqtt/test_switch.py b/tests/components/mqtt/test_switch.py index b90dfa68413..869a413eb6b 100644 --- a/tests/components/mqtt/test_switch.py +++ b/tests/components/mqtt/test_switch.py @@ -30,7 +30,7 @@ from .test_common import ( ) from tests.async_mock import patch -from tests.common import async_fire_mqtt_message, async_mock_mqtt_component +from tests.common import async_fire_mqtt_message from tests.components.switch import common DEFAULT_CONFIG = { @@ -38,13 +38,7 @@ DEFAULT_CONFIG = { } -@pytest.fixture -def mock_publish(hass): - """Initialize components.""" - yield hass.loop.run_until_complete(async_mock_mqtt_component(hass)) - - -async def test_controlling_state_via_topic(hass, mock_publish): +async def test_controlling_state_via_topic(hass, mqtt_mock): """Test the controlling state via topic.""" assert await async_setup_component( hass, @@ -77,7 +71,7 @@ async def test_controlling_state_via_topic(hass, mock_publish): assert state.state == STATE_OFF -async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish): +async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock): """Test the sending MQTT commands in optimistic mode.""" fake_state = ha.State("switch.test", "on") @@ -107,23 +101,23 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish): await common.async_turn_on(hass, "switch.test") - mock_publish.async_publish.assert_called_once_with( + mqtt_mock.async_publish.assert_called_once_with( "command-topic", "beer on", 2, False ) - mock_publish.async_publish.reset_mock() + mqtt_mock.async_publish.reset_mock() state = hass.states.get("switch.test") assert state.state == STATE_ON await common.async_turn_off(hass, "switch.test") - mock_publish.async_publish.assert_called_once_with( + mqtt_mock.async_publish.assert_called_once_with( "command-topic", "beer off", 2, False ) state = hass.states.get("switch.test") assert state.state == STATE_OFF -async def test_controlling_state_via_topic_and_json_message(hass, mock_publish): +async def test_controlling_state_via_topic_and_json_message(hass, mqtt_mock): """Test the controlling state via topic and JSON message.""" assert await async_setup_component( hass, @@ -206,7 +200,7 @@ async def test_custom_availability_payload(hass, mqtt_mock): ) -async def test_custom_state_payload(hass, mock_publish): +async def test_custom_state_payload(hass, mqtt_mock): """Test the state payload.""" assert await async_setup_component( hass, @@ -276,7 +270,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog): ) -async def test_unique_id(hass): +async def test_unique_id(hass, mqtt_mock): """Test unique id option only creates one switch per unique_id.""" config = { switch.DOMAIN: [ @@ -296,7 +290,7 @@ async def test_unique_id(hass): }, ] } - await help_test_unique_id(hass, switch.DOMAIN, config) + await help_test_unique_id(hass, mqtt_mock, switch.DOMAIN, config) async def test_discovery_removal_switch(hass, mqtt_mock, caplog): diff --git a/tests/components/mqtt_json/test_device_tracker.py b/tests/components/mqtt_json/test_device_tracker.py index 864b3c232ed..d0f798c18ae 100644 --- a/tests/components/mqtt_json/test_device_tracker.py +++ b/tests/components/mqtt_json/test_device_tracker.py @@ -13,7 +13,7 @@ from homeassistant.const import CONF_PLATFORM from homeassistant.setup import async_setup_component from tests.async_mock import patch -from tests.common import async_fire_mqtt_message, async_mock_mqtt_component +from tests.common import async_fire_mqtt_message _LOGGER = logging.getLogger(__name__) @@ -28,9 +28,8 @@ LOCATION_MESSAGE_INCOMPLETE = {"longitude": 2.0} @pytest.fixture(autouse=True) -def setup_comp(hass): +def setup_comp(hass, mqtt_mock): """Initialize components.""" - hass.loop.run_until_complete(async_mock_mqtt_component(hass)) yaml_devices = hass.config.path(YAML_DEVICES) yield if os.path.isfile(yaml_devices): diff --git a/tests/components/mqtt_room/test_sensor.py b/tests/components/mqtt_room/test_sensor.py index 20aa34342d3..e17fbb4847d 100644 --- a/tests/components/mqtt_room/test_sensor.py +++ b/tests/components/mqtt_room/test_sensor.py @@ -9,7 +9,7 @@ from homeassistant.setup import async_setup_component from homeassistant.util import dt from tests.async_mock import patch -from tests.common import async_fire_mqtt_message, async_mock_mqtt_component +from tests.common import async_fire_mqtt_message DEVICE_ID = "123TESTMAC" NAME = "test_device" @@ -50,10 +50,8 @@ async def assert_distance(hass, distance): assert state.attributes.get("distance") == distance -async def test_room_update(hass): +async def test_room_update(hass, mqtt_mock): """Test the updating between rooms.""" - await async_mock_mqtt_component(hass) - assert await async_setup_component( hass, sensor.DOMAIN, diff --git a/tests/components/owntracks/test_device_tracker.py b/tests/components/owntracks/test_device_tracker.py index d71f0fe0aee..7f066346d5a 100644 --- a/tests/components/owntracks/test_device_tracker.py +++ b/tests/components/owntracks/test_device_tracker.py @@ -8,12 +8,7 @@ from homeassistant.const import STATE_NOT_HOME from homeassistant.setup import async_setup_component from tests.async_mock import patch -from tests.common import ( - MockConfigEntry, - async_fire_mqtt_message, - async_mock_mqtt_component, - mock_coro, -) +from tests.common import MockConfigEntry, async_fire_mqtt_message, mock_coro USER = "greg" DEVICE = "phone" @@ -286,13 +281,12 @@ BAD_JSON_SUFFIX = "** and it ends here ^^" @pytest.fixture -def setup_comp(hass, mock_device_tracker_conf): +def setup_comp(hass, mock_device_tracker_conf, mqtt_mock): """Initialize components.""" assert hass.loop.run_until_complete( async_setup_component(hass, "persistent_notification", {}) ) hass.loop.run_until_complete(async_setup_component(hass, "device_tracker", {})) - hass.loop.run_until_complete(async_mock_mqtt_component(hass)) hass.states.async_set("zone.inner", "zoning", INNER_ZONE) diff --git a/tests/components/snips/test_init.py b/tests/components/snips/test_init.py index 40fb30ddd19..dc69c10a7fb 100644 --- a/tests/components/snips/test_init.py +++ b/tests/components/snips/test_init.py @@ -10,18 +10,11 @@ from homeassistant.components.mqtt import MQTT_PUBLISH_SCHEMA import homeassistant.components.snips as snips from homeassistant.helpers.intent import ServiceIntentHandler, async_register -from tests.common import ( - async_fire_mqtt_message, - async_mock_intent, - async_mock_mqtt_component, - async_mock_service, -) +from tests.common import async_fire_mqtt_message, async_mock_intent, async_mock_service -async def test_snips_config(hass): +async def test_snips_config(hass, mqtt_mock): """Test Snips Config.""" - await async_mock_mqtt_component(hass) - result = await async_setup_component( hass, "snips", @@ -36,10 +29,8 @@ async def test_snips_config(hass): assert result -async def test_snips_bad_config(hass): +async def test_snips_bad_config(hass, mqtt_mock): """Test Snips bad config.""" - await async_mock_mqtt_component(hass) - result = await async_setup_component( hass, "snips", @@ -54,10 +45,8 @@ async def test_snips_bad_config(hass): assert not result -async def test_snips_config_feedback_on(hass): +async def test_snips_config_feedback_on(hass, mqtt_mock): """Test Snips Config.""" - await async_mock_mqtt_component(hass) - calls = async_mock_service(hass, "mqtt", "publish", MQTT_PUBLISH_SCHEMA) result = await async_setup_component( hass, "snips", {"snips": {"feedback_sounds": True}} @@ -74,10 +63,8 @@ async def test_snips_config_feedback_on(hass): assert calls[1].data["retain"] -async def test_snips_config_feedback_off(hass): +async def test_snips_config_feedback_off(hass, mqtt_mock): """Test Snips Config.""" - await async_mock_mqtt_component(hass) - calls = async_mock_service(hass, "mqtt", "publish", MQTT_PUBLISH_SCHEMA) result = await async_setup_component( hass, "snips", {"snips": {"feedback_sounds": False}} @@ -94,10 +81,8 @@ async def test_snips_config_feedback_off(hass): assert not calls[1].data["retain"] -async def test_snips_config_no_feedback(hass): +async def test_snips_config_no_feedback(hass, mqtt_mock): """Test Snips Config.""" - await async_mock_mqtt_component(hass) - calls = async_mock_service(hass, "snips", "say") result = await async_setup_component(hass, "snips", {"snips": {}}) assert result @@ -105,10 +90,8 @@ async def test_snips_config_no_feedback(hass): assert len(calls) == 0 -async def test_snips_intent(hass): +async def test_snips_intent(hass, mqtt_mock): """Test intent via Snips.""" - await async_mock_mqtt_component(hass) - result = await async_setup_component(hass, "snips", {"snips": {}}) assert result payload = """ @@ -152,10 +135,8 @@ async def test_snips_intent(hass): assert intent.text_input == "turn the lights green" -async def test_snips_service_intent(hass): +async def test_snips_service_intent(hass, mqtt_mock): """Test ServiceIntentHandler via Snips.""" - await async_mock_mqtt_component(hass) - hass.states.async_set("light.kitchen", "off") calls = async_mock_service(hass, "light", "turn_on") result = await async_setup_component(hass, "snips", {"snips": {}}) @@ -196,10 +177,8 @@ async def test_snips_service_intent(hass): assert "site_id" not in calls[0].data -async def test_snips_intent_with_duration(hass): +async def test_snips_intent_with_duration(hass, mqtt_mock): """Test intent with Snips duration.""" - await async_mock_mqtt_component(hass) - result = await async_setup_component(hass, "snips", {"snips": {}}) assert result payload = """ @@ -251,10 +230,8 @@ async def test_snips_intent_with_duration(hass): } -async def test_intent_speech_response(hass): +async def test_intent_speech_response(hass, mqtt_mock): """Test intent speech response via Snips.""" - await async_mock_mqtt_component(hass) - calls = async_mock_service(hass, "mqtt", "publish", MQTT_PUBLISH_SCHEMA) result = await async_setup_component(hass, "snips", {"snips": {}}) assert result @@ -292,10 +269,8 @@ async def test_intent_speech_response(hass): assert topic == "hermes/dialogueManager/endSession" -async def test_unknown_intent(hass, caplog): +async def test_unknown_intent(hass, caplog, mqtt_mock): """Test unknown intent.""" - await async_mock_mqtt_component(hass) - caplog.set_level(logging.WARNING) result = await async_setup_component(hass, "snips", {"snips": {}}) assert result @@ -315,10 +290,8 @@ async def test_unknown_intent(hass, caplog): assert "Received unknown intent unknownIntent" in caplog.text -async def test_snips_intent_user(hass): +async def test_snips_intent_user(hass, mqtt_mock): """Test intentName format user_XXX__intentName.""" - await async_mock_mqtt_component(hass) - result = await async_setup_component(hass, "snips", {"snips": {}}) assert result payload = """ @@ -341,10 +314,8 @@ async def test_snips_intent_user(hass): assert intent.intent_type == "Lights" -async def test_snips_intent_username(hass): +async def test_snips_intent_username(hass, mqtt_mock): """Test intentName format username:intentName.""" - await async_mock_mqtt_component(hass) - result = await async_setup_component(hass, "snips", {"snips": {}}) assert result payload = """ @@ -367,10 +338,8 @@ async def test_snips_intent_username(hass): assert intent.intent_type == "Lights" -async def test_snips_low_probability(hass, caplog): +async def test_snips_low_probability(hass, caplog, mqtt_mock): """Test intent via Snips.""" - await async_mock_mqtt_component(hass) - caplog.set_level(logging.WARNING) result = await async_setup_component( hass, "snips", {"snips": {"probability_threshold": 0.5}} @@ -393,10 +362,8 @@ async def test_snips_low_probability(hass, caplog): assert "Intent below probaility threshold 0.49 < 0.5" in caplog.text -async def test_intent_special_slots(hass): +async def test_intent_special_slots(hass, mqtt_mock): """Test intent special slot values via Snips.""" - await async_mock_mqtt_component(hass) - calls = async_mock_service(hass, "light", "turn_on") result = await async_setup_component(hass, "snips", {"snips": {}}) assert result diff --git a/tests/conftest.py b/tests/conftest.py index efaf1ff7dff..118774eb8e5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,9 +5,10 @@ import logging import pytest import requests_mock as _requests_mock -from homeassistant import util +from homeassistant import core as ha, util from homeassistant.auth.const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY from homeassistant.auth.providers import homeassistant, legacy_api_password +from homeassistant.components import mqtt from homeassistant.components.websocket_api.auth import ( TYPE_AUTH, TYPE_AUTH_OK, @@ -18,7 +19,7 @@ from homeassistant.exceptions import ServiceNotFound from homeassistant.setup import async_setup_component from homeassistant.util import location -from tests.async_mock import patch +from tests.async_mock import MagicMock, patch from tests.ignore_uncaught_exceptions import IGNORE_UNCAUGHT_EXCEPTIONS pytest.register_assert_rewrite("tests.common") @@ -27,6 +28,7 @@ from tests.common import ( # noqa: E402, isort:skip CLIENT_ID, INSTANCES, MockUser, + async_fire_mqtt_message, async_test_home_assistant, mock_storage as mock_storage, ) @@ -267,3 +269,46 @@ def fail_on_log_exception(request, monkeypatch): raise monkeypatch.setattr("homeassistant.util.logging.log_exception", log_exception) + + +@pytest.fixture +def mqtt_config(): + """Fixture to allow overriding MQTT config.""" + return None + + +@pytest.fixture +def mqtt_client_mock(hass): + """Fixture to mock MQTT client.""" + + @ha.callback + def _async_fire_mqtt_message(topic, payload, qos, retain): + async_fire_mqtt_message(hass, topic, payload, qos, retain) + + with patch("paho.mqtt.client.Client") as mock_client: + mock_client = mock_client.return_value + mock_client.connect.return_value = 0 + mock_client.subscribe.return_value = (0, 0) + mock_client.unsubscribe.return_value = (0, 0) + mock_client.publish.side_effect = _async_fire_mqtt_message + yield mock_client + + +@pytest.fixture +async def mqtt_mock(hass, mqtt_client_mock, mqtt_config): + """Fixture to mock MQTT component.""" + if mqtt_config is None: + mqtt_config = {mqtt.CONF_BROKER: "mock-broker"} + + result = await async_setup_component(hass, mqtt.DOMAIN, {mqtt.DOMAIN: mqtt_config}) + assert result + await hass.async_block_till_done() + + mqtt_component_mock = MagicMock(spec_set=hass.data["mqtt"], wraps=hass.data["mqtt"]) + hass.data["mqtt"].connected = mqtt_component_mock.connected + mqtt_component_mock._mqttc = mqtt_client_mock + + hass.data["mqtt"] = mqtt_component_mock + component = hass.data["mqtt"] + component.reset_mock() + return component