Drop use of async_mock_mqtt_component (#37011)

This commit is contained in:
Erik Montnemery authored and GitHub committed 2020-06-22 23:59:50 +02:00
1 parent a2e2c35011
commit 280f49540e
25 files changed
+126 -209

No files matched your search

-51
View File
@@ -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
@@ -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):
+2 -2
View File
@@ -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):
+4 -5
View File
@@ -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):
+2 -2
View File
@@ -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):
+6 -13
View File
@@ -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(
+2 -2
View File
@@ -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):
+2 -4
View File
@@ -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(
+2 -2
View File
@@ -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):
+1 -1
View File
@@ -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):
+2 -2
View File
@@ -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):
+2 -2
View File
@@ -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):
+2 -2
View File
@@ -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):
+2 -2
View File
@@ -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):
+2 -2
View File
@@ -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):
+1 -1
View File
@@ -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):
+5 -8
View File
@@ -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
+10 -16
View File
@@ -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):