Skip to content

Commit

Permalink
Fix incorrect zeroconf type hint in tests (home-assistant#118465)
Browse files Browse the repository at this point in the history
* Fix incorrect `mock_async_zeroconf` type hint

* Adjust thread

* One more

* Fix mock_zeroconf also

* Adjust

* Adjust
  • Loading branch information
epenet authored May 30, 2024
1 parent 1352c4e commit a5dc4cb
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 32 deletions.
4 changes: 2 additions & 2 deletions pylint/plugins/hass_enforce_type_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ class ClassTypeHintMatch:
"issue_registry": "IssueRegistry",
"legacy_auth": "LegacyApiPasswordAuthProvider",
"local_auth": "HassAuthProvider",
"mock_async_zeroconf": "None",
"mock_async_zeroconf": "MagicMock",
"mock_bleak_scanner_start": "MagicMock",
"mock_bluetooth": "None",
"mock_bluetooth_adapters": "None",
"mock_device_tracker_conf": "list[Device]",
"mock_get_source_ip": "None",
"mock_hass_config": "None",
"mock_hass_config_yaml": "None",
"mock_zeroconf": "None",
"mock_zeroconf": "MagicMock",
"mqtt_client_mock": "MqttMockPahoClient",
"mqtt_mock": "MqttMockHAClient",
"mqtt_mock_entry": "MqttMockHAClientGenerator",
Expand Down
6 changes: 3 additions & 3 deletions tests/components/homekit/test_homekit.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ async def test_homekit_setup(


async def test_homekit_setup_ip_address(
hass: HomeAssistant, hk_driver, mock_async_zeroconf: None
hass: HomeAssistant, hk_driver, mock_async_zeroconf: MagicMock
) -> None:
"""Test setup with given IP address."""
entry = MockConfigEntry(
Expand Down Expand Up @@ -344,7 +344,7 @@ async def test_homekit_setup_ip_address(
async def test_homekit_with_single_advertise_ips(
hass: HomeAssistant,
hk_driver,
mock_async_zeroconf: None,
mock_async_zeroconf: MagicMock,
hass_storage: dict[str, Any],
) -> None:
"""Test setup with a single advertise ips."""
Expand Down Expand Up @@ -379,7 +379,7 @@ async def test_homekit_with_single_advertise_ips(
async def test_homekit_with_many_advertise_ips(
hass: HomeAssistant,
hk_driver,
mock_async_zeroconf: None,
mock_async_zeroconf: MagicMock,
hass_storage: dict[str, Any],
) -> None:
"""Test setup with many advertise ips."""
Expand Down
4 changes: 3 additions & 1 deletion tests/components/otbr/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@


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:
"""Test the active dataset is imported at setup."""
add_service_listener_called = asyncio.Event()
Expand Down
10 changes: 5 additions & 5 deletions tests/components/thread/test_dataset_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import asyncio
from typing import Any
from unittest.mock import ANY, AsyncMock, patch
from unittest.mock import ANY, AsyncMock, MagicMock, patch

import pytest
from python_otbr_api.tlv_parser import TLVError
Expand Down Expand Up @@ -710,7 +710,7 @@ async def test_set_preferred_extended_address(hass: HomeAssistant) -> None:


async def test_automatically_set_preferred_dataset(
hass: HomeAssistant, mock_async_zeroconf: None
hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None:
"""Test automatically setting the first dataset as the preferred dataset."""
add_service_listener_called = asyncio.Event()
Expand Down Expand Up @@ -775,7 +775,7 @@ async def mock_remove_service_listener(listener: Any):


async def test_automatically_set_preferred_dataset_own_and_other_router(
hass: HomeAssistant, mock_async_zeroconf: None
hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None:
"""Test automatically setting the first dataset as the preferred dataset.
Expand Down Expand Up @@ -854,7 +854,7 @@ async def mock_remove_service_listener(listener: Any):


async def test_automatically_set_preferred_dataset_other_router(
hass: HomeAssistant, mock_async_zeroconf: None
hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None:
"""Test automatically setting the first dataset as the preferred dataset.
Expand Down Expand Up @@ -922,7 +922,7 @@ async def mock_remove_service_listener(listener: Any):


async def test_automatically_set_preferred_dataset_no_router(
hass: HomeAssistant, mock_async_zeroconf: None
hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None:
"""Test automatically setting the first dataset as the preferred dataset.
Expand Down
4 changes: 2 additions & 2 deletions tests/components/thread/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test the thread websocket API."""

import dataclasses
from unittest.mock import Mock, patch
from unittest.mock import MagicMock, Mock, patch

import pytest
from syrupy.assertion import SnapshotAssertion
Expand Down Expand Up @@ -182,7 +182,7 @@ def ndb() -> Mock:

async def test_diagnostics(
hass: HomeAssistant,
mock_async_zeroconf: None,
mock_async_zeroconf: MagicMock,
ndb: Mock,
hass_client: ClientSessionGenerator,
snapshot: SnapshotAssertion,
Expand Down
18 changes: 10 additions & 8 deletions tests/components/thread/test_discovery.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Test the thread websocket API."""

from unittest.mock import ANY, AsyncMock, Mock
from unittest.mock import ANY, AsyncMock, MagicMock, Mock

import pytest
from zeroconf.asyncio import AsyncServiceInfo
Expand All @@ -24,7 +24,9 @@
)


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."""
mock_async_zeroconf.async_add_service_listener = AsyncMock()
mock_async_zeroconf.async_remove_service_listener = AsyncMock()
Expand Down Expand Up @@ -151,7 +153,7 @@ def router_removed(key: str) -> None:
],
)
async def test_discover_routers_unconfigured(
hass: HomeAssistant, mock_async_zeroconf: None, data, unconfigured
hass: HomeAssistant, mock_async_zeroconf: MagicMock, data, unconfigured
) -> None:
"""Test discovering thread routers and setting the unconfigured flag."""
mock_async_zeroconf.async_add_service_listener = AsyncMock()
Expand Down Expand Up @@ -197,7 +199,7 @@ async def test_discover_routers_unconfigured(
"data", [ROUTER_DISCOVERY_HASS_BAD_DATA, ROUTER_DISCOVERY_HASS_MISSING_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:
"""Test discovering thread routers with bad or missing vendor mDNS data."""
mock_async_zeroconf.async_add_service_listener = AsyncMock()
Expand Down Expand Up @@ -247,7 +249,7 @@ async def test_discover_routers_bad_or_missing_optional_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:
"""Test discovering thread routers with missing mandatory mDNS data."""
mock_async_zeroconf.async_add_service_listener = AsyncMock()
Expand Down Expand Up @@ -281,7 +283,7 @@ async def test_discover_routers_bad_or_missing_mandatory_data(


async def test_discover_routers_get_service_info_fails(
hass: HomeAssistant, mock_async_zeroconf: None
hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None:
"""Test discovering thread routers with invalid mDNS data."""
mock_async_zeroconf.async_add_service_listener = AsyncMock()
Expand Down Expand Up @@ -311,7 +313,7 @@ async def test_discover_routers_get_service_info_fails(


async def test_discover_routers_update_unchanged(
hass: HomeAssistant, mock_async_zeroconf: None
hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None:
"""Test discovering thread routers with identical mDNS data in update."""
mock_async_zeroconf.async_add_service_listener = AsyncMock()
Expand Down Expand Up @@ -353,7 +355,7 @@ async def test_discover_routers_update_unchanged(


async def test_discover_routers_stop_twice(
hass: HomeAssistant, mock_async_zeroconf: None
hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None:
"""Test discovering thread routers stopping discovery twice."""
mock_async_zeroconf.async_add_service_listener = AsyncMock()
Expand Down
6 changes: 4 additions & 2 deletions tests/components/thread/test_websocket_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Test the thread websocket API."""

from unittest.mock import ANY, AsyncMock
from unittest.mock import ANY, AsyncMock, MagicMock

from zeroconf.asyncio import AsyncServiceInfo

Expand Down Expand Up @@ -315,7 +315,9 @@ async def test_set_preferred_dataset_wrong_id(


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:
"""Test discovering thread routers."""
mock_async_zeroconf.async_add_service_listener = AsyncMock()
Expand Down
16 changes: 9 additions & 7 deletions tests/components/zeroconf/test_init.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test Zeroconf component setup process."""

from typing import Any
from unittest.mock import call, patch
from unittest.mock import MagicMock, call, patch

import pytest
from zeroconf import (
Expand Down Expand Up @@ -148,7 +148,7 @@ def mock_zc_info(service_type, name):
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."""
mock_zc = {
"_http._tcp.local.": [
Expand Down Expand Up @@ -238,7 +238,7 @@ async def test_setup_with_overly_long_url_and_name(


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:
"""Test default interface config."""
with (
Expand Down Expand Up @@ -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"


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."""
assert await async_setup_component(hass, zeroconf.DOMAIN, {zeroconf.DOMAIN: {}})
assert await zeroconf.async_get_async_instance(hass) is mock_async_zeroconf
Expand Down Expand Up @@ -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."""
hass.config.location_name = ""
with patch("homeassistant.components.zeroconf.HaZeroconf"):
Expand All @@ -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(
hass: HomeAssistant, mock_async_zeroconf: None, caplog: pytest.LogCaptureFixture
hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None:
"""Test we still setup with disallowed characters in the location name."""
with (
Expand All @@ -1323,7 +1325,7 @@ async def test_setup_with_disallowed_characters_in_local_name(


async def test_start_with_frontend(
hass: HomeAssistant, mock_async_zeroconf: None
hass: HomeAssistant, mock_async_zeroconf: MagicMock
) -> None:
"""Test we start with the frontend."""
with patch("homeassistant.components.zeroconf.HaZeroconf"):
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ def disable_translations_once(translations_once):


@pytest.fixture
def mock_zeroconf() -> Generator[None, None, None]:
def mock_zeroconf() -> Generator[MagicMock, None, None]:
"""Mock zeroconf."""
from zeroconf import DNSCache # pylint: disable=import-outside-toplevel

Expand All @@ -1206,7 +1206,7 @@ def mock_zeroconf() -> Generator[None, None, None]:


@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."""
from zeroconf import DNSCache, Zeroconf # pylint: disable=import-outside-toplevel
from zeroconf.asyncio import ( # pylint: disable=import-outside-toplevel
Expand Down

0 comments on commit a5dc4cb

Please sign in to comment.