From 092cdcfe91611a368eb305dd2b64fc5101cdbeaa Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 30 May 2024 08:46:18 +0200 Subject: [PATCH] Improve type hints in tests (a-h) (#118379) --- tests/components/camera/test_init.py | 6 ++--- .../test_auth_provider_homeassistant.py | 10 ++++--- tests/components/config/test_automation.py | 6 ++--- tests/components/config/test_core.py | 10 +++++-- tests/components/counter/test_init.py | 3 ++- tests/components/dynalite/test_panel.py | 14 +++++++--- tests/components/energy/test_sensor.py | 3 ++- tests/components/esphome/test_dashboard.py | 27 ++++++++++++------- tests/components/frontend/test_init.py | 8 +++--- .../google_assistant/test_google_assistant.py | 2 +- tests/components/hassio/test_http.py | 3 ++- .../homeassistant_alerts/test_init.py | 6 ++--- .../components/huawei_lte/test_config_flow.py | 2 +- 13 files changed, 66 insertions(+), 34 deletions(-) diff --git a/tests/components/camera/test_init.py b/tests/components/camera/test_init.py index dffc7e5aa53..0520908f210 100644 --- a/tests/components/camera/test_init.py +++ b/tests/components/camera/test_init.py @@ -849,12 +849,12 @@ async def test_rtsp_to_web_rtc_offer( async def test_unsupported_rtsp_to_web_rtc_stream_type( - hass, - hass_ws_client, + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, mock_camera, mock_hls_stream_source, # Not an RTSP stream source mock_rtsp_to_web_rtc, -): +) -> None: """Test rtsp-to-webrtc is not registered for non-RTSP streams.""" client = await hass_ws_client(hass) await client.send_json( diff --git a/tests/components/config/test_auth_provider_homeassistant.py b/tests/components/config/test_auth_provider_homeassistant.py index d2631cd7a7c..5c5661376e2 100644 --- a/tests/components/config/test_auth_provider_homeassistant.py +++ b/tests/components/config/test_auth_provider_homeassistant.py @@ -13,19 +13,23 @@ from tests.typing import WebSocketGenerator @pytest.fixture(autouse=True) -async def setup_config(hass, local_auth): +async def setup_config( + hass: HomeAssistant, local_auth: prov_ha.HassAuthProvider +) -> None: """Fixture that sets up the auth provider .""" auth_ha.async_setup(hass) @pytest.fixture -async def auth_provider(local_auth): +async def auth_provider( + local_auth: prov_ha.HassAuthProvider, +) -> prov_ha.HassAuthProvider: """Hass auth provider.""" return local_auth @pytest.fixture -async def owner_access_token(hass, hass_owner_user): +async def owner_access_token(hass: HomeAssistant, hass_owner_user: MockUser) -> str: """Access token for owner user.""" refresh_token = await hass.auth.async_create_refresh_token( hass_owner_user, CLIENT_ID diff --git a/tests/components/config/test_automation.py b/tests/components/config/test_automation.py index b17face10d9..9d9ee5d5649 100644 --- a/tests/components/config/test_automation.py +++ b/tests/components/config/test_automation.py @@ -25,10 +25,10 @@ def stub_blueprint_populate_autouse(stub_blueprint_populate: None) -> None: @pytest.fixture async def setup_automation( - hass, + hass: HomeAssistant, automation_config, - stub_blueprint_populate, -): + stub_blueprint_populate: None, +) -> None: """Set up automation integration.""" assert await async_setup_component( hass, "automation", {"automation": automation_config} diff --git a/tests/components/config/test_core.py b/tests/components/config/test_core.py index da8a60ca6fd..29cbdd9b83e 100644 --- a/tests/components/config/test_core.py +++ b/tests/components/config/test_core.py @@ -19,11 +19,17 @@ from homeassistant.util import dt as dt_util, location from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM from tests.common import MockUser -from tests.typing import ClientSessionGenerator, WebSocketGenerator +from tests.typing import ( + ClientSessionGenerator, + MockHAClientWebSocket, + WebSocketGenerator, +) @pytest.fixture -async def client(hass, hass_ws_client): +async def client( + hass: HomeAssistant, hass_ws_client: WebSocketGenerator +) -> MockHAClientWebSocket: """Fixture that can interact with the config manager API.""" with patch.object(config, "SECTIONS", [core]): assert await async_setup_component(hass, "config", {}) diff --git a/tests/components/counter/test_init.py b/tests/components/counter/test_init.py index 342c22baf24..ef2caf2eab1 100644 --- a/tests/components/counter/test_init.py +++ b/tests/components/counter/test_init.py @@ -1,6 +1,7 @@ """The tests for the counter component.""" import logging +from typing import Any import pytest @@ -37,7 +38,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/dynalite/test_panel.py b/tests/components/dynalite/test_panel.py index a1cd9749eb5..97752142f0c 100644 --- a/tests/components/dynalite/test_panel.py +++ b/tests/components/dynalite/test_panel.py @@ -5,11 +5,15 @@ from unittest.mock import patch from homeassistant.components import dynalite from homeassistant.components.cover import DEVICE_CLASSES from homeassistant.const import CONF_PORT +from homeassistant.core import HomeAssistant from tests.common import MockConfigEntry +from tests.typing import WebSocketGenerator -async def test_get_config(hass, hass_ws_client): +async def test_get_config( + hass: HomeAssistant, hass_ws_client: WebSocketGenerator +) -> None: """Get the config via websocket.""" host = "1.2.3.4" port = 765 @@ -49,7 +53,9 @@ async def test_get_config(hass, hass_ws_client): } -async def test_save_config(hass, hass_ws_client): +async def test_save_config( + hass: HomeAssistant, hass_ws_client: WebSocketGenerator +) -> None: """Save the config via websocket.""" host1 = "1.2.3.4" port1 = 765 @@ -103,7 +109,9 @@ async def test_save_config(hass, hass_ws_client): assert modified_entry.data[CONF_PORT] == port3 -async def test_save_config_invalid_entry(hass, hass_ws_client): +async def test_save_config_invalid_entry( + hass: HomeAssistant, hass_ws_client: WebSocketGenerator +) -> None: """Try to update nonexistent entry.""" host1 = "1.2.3.4" port1 = 765 diff --git a/tests/components/energy/test_sensor.py b/tests/components/energy/test_sensor.py index 192cf6abea4..4128a80c587 100644 --- a/tests/components/energy/test_sensor.py +++ b/tests/components/energy/test_sensor.py @@ -7,6 +7,7 @@ from typing import Any import pytest from homeassistant.components.energy import data +from homeassistant.components.recorder.core import Recorder from homeassistant.components.recorder.util import session_scope from homeassistant.components.sensor import ( ATTR_LAST_RESET, @@ -35,7 +36,7 @@ TEST_TIME_ADVANCE_INTERVAL = timedelta(milliseconds=10) @pytest.fixture -async def setup_integration(recorder_mock): +async def setup_integration(recorder_mock: Recorder): """Set up the integration.""" async def setup_integration(hass): diff --git a/tests/components/esphome/test_dashboard.py b/tests/components/esphome/test_dashboard.py index dbf092bb9fc..1b0303a8a48 100644 --- a/tests/components/esphome/test_dashboard.py +++ b/tests/components/esphome/test_dashboard.py @@ -1,5 +1,6 @@ """Test ESPHome dashboard features.""" +from typing import Any from unittest.mock import patch from aioesphomeapi import DeviceInfo, InvalidAuthAPIError @@ -15,7 +16,7 @@ from tests.common import MockConfigEntry async def test_dashboard_storage( - hass: HomeAssistant, init_integration, mock_dashboard, hass_storage + hass: HomeAssistant, init_integration, mock_dashboard, hass_storage: dict[str, Any] ) -> None: """Test dashboard storage.""" assert hass_storage[dashboard.STORAGE_KEY]["data"] == { @@ -28,8 +29,10 @@ async def test_dashboard_storage( async def test_restore_dashboard_storage( - hass: HomeAssistant, mock_config_entry: MockConfigEntry, hass_storage -) -> MockConfigEntry: + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + hass_storage: dict[str, Any], +) -> None: """Restore dashboard url and slug from storage.""" hass_storage[dashboard.STORAGE_KEY] = { "version": dashboard.STORAGE_VERSION, @@ -46,8 +49,10 @@ async def test_restore_dashboard_storage( async def test_restore_dashboard_storage_end_to_end( - hass: HomeAssistant, mock_config_entry: MockConfigEntry, hass_storage -) -> MockConfigEntry: + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + hass_storage: dict[str, Any], +) -> None: """Restore dashboard url and slug from storage.""" hass_storage[dashboard.STORAGE_KEY] = { "version": dashboard.STORAGE_VERSION, @@ -65,8 +70,10 @@ async def test_restore_dashboard_storage_end_to_end( async def test_setup_dashboard_fails( - hass: HomeAssistant, mock_config_entry: MockConfigEntry, hass_storage -) -> MockConfigEntry: + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + hass_storage: dict[str, Any], +) -> None: """Test that nothing is stored on failed dashboard setup when there was no dashboard before.""" with patch.object( coordinator.ESPHomeDashboardAPI, "get_devices", side_effect=TimeoutError @@ -83,8 +90,10 @@ async def test_setup_dashboard_fails( async def test_setup_dashboard_fails_when_already_setup( - hass: HomeAssistant, mock_config_entry: MockConfigEntry, hass_storage -) -> MockConfigEntry: + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + hass_storage: dict[str, Any], +) -> None: """Test failed dashboard setup still reloads entries if one existed before.""" with patch.object( coordinator.ESPHomeDashboardAPI, "get_devices" diff --git a/tests/components/frontend/test_init.py b/tests/components/frontend/test_init.py index ddfe2b80b1d..57ee04da47f 100644 --- a/tests/components/frontend/test_init.py +++ b/tests/components/frontend/test_init.py @@ -109,14 +109,16 @@ async def mock_http_client( @pytest.fixture async def themes_ws_client( - hass: HomeAssistant, hass_ws_client: ClientSessionGenerator, frontend_themes -) -> TestClient: + hass: HomeAssistant, hass_ws_client: WebSocketGenerator, frontend_themes +) -> MockHAClientWebSocket: """Start the Home Assistant HTTP component.""" return await hass_ws_client(hass) @pytest.fixture -async def ws_client(hass, hass_ws_client, frontend): +async def ws_client( + hass: HomeAssistant, hass_ws_client: WebSocketGenerator, frontend +) -> MockHAClientWebSocket: """Start the Home Assistant HTTP component.""" return await hass_ws_client(hass) diff --git a/tests/components/google_assistant/test_google_assistant.py b/tests/components/google_assistant/test_google_assistant.py index 015818d132d..ea30f89e0ef 100644 --- a/tests/components/google_assistant/test_google_assistant.py +++ b/tests/components/google_assistant/test_google_assistant.py @@ -36,7 +36,7 @@ ACCESS_TOKEN = "superdoublesecret" @pytest.fixture -def auth_header(hass_access_token): +def auth_header(hass_access_token: str) -> dict[str, str]: """Generate an HTTP header with bearer token authorization.""" return {AUTHORIZATION: f"Bearer {hass_access_token}"} diff --git a/tests/components/hassio/test_http.py b/tests/components/hassio/test_http.py index 55d4d8b0365..a5ffb4f0d83 100644 --- a/tests/components/hassio/test_http.py +++ b/tests/components/hassio/test_http.py @@ -6,6 +6,7 @@ from unittest.mock import patch from aiohttp import StreamReader import pytest +from tests.common import MockUser from tests.test_util.aiohttp import AiohttpClientMocker @@ -19,7 +20,7 @@ def mock_not_onboarded(): @pytest.fixture -def hassio_user_client(hassio_client, hass_admin_user): +def hassio_user_client(hassio_client, hass_admin_user: MockUser): """Return a Hass.io HTTP client tied to a non-admin user.""" hass_admin_user.groups = [] return hassio_client diff --git a/tests/components/homeassistant_alerts/test_init.py b/tests/components/homeassistant_alerts/test_init.py index c1974bdf886..444db019c7c 100644 --- a/tests/components/homeassistant_alerts/test_init.py +++ b/tests/components/homeassistant_alerts/test_init.py @@ -580,13 +580,13 @@ async def test_no_alerts( ) async def test_alerts_change( hass: HomeAssistant, - hass_ws_client, + hass_ws_client: WebSocketGenerator, aioclient_mock: AiohttpClientMocker, ha_version: str, fixture_1: str, - expected_alerts_1: list[tuple(str, str)], + expected_alerts_1: list[tuple[str, str]], fixture_2: str, - expected_alerts_2: list[tuple(str, str)], + expected_alerts_2: list[tuple[str, str]], ) -> None: """Test creating issues based on alerts.""" fixture_1_content = load_fixture(fixture_1, "homeassistant_alerts") diff --git a/tests/components/huawei_lte/test_config_flow.py b/tests/components/huawei_lte/test_config_flow.py index 200796c87e7..329f06795d2 100644 --- a/tests/components/huawei_lte/test_config_flow.py +++ b/tests/components/huawei_lte/test_config_flow.py @@ -134,7 +134,7 @@ async def test_connection_errors( @pytest.fixture -def login_requests_mock(requests_mock): +def login_requests_mock(requests_mock: requests_mock.Mocker) -> requests_mock.Mocker: """Set up a requests_mock with base mocks for login tests.""" https_url = urlunparse( urlparse(FIXTURE_USER_INPUT[CONF_URL])._replace(scheme="https")