Set up mqtt tests from client fixture of mqtt entry setup fixture, not both (#120274)

* Fix entry setup and cleanup issues in mqtt tests

* Reduce changes by using mqtt_client_mock alias

* Reduce sleep time where possibe
This commit is contained in:
Jan Bouwhuis 2024-06-24 19:42:32 +02:00 committed by GitHub
parent 1e5f4c2d75
commit a7200a70b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 339 additions and 448 deletions

View File

@ -1,12 +1,20 @@
"""Test fixtures for mqtt component."""
import asyncio
from random import getrandbits
from typing import Any
from unittest.mock import patch
import pytest
from typing_extensions import Generator
from typing_extensions import AsyncGenerator, Generator
from homeassistant.components import mqtt
from homeassistant.components.mqtt.models import ReceiveMessage
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED
from homeassistant.core import HomeAssistant, callback
from tests.common import MockConfigEntry
from tests.typing import MqttMockPahoClient
ENTRY_DEFAULT_BIRTH_MESSAGE = {
mqtt.CONF_BROKER: "mock-broker",
@ -39,3 +47,35 @@ def mock_temp_dir(temp_dir_prefix: str) -> Generator[str]:
f"home-assistant-mqtt-{temp_dir_prefix}-{getrandbits(10):03x}",
) as mocked_temp_dir:
yield mocked_temp_dir
@pytest.fixture
async def setup_with_birth_msg_client_mock(
hass: HomeAssistant,
mqtt_config_entry_data: dict[str, Any] | None,
mqtt_client_mock: MqttMockPahoClient,
) -> AsyncGenerator[MqttMockPahoClient]:
"""Test sending birth message."""
birth = asyncio.Event()
with (
patch("homeassistant.components.mqtt.client.INITIAL_SUBSCRIBE_COOLDOWN", 0.0),
patch("homeassistant.components.mqtt.client.DISCOVERY_COOLDOWN", 0.0),
patch("homeassistant.components.mqtt.client.SUBSCRIBE_COOLDOWN", 0.0),
):
entry = MockConfigEntry(
domain=mqtt.DOMAIN, data={mqtt.CONF_BROKER: "test-broker"}
)
entry.add_to_hass(hass)
hass.config.components.add(mqtt.DOMAIN)
assert await hass.config_entries.async_setup(entry.entry_id)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
@callback
def wait_birth(msg: ReceiveMessage) -> None:
"""Handle birth message."""
birth.set()
await mqtt.async_subscribe(hass, "homeassistant/status", wait_birth)
await hass.async_block_till_done()
await birth.wait()
yield mqtt_client_mock

View File

@ -1531,7 +1531,7 @@ async def test_mqtt_discovery_unsubscribe_once(
async def async_step_mqtt(self, discovery_info: MqttServiceInfo) -> FlowResult:
"""Test mqtt step."""
await asyncio.sleep(0.1)
await asyncio.sleep(0)
return self.async_abort(reason="already_configured")
mock_platform(hass, "comp.config_flow", None)
@ -1573,7 +1573,7 @@ async def test_mqtt_discovery_unsubscribe_once(
async_fire_mqtt_message(hass, "comp/discovery/bla/config", "")
async_fire_mqtt_message(hass, "comp/discovery/bla/config", "")
await wait_unsub.wait()
await asyncio.sleep(0.2)
await asyncio.sleep(0)
await hass.async_block_till_done(wait_background_tasks=True)
mqtt_client_mock.unsubscribe.assert_called_once_with(["comp/discovery/#"])
await hass.async_block_till_done(wait_background_tasks=True)

File diff suppressed because it is too large Load Diff