mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Drop use of async_mock_mqtt_component (#37011)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user