Improve type hints in zwave_js tests (#121594)

pull/121619/head
epenet 2024-07-09 20:13:47 +02:00 committed by GitHub
parent 31dc80c616
commit a4980cf0fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 33 deletions

View File

@ -1,9 +1,11 @@
"""Provide common Z-Wave JS fixtures."""
import asyncio
from collections.abc import Generator
import copy
import io
import json
from typing import Any
from unittest.mock import DEFAULT, AsyncMock, patch
import pytest
@ -20,13 +22,13 @@ from tests.common import MockConfigEntry, load_fixture
@pytest.fixture(name="addon_info_side_effect")
def addon_info_side_effect_fixture():
def addon_info_side_effect_fixture() -> Any | None:
"""Return the add-on info side effect."""
return None
@pytest.fixture(name="addon_info")
def mock_addon_info(addon_info_side_effect):
def mock_addon_info(addon_info_side_effect: Any | None) -> Generator[AsyncMock]:
"""Mock Supervisor add-on info."""
with patch(
"homeassistant.components.hassio.addon_manager.async_get_addon_info",
@ -44,13 +46,15 @@ def mock_addon_info(addon_info_side_effect):
@pytest.fixture(name="addon_store_info_side_effect")
def addon_store_info_side_effect_fixture():
def addon_store_info_side_effect_fixture() -> Any | None:
"""Return the add-on store info side effect."""
return None
@pytest.fixture(name="addon_store_info")
def mock_addon_store_info(addon_store_info_side_effect):
def mock_addon_store_info(
addon_store_info_side_effect: Any | None,
) -> Generator[AsyncMock]:
"""Mock Supervisor add-on info."""
with patch(
"homeassistant.components.hassio.addon_manager.async_get_addon_store_info",
@ -66,7 +70,7 @@ def mock_addon_store_info(addon_store_info_side_effect):
@pytest.fixture(name="addon_running")
def mock_addon_running(addon_store_info, addon_info):
def mock_addon_running(addon_store_info: AsyncMock, addon_info: AsyncMock) -> AsyncMock:
"""Mock add-on already running."""
addon_store_info.return_value = {
"available": True,
@ -81,7 +85,9 @@ def mock_addon_running(addon_store_info, addon_info):
@pytest.fixture(name="addon_installed")
def mock_addon_installed(addon_store_info, addon_info):
def mock_addon_installed(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> AsyncMock:
"""Mock add-on already installed but not running."""
addon_store_info.return_value = {
"available": True,
@ -96,23 +102,27 @@ def mock_addon_installed(addon_store_info, addon_info):
@pytest.fixture(name="addon_not_installed")
def mock_addon_not_installed(addon_store_info, addon_info):
def mock_addon_not_installed(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> AsyncMock:
"""Mock add-on not installed."""
addon_store_info.return_value["available"] = True
return addon_info
@pytest.fixture(name="addon_options")
def mock_addon_options(addon_info):
def mock_addon_options(addon_info: AsyncMock):
"""Mock add-on options."""
return addon_info.return_value["options"]
@pytest.fixture(name="set_addon_options_side_effect")
def set_addon_options_side_effect_fixture(addon_options):
def set_addon_options_side_effect_fixture(
addon_options: dict[str, Any],
) -> Any | None:
"""Return the set add-on options side effect."""
async def set_addon_options(hass: HomeAssistant, slug, options):
async def set_addon_options(hass: HomeAssistant, slug: str, options: dict) -> None:
"""Mock set add-on options."""
addon_options.update(options["options"])
@ -120,7 +130,9 @@ def set_addon_options_side_effect_fixture(addon_options):
@pytest.fixture(name="set_addon_options")
def mock_set_addon_options(set_addon_options_side_effect):
def mock_set_addon_options(
set_addon_options_side_effect: Any | None,
) -> Generator[AsyncMock]:
"""Mock set add-on options."""
with patch(
"homeassistant.components.hassio.addon_manager.async_set_addon_options",
@ -130,7 +142,9 @@ def mock_set_addon_options(set_addon_options_side_effect):
@pytest.fixture(name="install_addon_side_effect")
def install_addon_side_effect_fixture(addon_store_info, addon_info):
def install_addon_side_effect_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> Any | None:
"""Return the install add-on side effect."""
async def install_addon(hass: HomeAssistant, slug):
@ -149,7 +163,7 @@ def install_addon_side_effect_fixture(addon_store_info, addon_info):
@pytest.fixture(name="install_addon")
def mock_install_addon(install_addon_side_effect):
def mock_install_addon(install_addon_side_effect: Any | None) -> Generator[AsyncMock]:
"""Mock install add-on."""
with patch(
"homeassistant.components.hassio.addon_manager.async_install_addon",
@ -159,7 +173,7 @@ def mock_install_addon(install_addon_side_effect):
@pytest.fixture(name="update_addon")
def mock_update_addon():
def mock_update_addon() -> Generator[AsyncMock]:
"""Mock update add-on."""
with patch(
"homeassistant.components.hassio.addon_manager.async_update_addon"
@ -168,7 +182,9 @@ def mock_update_addon():
@pytest.fixture(name="start_addon_side_effect")
def start_addon_side_effect_fixture(addon_store_info, addon_info):
def start_addon_side_effect_fixture(
addon_store_info: AsyncMock, addon_info: AsyncMock
) -> Any | None:
"""Return the start add-on options side effect."""
async def start_addon(hass: HomeAssistant, slug):
@ -186,7 +202,7 @@ def start_addon_side_effect_fixture(addon_store_info, addon_info):
@pytest.fixture(name="start_addon")
def mock_start_addon(start_addon_side_effect):
def mock_start_addon(start_addon_side_effect: Any | None) -> Generator[AsyncMock]:
"""Mock start add-on."""
with patch(
"homeassistant.components.hassio.addon_manager.async_start_addon",
@ -196,7 +212,7 @@ def mock_start_addon(start_addon_side_effect):
@pytest.fixture(name="stop_addon")
def stop_addon_fixture():
def stop_addon_fixture() -> Generator[AsyncMock]:
"""Mock stop add-on."""
with patch(
"homeassistant.components.hassio.addon_manager.async_stop_addon"
@ -205,13 +221,13 @@ def stop_addon_fixture():
@pytest.fixture(name="restart_addon_side_effect")
def restart_addon_side_effect_fixture():
def restart_addon_side_effect_fixture() -> Any | None:
"""Return the restart add-on options side effect."""
return None
@pytest.fixture(name="restart_addon")
def mock_restart_addon(restart_addon_side_effect):
def mock_restart_addon(restart_addon_side_effect: Any | None) -> Generator[AsyncMock]:
"""Mock restart add-on."""
with patch(
"homeassistant.components.hassio.addon_manager.async_restart_addon",
@ -221,7 +237,7 @@ def mock_restart_addon(restart_addon_side_effect):
@pytest.fixture(name="uninstall_addon")
def uninstall_addon_fixture():
def uninstall_addon_fixture() -> Generator[AsyncMock]:
"""Mock uninstall add-on."""
with patch(
"homeassistant.components.hassio.addon_manager.async_uninstall_addon"
@ -230,7 +246,7 @@ def uninstall_addon_fixture():
@pytest.fixture(name="create_backup")
def create_backup_fixture():
def create_backup_fixture() -> Generator[AsyncMock]:
"""Mock create backup."""
with patch(
"homeassistant.components.hassio.addon_manager.async_create_backup"

View File

@ -4,7 +4,8 @@ import asyncio
from collections.abc import Generator
from copy import copy
from ipaddress import ip_address
from unittest.mock import DEFAULT, MagicMock, call, patch
from typing import Any
from unittest.mock import DEFAULT, AsyncMock, MagicMock, call, patch
import aiohttp
import pytest
@ -59,7 +60,7 @@ CP2652_ZIGBEE_DISCOVERY_INFO = usb.UsbServiceInfo(
@pytest.fixture(name="setup_entry")
def setup_entry_fixture():
def setup_entry_fixture() -> Generator[AsyncMock]:
"""Mock entry setup."""
with patch(
"homeassistant.components.zwave_js.async_setup_entry", return_value=True
@ -68,7 +69,7 @@ def setup_entry_fixture():
@pytest.fixture(name="supervisor")
def mock_supervisor_fixture():
def mock_supervisor_fixture() -> Generator[None]:
"""Mock Supervisor."""
with patch(
"homeassistant.components.zwave_js.config_flow.is_hassio", return_value=True
@ -77,19 +78,21 @@ def mock_supervisor_fixture():
@pytest.fixture(name="discovery_info")
def discovery_info_fixture():
def discovery_info_fixture() -> dict[str, Any]:
"""Return the discovery info from the supervisor."""
return DEFAULT
@pytest.fixture(name="discovery_info_side_effect")
def discovery_info_side_effect_fixture():
def discovery_info_side_effect_fixture() -> Any | None:
"""Return the discovery info from the supervisor."""
return None
@pytest.fixture(name="get_addon_discovery_info")
def mock_get_addon_discovery_info(discovery_info, discovery_info_side_effect):
def mock_get_addon_discovery_info(
discovery_info: dict[str, Any], discovery_info_side_effect: Any | None
) -> Generator[AsyncMock]:
"""Mock get add-on discovery info."""
with patch(
"homeassistant.components.hassio.addon_manager.async_get_addon_discovery_info",
@ -100,13 +103,15 @@ def mock_get_addon_discovery_info(discovery_info, discovery_info_side_effect):
@pytest.fixture(name="server_version_side_effect")
def server_version_side_effect_fixture():
def server_version_side_effect_fixture() -> Any | None:
"""Return the server version side effect."""
return None
@pytest.fixture(name="get_server_version", autouse=True)
def mock_get_server_version(server_version_side_effect, server_version_timeout):
def mock_get_server_version(
server_version_side_effect: Any | None, server_version_timeout: int
) -> Generator[AsyncMock]:
"""Mock server version."""
version_info = VersionInfo(
driver_version="mock-driver-version",
@ -130,18 +135,18 @@ def mock_get_server_version(server_version_side_effect, server_version_timeout):
@pytest.fixture(name="server_version_timeout")
def mock_server_version_timeout():
def mock_server_version_timeout() -> int:
"""Patch the timeout for getting server version."""
return SERVER_VERSION_TIMEOUT
@pytest.fixture(name="addon_setup_time", autouse=True)
def mock_addon_setup_time():
def mock_addon_setup_time() -> Generator[None]:
"""Mock add-on setup sleep time."""
with patch(
"homeassistant.components.zwave_js.config_flow.ADDON_SETUP_TIMEOUT", new=0
) as addon_setup_time:
yield addon_setup_time
):
yield
@pytest.fixture(name="serial_port")