mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 13:47:35 +00:00
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:
parent
3a83f4bdbe
commit
a2e2c35011
@ -366,11 +366,19 @@ def async_publish(
|
|||||||
@bind_hass
|
@bind_hass
|
||||||
def publish_template(
|
def publish_template(
|
||||||
hass: HomeAssistantType, topic, payload_template, qos=None, retain=None
|
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:
|
) -> None:
|
||||||
"""Publish message to an MQTT topic using a template payload."""
|
"""Publish message to an MQTT topic using a template payload."""
|
||||||
data = _build_publish_data(topic, qos, retain)
|
data = _build_publish_data(topic, qos, retain)
|
||||||
data[ATTR_PAYLOAD_TEMPLATE] = payload_template
|
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:
|
def wrap_msg_callback(msg_callback: MessageCallbackType) -> MessageCallbackType:
|
||||||
|
@ -344,10 +344,13 @@ async def async_mock_mqtt_component(hass, config=None):
|
|||||||
assert result
|
assert result
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
hass.data["mqtt"] = MagicMock(
|
mqtt_component_mock = MagicMock(
|
||||||
spec_set=hass.data["mqtt"], wraps=hass.data["mqtt"]
|
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"]
|
return hass.data["mqtt"]
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,12 +1,52 @@
|
|||||||
"""Test fixtures for mqtt component."""
|
"""Test fixtures for mqtt component."""
|
||||||
import pytest
|
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
|
@pytest.fixture
|
||||||
def mqtt_mock(loop, hass):
|
def mqtt_config():
|
||||||
"""Fixture to mock MQTT."""
|
"""Fixture to allow overriding MQTT config."""
|
||||||
client = loop.run_until_complete(async_mock_mqtt_component(hass))
|
return None
|
||||||
client.reset_mock()
|
|
||||||
return client
|
|
||||||
|
@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
Loading…
x
Reference in New Issue
Block a user