Correct type hints mqtt_client_mock and move new generator type (#87527)

* Correct mqtt_client_mock and move MqttMockType

* Rename MqttMockType to MqttMockGenerator

* Make types more specific

* adjust returntype _setup_mqtt_entry

* Correct return type _setup_mqtt_entry

* Update tests/conftest.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update tests/conftest.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

---------

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
Jan Bouwhuis 2023-02-06 15:05:06 +01:00 committed by GitHub
parent 212e172be2
commit 4ad386d794
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 17 deletions

View File

@ -55,7 +55,13 @@ from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util, location from homeassistant.util import dt as dt_util, location
from .ignore_uncaught_exceptions import IGNORE_UNCAUGHT_EXCEPTIONS from .ignore_uncaught_exceptions import IGNORE_UNCAUGHT_EXCEPTIONS
from .typing import ClientSessionGenerator, WebSocketGenerator from .typing import (
ClientSessionGenerator,
MqttMockHAClient,
MqttMockHAClientGenerator,
MqttMockPahoClient,
WebSocketGenerator,
)
pytest.register_assert_rewrite("tests.common") pytest.register_assert_rewrite("tests.common")
@ -89,8 +95,6 @@ def _utcnow():
dt_util.utcnow = _utcnow dt_util.utcnow = _utcnow
event.time_tracker_utcnow = _utcnow event.time_tracker_utcnow = _utcnow
MqttMockType = Callable[..., Coroutine[Any, Any, MagicMock]]
def pytest_addoption(parser): def pytest_addoption(parser):
"""Register custom pytest options.""" """Register custom pytest options."""
@ -718,7 +722,7 @@ def mqtt_config_entry_data() -> dict[str, Any] | None:
@pytest.fixture @pytest.fixture
def mqtt_client_mock(hass: HomeAssistant) -> Generator[Any, MagicMock, None]: def mqtt_client_mock(hass: HomeAssistant) -> Generator[MqttMockPahoClient, None, None]:
"""Fixture to mock MQTT client.""" """Fixture to mock MQTT client."""
mid: int = 0 mid: int = 0
@ -765,10 +769,10 @@ def mqtt_client_mock(hass: HomeAssistant) -> Generator[Any, MagicMock, None]:
@pytest.fixture @pytest.fixture
async def mqtt_mock( async def mqtt_mock(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_client_mock: MagicMock, mqtt_client_mock: MqttMockPahoClient,
mqtt_config_entry_data: dict[str, Any] | None, mqtt_config_entry_data: dict[str, Any] | None,
mqtt_mock_entry_no_yaml_config: Callable[..., Coroutine[Any, Any, MagicMock]], mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> AsyncGenerator[MagicMock, None]: ) -> AsyncGenerator[MqttMockHAClient, None]:
"""Fixture to mock MQTT component.""" """Fixture to mock MQTT component."""
return await mqtt_mock_entry_no_yaml_config() return await mqtt_mock_entry_no_yaml_config()
@ -776,9 +780,9 @@ async def mqtt_mock(
@asynccontextmanager @asynccontextmanager
async def _mqtt_mock_entry( async def _mqtt_mock_entry(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_client_mock: MagicMock, mqtt_client_mock: MqttMockPahoClient,
mqtt_config_entry_data: dict[str, Any] | None, mqtt_config_entry_data: dict[str, Any] | None,
) -> AsyncGenerator[Callable[..., Coroutine[Any, Any, Any]], None]: ) -> AsyncGenerator[MqttMockHAClientGenerator, None]:
"""Fixture to mock a delayed setup of the MQTT config entry.""" """Fixture to mock a delayed setup of the MQTT config entry."""
# Local import to avoid processing MQTT modules when running a testcase # Local import to avoid processing MQTT modules when running a testcase
# which does not use MQTT. # which does not use MQTT.
@ -822,12 +826,12 @@ async def _mqtt_mock_entry(
return mock_mqtt_instance return mock_mqtt_instance
def create_mock_mqtt(*args, **kwargs) -> MagicMock: def create_mock_mqtt(*args, **kwargs) -> MqttMockHAClient:
"""Create a mock based on mqtt.MQTT.""" """Create a mock based on mqtt.MQTT."""
nonlocal mock_mqtt_instance nonlocal mock_mqtt_instance
nonlocal real_mqtt_instance nonlocal real_mqtt_instance
real_mqtt_instance = real_mqtt(*args, **kwargs) real_mqtt_instance = real_mqtt(*args, **kwargs)
mock_mqtt_instance = MagicMock( mock_mqtt_instance = MqttMockHAClient(
return_value=real_mqtt_instance, return_value=real_mqtt_instance,
spec_set=real_mqtt_instance, spec_set=real_mqtt_instance,
wraps=real_mqtt_instance, wraps=real_mqtt_instance,
@ -841,9 +845,9 @@ async def _mqtt_mock_entry(
@pytest.fixture @pytest.fixture
async def mqtt_mock_entry_no_yaml_config( async def mqtt_mock_entry_no_yaml_config(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_client_mock: MagicMock, mqtt_client_mock: MqttMockPahoClient,
mqtt_config_entry_data: dict[str, Any] | None, mqtt_config_entry_data: dict[str, Any] | None,
) -> AsyncGenerator[MqttMockType, None,]: ) -> AsyncGenerator[MqttMockHAClientGenerator, None]:
"""Set up an MQTT config entry without MQTT yaml config.""" """Set up an MQTT config entry without MQTT yaml config."""
async def _async_setup_config_entry( async def _async_setup_config_entry(
@ -854,7 +858,7 @@ async def mqtt_mock_entry_no_yaml_config(
await hass.async_block_till_done() await hass.async_block_till_done()
return True return True
async def _setup_mqtt_entry() -> Callable[..., Coroutine[Any, Any, MagicMock]]: async def _setup_mqtt_entry() -> MqttMockHAClient:
"""Set up the MQTT config entry.""" """Set up the MQTT config entry."""
return await mqtt_mock_entry(_async_setup_config_entry) return await mqtt_mock_entry(_async_setup_config_entry)
@ -867,9 +871,9 @@ async def mqtt_mock_entry_no_yaml_config(
@pytest.fixture @pytest.fixture
async def mqtt_mock_entry_with_yaml_config( async def mqtt_mock_entry_with_yaml_config(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_client_mock: MagicMock, mqtt_client_mock: MqttMockPahoClient,
mqtt_config_entry_data: dict[str, Any] | None, mqtt_config_entry_data: dict[str, Any] | None,
) -> AsyncGenerator[MqttMockType, None,]: ) -> AsyncGenerator[MqttMockHAClientGenerator, None]:
"""Set up an MQTT config entry with MQTT yaml config.""" """Set up an MQTT config entry with MQTT yaml config."""
async def _async_do_not_setup_config_entry( async def _async_do_not_setup_config_entry(
@ -878,7 +882,7 @@ async def mqtt_mock_entry_with_yaml_config(
"""Do nothing.""" """Do nothing."""
return True return True
async def _setup_mqtt_entry() -> Callable[..., Coroutine[Any, Any, MagicMock]]: async def _setup_mqtt_entry() -> MqttMockHAClient:
"""Set up the MQTT config entry.""" """Set up the MQTT config entry."""
return await mqtt_mock_entry(_async_do_not_setup_config_entry) return await mqtt_mock_entry(_async_do_not_setup_config_entry)

View File

@ -3,9 +3,16 @@ from __future__ import annotations
from collections.abc import Callable, Coroutine from collections.abc import Callable, Coroutine
from typing import Any from typing import Any
from unittest.mock import MagicMock
from aiohttp import ClientWebSocketResponse from aiohttp import ClientWebSocketResponse
from aiohttp.test_utils import TestClient from aiohttp.test_utils import TestClient
ClientSessionGenerator = Callable[..., Coroutine[Any, Any, TestClient]] ClientSessionGenerator = Callable[..., Coroutine[Any, Any, TestClient]]
MqttMockPahoClient = MagicMock
"""MagicMock for `paho.mqtt.client.Client`"""
MqttMockHAClient = MagicMock
"""MagicMock for `homeassistant.components.mqtt.MQTT`."""
MqttMockHAClientGenerator = Callable[..., Coroutine[Any, Any, MqttMockHAClient]]
"""MagicMock generator for `homeassistant.components.mqtt.MQTT`."""
WebSocketGenerator = Callable[..., Coroutine[Any, Any, ClientWebSocketResponse]] WebSocketGenerator = Callable[..., Coroutine[Any, Any, ClientWebSocketResponse]]