From 242ee0464281d75a9b5439c58309f4973a17d4e3 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 30 May 2024 08:47:08 +0200 Subject: [PATCH] Improve type hints in tests (i-p) (#118380) --- tests/components/input_boolean/test_init.py | 3 ++- tests/components/input_button/test_init.py | 3 ++- tests/components/input_datetime/test_init.py | 3 ++- tests/components/input_number/test_init.py | 3 ++- tests/components/input_select/test_init.py | 3 ++- tests/components/input_text/test_init.py | 3 ++- tests/components/knx/conftest.py | 3 ++- tests/components/logbook/test_init.py | 2 +- tests/components/lovelace/test_dashboard.py | 16 ++++++++++++---- tests/components/lovelace/test_resources.py | 16 ++++++++++++---- tests/components/matrix/conftest.py | 3 ++- tests/components/maxcube/conftest.py | 11 ++++++++++- tests/components/mobile_app/test_notify.py | 4 +++- tests/components/network/test_init.py | 6 +++++- tests/components/otbr/test_websocket_api.py | 6 ++++-- .../components/owntracks/test_device_tracker.py | 7 +++++-- tests/components/person/conftest.py | 8 +++++++- tests/components/plex/conftest.py | 5 +++-- 18 files changed, 78 insertions(+), 27 deletions(-) diff --git a/tests/components/input_boolean/test_init.py b/tests/components/input_boolean/test_init.py index 2a616691e62..b2e99836477 100644 --- a/tests/components/input_boolean/test_init.py +++ b/tests/components/input_boolean/test_init.py @@ -1,6 +1,7 @@ """The tests for the input_boolean component.""" import logging +from typing import Any from unittest.mock import patch import pytest @@ -30,7 +31,7 @@ _LOGGER = logging.getLogger(__name__) @pytest.fixture -def storage_setup(hass, hass_storage): +def storage_setup(hass: HomeAssistant, hass_storage: dict[str, Any]): """Storage setup.""" async def _storage(items=None, config=None): diff --git a/tests/components/input_button/test_init.py b/tests/components/input_button/test_init.py index 568d0076318..e59d0543751 100644 --- a/tests/components/input_button/test_init.py +++ b/tests/components/input_button/test_init.py @@ -1,6 +1,7 @@ """The tests for the input_test component.""" import logging +from typing import Any from unittest.mock import patch import pytest @@ -27,7 +28,7 @@ _LOGGER = logging.getLogger(__name__) @pytest.fixture -def storage_setup(hass, hass_storage): +def storage_setup(hass: HomeAssistant, hass_storage: dict[str, Any]): """Storage setup.""" async def _storage(items=None, config=None): diff --git a/tests/components/input_datetime/test_init.py b/tests/components/input_datetime/test_init.py index 5d8ea90b8a6..fdbb9a7803f 100644 --- a/tests/components/input_datetime/test_init.py +++ b/tests/components/input_datetime/test_init.py @@ -1,6 +1,7 @@ """Tests for the Input slider component.""" import datetime +from typing import Any from unittest.mock import patch import pytest @@ -45,7 +46,7 @@ INITIAL_DATETIME = f"{INITIAL_DATE} {INITIAL_TIME}" @pytest.fixture -def storage_setup(hass, hass_storage): +def storage_setup(hass: HomeAssistant, hass_storage: dict[str, Any]): """Storage setup.""" async def _storage(items=None, config=None): diff --git a/tests/components/input_number/test_init.py b/tests/components/input_number/test_init.py index 62b95fe16b3..73e41f347ce 100644 --- a/tests/components/input_number/test_init.py +++ b/tests/components/input_number/test_init.py @@ -1,5 +1,6 @@ """The tests for the Input number component.""" +from typing import Any from unittest.mock import patch import pytest @@ -29,7 +30,7 @@ from tests.typing import WebSocketGenerator @pytest.fixture -def storage_setup(hass, hass_storage): +def storage_setup(hass: HomeAssistant, hass_storage: dict[str, Any]): """Storage setup.""" async def _storage(items=None, config=None): diff --git a/tests/components/input_select/test_init.py b/tests/components/input_select/test_init.py index 431f8b7d078..153d8ed848d 100644 --- a/tests/components/input_select/test_init.py +++ b/tests/components/input_select/test_init.py @@ -1,5 +1,6 @@ """The tests for the Input select component.""" +from typing import Any from unittest.mock import patch import pytest @@ -36,7 +37,7 @@ from tests.typing import WebSocketGenerator @pytest.fixture -def storage_setup(hass, hass_storage): +def storage_setup(hass: HomeAssistant, hass_storage: dict[str, Any]): """Storage setup.""" async def _storage(items=None, config=None, minor_version=STORAGE_VERSION_MINOR): diff --git a/tests/components/input_text/test_init.py b/tests/components/input_text/test_init.py index d98ee4f7668..3cae98b6dfe 100644 --- a/tests/components/input_text/test_init.py +++ b/tests/components/input_text/test_init.py @@ -1,5 +1,6 @@ """The tests for the Input text component.""" +from typing import Any from unittest.mock import patch import pytest @@ -36,7 +37,7 @@ TEST_VAL_MAX = 22 @pytest.fixture -def storage_setup(hass, hass_storage): +def storage_setup(hass: HomeAssistant, hass_storage: dict[str, Any]): """Storage setup.""" async def _storage(items=None, config=None): diff --git a/tests/components/knx/conftest.py b/tests/components/knx/conftest.py index a580fc9eb2c..864a160ac1a 100644 --- a/tests/components/knx/conftest.py +++ b/tests/components/knx/conftest.py @@ -4,6 +4,7 @@ from __future__ import annotations import asyncio import json +from typing import Any from unittest.mock import DEFAULT, AsyncMock, Mock, patch import pytest @@ -273,7 +274,7 @@ async def knx(request, hass, mock_config_entry: MockConfigEntry): @pytest.fixture -def load_knxproj(hass_storage): +def load_knxproj(hass_storage: dict[str, Any]) -> None: """Mock KNX project data.""" hass_storage[KNX_PROJECT_STORAGE_KEY] = { "version": 1, diff --git a/tests/components/logbook/test_init.py b/tests/components/logbook/test_init.py index 0ba96a8ca6a..3534192a43e 100644 --- a/tests/components/logbook/test_init.py +++ b/tests/components/logbook/test_init.py @@ -61,7 +61,7 @@ EMPTY_CONFIG = logbook.CONFIG_SCHEMA({logbook.DOMAIN: {}}) @pytest.fixture -async def hass_(recorder_mock, hass): +async def hass_(recorder_mock: Recorder, hass: HomeAssistant) -> HomeAssistant: """Set up things to be run when tests are started.""" assert await async_setup_component(hass, logbook.DOMAIN, EMPTY_CONFIG) return hass diff --git a/tests/components/lovelace/test_dashboard.py b/tests/components/lovelace/test_dashboard.py index 3353b2eea51..affa5e1479f 100644 --- a/tests/components/lovelace/test_dashboard.py +++ b/tests/components/lovelace/test_dashboard.py @@ -30,7 +30,9 @@ def mock_onboarding_done() -> Generator[MagicMock, None, None]: async def test_lovelace_from_storage( - hass: HomeAssistant, hass_ws_client, hass_storage: dict[str, Any] + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + hass_storage: dict[str, Any], ) -> None: """Test we load lovelace config from storage.""" assert await async_setup_component(hass, "lovelace", {}) @@ -83,7 +85,9 @@ async def test_lovelace_from_storage( async def test_lovelace_from_storage_save_before_load( - hass: HomeAssistant, hass_ws_client, hass_storage: dict[str, Any] + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + hass_storage: dict[str, Any], ) -> None: """Test we can load lovelace config from storage.""" assert await async_setup_component(hass, "lovelace", {}) @@ -101,7 +105,9 @@ async def test_lovelace_from_storage_save_before_load( async def test_lovelace_from_storage_delete( - hass: HomeAssistant, hass_ws_client, hass_storage: dict[str, Any] + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + hass_storage: dict[str, Any], ) -> None: """Test we delete lovelace config from storage.""" assert await async_setup_component(hass, "lovelace", {}) @@ -352,7 +358,9 @@ async def test_wrong_key_dashboard_from_yaml(hass: HomeAssistant) -> None: async def test_storage_dashboards( - hass: HomeAssistant, hass_ws_client, hass_storage: dict[str, Any] + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + hass_storage: dict[str, Any], ) -> None: """Test we load lovelace config from storage.""" assert await async_setup_component(hass, "lovelace", {}) diff --git a/tests/components/lovelace/test_resources.py b/tests/components/lovelace/test_resources.py index 7591960b589..d2008ce5d41 100644 --- a/tests/components/lovelace/test_resources.py +++ b/tests/components/lovelace/test_resources.py @@ -56,7 +56,9 @@ async def test_yaml_resources_backwards( async def test_storage_resources( - hass: HomeAssistant, hass_ws_client, hass_storage: dict[str, Any] + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + hass_storage: dict[str, Any], ) -> None: """Test defining resources in storage config.""" resource_config = [{**item, "id": uuid.uuid4().hex} for item in RESOURCE_EXAMPLES] @@ -77,7 +79,9 @@ async def test_storage_resources( async def test_storage_resources_import( - hass: HomeAssistant, hass_ws_client, hass_storage: dict[str, Any] + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + hass_storage: dict[str, Any], ) -> None: """Test importing resources from storage config.""" assert await async_setup_component(hass, "lovelace", {}) @@ -165,7 +169,9 @@ async def test_storage_resources_import( async def test_storage_resources_import_invalid( - hass: HomeAssistant, hass_ws_client, hass_storage: dict[str, Any] + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + hass_storage: dict[str, Any], ) -> None: """Test importing resources from storage config.""" assert await async_setup_component(hass, "lovelace", {}) @@ -189,7 +195,9 @@ async def test_storage_resources_import_invalid( async def test_storage_resources_safe_mode( - hass: HomeAssistant, hass_ws_client, hass_storage: dict[str, Any] + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + hass_storage: dict[str, Any], ) -> None: """Test defining resources in storage config.""" diff --git a/tests/components/matrix/conftest.py b/tests/components/matrix/conftest.py index 18227914df4..f65deea8dad 100644 --- a/tests/components/matrix/conftest.py +++ b/tests/components/matrix/conftest.py @@ -2,6 +2,7 @@ from __future__ import annotations +from pathlib import Path import re import tempfile from unittest.mock import patch @@ -304,7 +305,7 @@ def command_events(hass: HomeAssistant): @pytest.fixture -def image_path(tmp_path): +def image_path(tmp_path: Path): """Provide the Path to a mock image.""" image = Image.new("RGBA", size=(50, 50), color=(256, 0, 0)) image_file = tempfile.NamedTemporaryFile(dir=tmp_path) diff --git a/tests/components/maxcube/conftest.py b/tests/components/maxcube/conftest.py index 82a852a5201..88e40edfdd0 100644 --- a/tests/components/maxcube/conftest.py +++ b/tests/components/maxcube/conftest.py @@ -10,6 +10,8 @@ from maxcube.windowshutter import MaxWindowShutter import pytest from homeassistant.components.maxcube import DOMAIN +from homeassistant.core import HomeAssistant +from homeassistant.helpers.typing import ConfigType from homeassistant.setup import async_setup_component from homeassistant.util.dt import now @@ -99,7 +101,14 @@ def hass_config(): @pytest.fixture -async def cube(hass, hass_config, room, thermostat, wallthermostat, windowshutter): +async def cube( + hass: HomeAssistant, + hass_config: ConfigType, + room, + thermostat, + wallthermostat, + windowshutter, +): """Build and setup a cube mock with a single room and some devices.""" with patch("homeassistant.components.maxcube.MaxCube") as mock: cube = mock.return_value diff --git a/tests/components/mobile_app/test_notify.py b/tests/components/mobile_app/test_notify.py index 53a51938fed..57f7933b00f 100644 --- a/tests/components/mobile_app/test_notify.py +++ b/tests/components/mobile_app/test_notify.py @@ -110,7 +110,9 @@ async def setup_push_receiver( @pytest.fixture -async def setup_websocket_channel_only_push(hass, hass_admin_user): +async def setup_websocket_channel_only_push( + hass: HomeAssistant, hass_admin_user: MockUser +) -> None: """Set up local push.""" entry = MockConfigEntry( data={ diff --git a/tests/components/network/test_init.py b/tests/components/network/test_init.py index b02692e5086..e57b3242e8c 100644 --- a/tests/components/network/test_init.py +++ b/tests/components/network/test_init.py @@ -20,6 +20,8 @@ from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.setup import async_setup_component +from tests.typing import WebSocketGenerator + _NO_LOOPBACK_IPADDR = "192.168.1.5" _LOOPBACK_IPADDR = "127.0.0.1" @@ -409,7 +411,9 @@ async def test_interfaces_configured_from_storage( async def test_interfaces_configured_from_storage_websocket_update( - hass: HomeAssistant, hass_ws_client, hass_storage: dict[str, Any] + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + hass_storage: dict[str, Any], ) -> None: """Test settings from storage can be updated via websocket api.""" hass_storage[STORAGE_KEY] = { diff --git a/tests/components/otbr/test_websocket_api.py b/tests/components/otbr/test_websocket_api.py index c8ac839f629..df55d38d3b7 100644 --- a/tests/components/otbr/test_websocket_api.py +++ b/tests/components/otbr/test_websocket_api.py @@ -18,11 +18,13 @@ from . import ( ) from tests.test_util.aiohttp import AiohttpClientMocker -from tests.typing import WebSocketGenerator +from tests.typing import MockHAClientWebSocket, WebSocketGenerator @pytest.fixture -async def websocket_client(hass, hass_ws_client): +async def websocket_client( + hass: HomeAssistant, hass_ws_client: WebSocketGenerator +) -> MockHAClientWebSocket: """Create a websocket client.""" return await hass_ws_client(hass) diff --git a/tests/components/owntracks/test_device_tracker.py b/tests/components/owntracks/test_device_tracker.py index a36d03e973c..80e76a5e7b4 100644 --- a/tests/components/owntracks/test_device_tracker.py +++ b/tests/components/owntracks/test_device_tracker.py @@ -6,12 +6,13 @@ from unittest.mock import patch import pytest from homeassistant.components import owntracks +from homeassistant.components.device_tracker.legacy import Device from homeassistant.const import STATE_NOT_HOME from homeassistant.core import HomeAssistant from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry, async_fire_mqtt_message -from tests.typing import ClientSessionGenerator +from tests.typing import ClientSessionGenerator, MqttMockHAClient USER = "greg" DEVICE = "phone" @@ -284,7 +285,9 @@ BAD_JSON_SUFFIX = "** and it ends here ^^" @pytest.fixture -def setup_comp(hass, mock_device_tracker_conf, mqtt_mock): +def setup_comp( + hass, mock_device_tracker_conf: list[Device], mqtt_mock: MqttMockHAClient +): """Initialize components.""" hass.loop.run_until_complete(async_setup_component(hass, "device_tracker", {})) diff --git a/tests/components/person/conftest.py b/tests/components/person/conftest.py index 7f06b854c5c..ecec42b003d 100644 --- a/tests/components/person/conftest.py +++ b/tests/components/person/conftest.py @@ -1,14 +1,18 @@ """The tests for the person component.""" import logging +from typing import Any import pytest from homeassistant.components import person from homeassistant.components.person import DOMAIN +from homeassistant.core import HomeAssistant from homeassistant.helpers import collection from homeassistant.setup import async_setup_component +from tests.common import MockUser + DEVICE_TRACKER = "device_tracker.test_tracker" DEVICE_TRACKER_2 = "device_tracker.test_tracker_2" @@ -27,7 +31,9 @@ def storage_collection(hass): @pytest.fixture -def storage_setup(hass, hass_storage, hass_admin_user): +def storage_setup( + hass: HomeAssistant, hass_storage: dict[str, Any], hass_admin_user: MockUser +) -> None: """Storage setup.""" hass_storage[DOMAIN] = { "key": DOMAIN, diff --git a/tests/components/plex/conftest.py b/tests/components/plex/conftest.py index d00b8eb944b..480573216bc 100644 --- a/tests/components/plex/conftest.py +++ b/tests/components/plex/conftest.py @@ -4,6 +4,7 @@ from collections.abc import Generator from unittest.mock import AsyncMock, patch import pytest +import requests_mock from homeassistant.components.plex.const import DOMAIN, PLEX_SERVER_CONFIG, SERVERS from homeassistant.const import CONF_URL @@ -436,7 +437,7 @@ def mock_websocket(): @pytest.fixture def mock_plex_calls( entry, - requests_mock, + requests_mock: requests_mock.Mocker, children_20, children_30, children_200, @@ -550,7 +551,7 @@ def setup_plex_server( livetv_sessions, mock_websocket, mock_plex_calls, - requests_mock, + requests_mock: requests_mock.Mocker, empty_payload, session_default, session_live_tv,