Use pytest for more MQTT tests (#36859)

* Use pytest for more MQTT tests

* Address review comments

* Break out PAHO client mock in separate fixture.

* tweak.
This commit is contained in:
Erik Montnemery 2020-06-22 22:02:29 +02:00 committed by GitHub
parent 3a83f4bdbe
commit a2e2c35011
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 570 additions and 509 deletions

View file

@ -366,11 +366,19 @@ def async_publish(
@bind_hass
def publish_template(
hass: HomeAssistantType, topic, payload_template, qos=None, retain=None
) -> None:
"""Publish message to an MQTT topic."""
hass.add_job(async_publish_template, hass, topic, payload_template, qos, retain)
@bind_hass
def async_publish_template(
hass: HomeAssistantType, topic, payload_template, qos=None, retain=None
) -> None:
"""Publish message to an MQTT topic using a template payload."""
data = _build_publish_data(topic, qos, retain)
data[ATTR_PAYLOAD_TEMPLATE] = payload_template
hass.services.call(DOMAIN, SERVICE_PUBLISH, data)
hass.async_create_task(hass.services.async_call(DOMAIN, SERVICE_PUBLISH, data))
def wrap_msg_callback(msg_callback: MessageCallbackType) -> MessageCallbackType:

View file

@ -344,10 +344,13 @@ async def async_mock_mqtt_component(hass, config=None):
assert result
await hass.async_block_till_done()
hass.data["mqtt"] = MagicMock(
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 = mock_client
hass.data["mqtt"] = mqtt_component_mock
return hass.data["mqtt"]

View file

@ -1,12 +1,52 @@
"""Test fixtures for mqtt component."""
import pytest
from tests.common import async_mock_mqtt_component
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_mock(loop, hass):
"""Fixture to mock MQTT."""
client = loop.run_until_complete(async_mock_mqtt_component(hass))
client.reset_mock()
return client
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

File diff suppressed because it is too large Load diff