Fix incorrect zeroconf type hint in tests (#118465)

* Fix incorrect `mock_async_zeroconf` type hint

* Adjust thread

* One more

* Fix mock_zeroconf also

* Adjust

* Adjust
This commit is contained in:
epenet 2024-05-30 21:57:09 +02:00 committed by GitHub
parent 1352c4e427
commit a5dc4cb1c7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 40 additions and 32 deletions

View file

@ -132,7 +132,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = {
"issue_registry": "IssueRegistry", "issue_registry": "IssueRegistry",
"legacy_auth": "LegacyApiPasswordAuthProvider", "legacy_auth": "LegacyApiPasswordAuthProvider",
"local_auth": "HassAuthProvider", "local_auth": "HassAuthProvider",
"mock_async_zeroconf": "None", "mock_async_zeroconf": "MagicMock",
"mock_bleak_scanner_start": "MagicMock", "mock_bleak_scanner_start": "MagicMock",
"mock_bluetooth": "None", "mock_bluetooth": "None",
"mock_bluetooth_adapters": "None", "mock_bluetooth_adapters": "None",
@ -140,7 +140,7 @@ _TEST_FIXTURES: dict[str, list[str] | str] = {
"mock_get_source_ip": "None", "mock_get_source_ip": "None",
"mock_hass_config": "None", "mock_hass_config": "None",
"mock_hass_config_yaml": "None", "mock_hass_config_yaml": "None",
"mock_zeroconf": "None", "mock_zeroconf": "MagicMock",
"mqtt_client_mock": "MqttMockPahoClient", "mqtt_client_mock": "MqttMockPahoClient",
"mqtt_mock": "MqttMockHAClient", "mqtt_mock": "MqttMockHAClient",
"mqtt_mock_entry": "MqttMockHAClientGenerator", "mqtt_mock_entry": "MqttMockHAClientGenerator",

View file

@ -297,7 +297,7 @@ async def test_homekit_setup(
async def test_homekit_setup_ip_address( async def test_homekit_setup_ip_address(
hass: HomeAssistant, hk_driver, mock_async_zeroconf: None hass: HomeAssistant, hk_driver, mock_async_zeroconf: MagicMock
) -> None: ) -> None:
"""Test setup with given IP address.""" """Test setup with given IP address."""
entry = MockConfigEntry( entry = MockConfigEntry(
@ -344,7 +344,7 @@ async def test_homekit_setup_ip_address(
async def test_homekit_with_single_advertise_ips( async def test_homekit_with_single_advertise_ips(
hass: HomeAssistant, hass: HomeAssistant,
hk_driver, hk_driver,
mock_async_zeroconf: None, mock_async_zeroconf: MagicMock,
hass_storage: dict[str, Any], hass_storage: dict[str, Any],
) -> None: ) -> None:
"""Test setup with a single advertise ips.""" """Test setup with a single advertise ips."""
@ -379,7 +379,7 @@ async def test_homekit_with_single_advertise_ips(
async def test_homekit_with_many_advertise_ips( async def test_homekit_with_many_advertise_ips(
hass: HomeAssistant, hass: HomeAssistant,
hk_driver, hk_driver,
mock_async_zeroconf: None, mock_async_zeroconf: MagicMock,
hass_storage: dict[str, Any], hass_storage: dict[str, Any],
) -> None: ) -> None:
"""Test setup with many advertise ips.""" """Test setup with many advertise ips."""

View file

@ -41,7 +41,9 @@ DATASET_NO_CHANNEL = bytes.fromhex(
async def test_import_dataset( async def test_import_dataset(
hass: HomeAssistant, mock_async_zeroconf: None, issue_registry: ir.IssueRegistry hass: HomeAssistant,
mock_async_zeroconf: MagicMock,
issue_registry: ir.IssueRegistry,
) -> None: ) -> None:
"""Test the active dataset is imported at setup.""" """Test the active dataset is imported at setup."""
add_service_listener_called = asyncio.Event() add_service_listener_called = asyncio.Event()

View file

@ -2,7 +2,7 @@
import asyncio import asyncio
from typing import Any from typing import Any
from unittest.mock import ANY, AsyncMock, patch from unittest.mock import ANY, AsyncMock, MagicMock, patch
import pytest import pytest
from python_otbr_api.tlv_parser import TLVError from python_otbr_api.tlv_parser import TLVError
@ -710,7 +710,7 @@ async def test_set_preferred_extended_address(hass: HomeAssistant) -> None:
async def test_automatically_set_preferred_dataset( async def test_automatically_set_preferred_dataset(
hass: HomeAssistant, mock_async_zeroconf: None hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None: ) -> None:
"""Test automatically setting the first dataset as the preferred dataset.""" """Test automatically setting the first dataset as the preferred dataset."""
add_service_listener_called = asyncio.Event() add_service_listener_called = asyncio.Event()
@ -775,7 +775,7 @@ async def test_automatically_set_preferred_dataset(
async def test_automatically_set_preferred_dataset_own_and_other_router( async def test_automatically_set_preferred_dataset_own_and_other_router(
hass: HomeAssistant, mock_async_zeroconf: None hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None: ) -> None:
"""Test automatically setting the first dataset as the preferred dataset. """Test automatically setting the first dataset as the preferred dataset.
@ -854,7 +854,7 @@ async def test_automatically_set_preferred_dataset_own_and_other_router(
async def test_automatically_set_preferred_dataset_other_router( async def test_automatically_set_preferred_dataset_other_router(
hass: HomeAssistant, mock_async_zeroconf: None hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None: ) -> None:
"""Test automatically setting the first dataset as the preferred dataset. """Test automatically setting the first dataset as the preferred dataset.
@ -922,7 +922,7 @@ async def test_automatically_set_preferred_dataset_other_router(
async def test_automatically_set_preferred_dataset_no_router( async def test_automatically_set_preferred_dataset_no_router(
hass: HomeAssistant, mock_async_zeroconf: None hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None: ) -> None:
"""Test automatically setting the first dataset as the preferred dataset. """Test automatically setting the first dataset as the preferred dataset.

View file

@ -1,7 +1,7 @@
"""Test the thread websocket API.""" """Test the thread websocket API."""
import dataclasses import dataclasses
from unittest.mock import Mock, patch from unittest.mock import MagicMock, Mock, patch
import pytest import pytest
from syrupy.assertion import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
@ -182,7 +182,7 @@ def ndb() -> Mock:
async def test_diagnostics( async def test_diagnostics(
hass: HomeAssistant, hass: HomeAssistant,
mock_async_zeroconf: None, mock_async_zeroconf: MagicMock,
ndb: Mock, ndb: Mock,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,

View file

@ -1,6 +1,6 @@
"""Test the thread websocket API.""" """Test the thread websocket API."""
from unittest.mock import ANY, AsyncMock, Mock from unittest.mock import ANY, AsyncMock, MagicMock, Mock
import pytest import pytest
from zeroconf.asyncio import AsyncServiceInfo from zeroconf.asyncio import AsyncServiceInfo
@ -24,7 +24,9 @@ from . import (
) )
async def test_discover_routers(hass: HomeAssistant, mock_async_zeroconf: None) -> None: async def test_discover_routers(
hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None:
"""Test discovering thread routers.""" """Test discovering thread routers."""
mock_async_zeroconf.async_add_service_listener = AsyncMock() mock_async_zeroconf.async_add_service_listener = AsyncMock()
mock_async_zeroconf.async_remove_service_listener = AsyncMock() mock_async_zeroconf.async_remove_service_listener = AsyncMock()
@ -151,7 +153,7 @@ async def test_discover_routers(hass: HomeAssistant, mock_async_zeroconf: None)
], ],
) )
async def test_discover_routers_unconfigured( async def test_discover_routers_unconfigured(
hass: HomeAssistant, mock_async_zeroconf: None, data, unconfigured hass: HomeAssistant, mock_async_zeroconf: MagicMock, data, unconfigured
) -> None: ) -> None:
"""Test discovering thread routers and setting the unconfigured flag.""" """Test discovering thread routers and setting the unconfigured flag."""
mock_async_zeroconf.async_add_service_listener = AsyncMock() mock_async_zeroconf.async_add_service_listener = AsyncMock()
@ -197,7 +199,7 @@ async def test_discover_routers_unconfigured(
"data", [ROUTER_DISCOVERY_HASS_BAD_DATA, ROUTER_DISCOVERY_HASS_MISSING_DATA] "data", [ROUTER_DISCOVERY_HASS_BAD_DATA, ROUTER_DISCOVERY_HASS_MISSING_DATA]
) )
async def test_discover_routers_bad_or_missing_optional_data( async def test_discover_routers_bad_or_missing_optional_data(
hass: HomeAssistant, mock_async_zeroconf: None, data hass: HomeAssistant, mock_async_zeroconf: MagicMock, data
) -> None: ) -> None:
"""Test discovering thread routers with bad or missing vendor mDNS data.""" """Test discovering thread routers with bad or missing vendor mDNS data."""
mock_async_zeroconf.async_add_service_listener = AsyncMock() mock_async_zeroconf.async_add_service_listener = AsyncMock()
@ -247,7 +249,7 @@ async def test_discover_routers_bad_or_missing_optional_data(
], ],
) )
async def test_discover_routers_bad_or_missing_mandatory_data( async def test_discover_routers_bad_or_missing_mandatory_data(
hass: HomeAssistant, mock_async_zeroconf: None, service hass: HomeAssistant, mock_async_zeroconf: MagicMock, service
) -> None: ) -> None:
"""Test discovering thread routers with missing mandatory mDNS data.""" """Test discovering thread routers with missing mandatory mDNS data."""
mock_async_zeroconf.async_add_service_listener = AsyncMock() mock_async_zeroconf.async_add_service_listener = AsyncMock()
@ -281,7 +283,7 @@ async def test_discover_routers_bad_or_missing_mandatory_data(
async def test_discover_routers_get_service_info_fails( async def test_discover_routers_get_service_info_fails(
hass: HomeAssistant, mock_async_zeroconf: None hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None: ) -> None:
"""Test discovering thread routers with invalid mDNS data.""" """Test discovering thread routers with invalid mDNS data."""
mock_async_zeroconf.async_add_service_listener = AsyncMock() mock_async_zeroconf.async_add_service_listener = AsyncMock()
@ -311,7 +313,7 @@ async def test_discover_routers_get_service_info_fails(
async def test_discover_routers_update_unchanged( async def test_discover_routers_update_unchanged(
hass: HomeAssistant, mock_async_zeroconf: None hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None: ) -> None:
"""Test discovering thread routers with identical mDNS data in update.""" """Test discovering thread routers with identical mDNS data in update."""
mock_async_zeroconf.async_add_service_listener = AsyncMock() mock_async_zeroconf.async_add_service_listener = AsyncMock()
@ -353,7 +355,7 @@ async def test_discover_routers_update_unchanged(
async def test_discover_routers_stop_twice( async def test_discover_routers_stop_twice(
hass: HomeAssistant, mock_async_zeroconf: None hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None: ) -> None:
"""Test discovering thread routers stopping discovery twice.""" """Test discovering thread routers stopping discovery twice."""
mock_async_zeroconf.async_add_service_listener = AsyncMock() mock_async_zeroconf.async_add_service_listener = AsyncMock()

View file

@ -1,6 +1,6 @@
"""Test the thread websocket API.""" """Test the thread websocket API."""
from unittest.mock import ANY, AsyncMock from unittest.mock import ANY, AsyncMock, MagicMock
from zeroconf.asyncio import AsyncServiceInfo from zeroconf.asyncio import AsyncServiceInfo
@ -315,7 +315,9 @@ async def test_set_preferred_dataset_wrong_id(
async def test_discover_routers( async def test_discover_routers(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator, mock_async_zeroconf: None hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
mock_async_zeroconf: MagicMock,
) -> None: ) -> None:
"""Test discovering thread routers.""" """Test discovering thread routers."""
mock_async_zeroconf.async_add_service_listener = AsyncMock() mock_async_zeroconf.async_add_service_listener = AsyncMock()

View file

@ -1,7 +1,7 @@
"""Test Zeroconf component setup process.""" """Test Zeroconf component setup process."""
from typing import Any from typing import Any
from unittest.mock import call, patch from unittest.mock import MagicMock, call, patch
import pytest import pytest
from zeroconf import ( from zeroconf import (
@ -148,7 +148,7 @@ def get_zeroconf_info_mock_model(model):
return mock_zc_info return mock_zc_info
async def test_setup(hass: HomeAssistant, mock_async_zeroconf: None) -> None: async def test_setup(hass: HomeAssistant, mock_async_zeroconf: MagicMock) -> None:
"""Test configured options for a device are loaded via config entry.""" """Test configured options for a device are loaded via config entry."""
mock_zc = { mock_zc = {
"_http._tcp.local.": [ "_http._tcp.local.": [
@ -238,7 +238,7 @@ async def test_setup_with_overly_long_url_and_name(
async def test_setup_with_defaults( async def test_setup_with_defaults(
hass: HomeAssistant, mock_zeroconf: None, mock_async_zeroconf: None hass: HomeAssistant, mock_zeroconf: MagicMock, mock_async_zeroconf: None
) -> None: ) -> None:
"""Test default interface config.""" """Test default interface config."""
with ( with (
@ -994,7 +994,9 @@ async def test_info_from_service_can_return_ipv6(hass: HomeAssistant) -> None:
assert info.host == "fd11:1111:1111:0:1234:1234:1234:1234" assert info.host == "fd11:1111:1111:0:1234:1234:1234:1234"
async def test_get_instance(hass: HomeAssistant, mock_async_zeroconf: None) -> None: async def test_get_instance(
hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None:
"""Test we get an instance.""" """Test we get an instance."""
assert await async_setup_component(hass, zeroconf.DOMAIN, {zeroconf.DOMAIN: {}}) assert await async_setup_component(hass, zeroconf.DOMAIN, {zeroconf.DOMAIN: {}})
assert await zeroconf.async_get_async_instance(hass) is mock_async_zeroconf assert await zeroconf.async_get_async_instance(hass) is mock_async_zeroconf
@ -1285,7 +1287,7 @@ async def test_async_detect_interfaces_explicitly_set_ipv6_freebsd(
) )
async def test_no_name(hass: HomeAssistant, mock_async_zeroconf: None) -> None: async def test_no_name(hass: HomeAssistant, mock_async_zeroconf: MagicMock) -> None:
"""Test fallback to Home for mDNS announcement if the name is missing.""" """Test fallback to Home for mDNS announcement if the name is missing."""
hass.config.location_name = "" hass.config.location_name = ""
with patch("homeassistant.components.zeroconf.HaZeroconf"): with patch("homeassistant.components.zeroconf.HaZeroconf"):
@ -1299,7 +1301,7 @@ async def test_no_name(hass: HomeAssistant, mock_async_zeroconf: None) -> None:
async def test_setup_with_disallowed_characters_in_local_name( async def test_setup_with_disallowed_characters_in_local_name(
hass: HomeAssistant, mock_async_zeroconf: None, caplog: pytest.LogCaptureFixture hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None: ) -> None:
"""Test we still setup with disallowed characters in the location name.""" """Test we still setup with disallowed characters in the location name."""
with ( with (
@ -1323,7 +1325,7 @@ async def test_setup_with_disallowed_characters_in_local_name(
async def test_start_with_frontend( async def test_start_with_frontend(
hass: HomeAssistant, mock_async_zeroconf: None hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None: ) -> None:
"""Test we start with the frontend.""" """Test we start with the frontend."""
with patch("homeassistant.components.zeroconf.HaZeroconf"): with patch("homeassistant.components.zeroconf.HaZeroconf"):

View file

@ -1190,7 +1190,7 @@ def disable_translations_once(translations_once):
@pytest.fixture @pytest.fixture
def mock_zeroconf() -> Generator[None, None, None]: def mock_zeroconf() -> Generator[MagicMock, None, None]:
"""Mock zeroconf.""" """Mock zeroconf."""
from zeroconf import DNSCache # pylint: disable=import-outside-toplevel from zeroconf import DNSCache # pylint: disable=import-outside-toplevel
@ -1206,7 +1206,7 @@ def mock_zeroconf() -> Generator[None, None, None]:
@pytest.fixture @pytest.fixture
def mock_async_zeroconf(mock_zeroconf: None) -> Generator[None, None, None]: def mock_async_zeroconf(mock_zeroconf: MagicMock) -> Generator[MagicMock, None, None]:
"""Mock AsyncZeroconf.""" """Mock AsyncZeroconf."""
from zeroconf import DNSCache, Zeroconf # pylint: disable=import-outside-toplevel from zeroconf import DNSCache, Zeroconf # pylint: disable=import-outside-toplevel
from zeroconf.asyncio import ( # pylint: disable=import-outside-toplevel from zeroconf.asyncio import ( # pylint: disable=import-outside-toplevel