From 93f791e5d0e76d71bf3830e38d33ab2b8267d5f7 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 20 Aug 2024 12:48:06 +0200 Subject: [PATCH] Add missing hass type hint in component tests (n) (#124225) --- tests/components/nest/test_camera.py | 6 +- tests/components/nest/test_device_trigger.py | 4 +- tests/components/notify/common.py | 7 +- tests/components/notify/test_legacy.py | 72 +++++++++++++++----- tests/components/nx584/test_binary_sensor.py | 9 ++- 5 files changed, 75 insertions(+), 23 deletions(-) diff --git a/tests/components/nest/test_camera.py b/tests/components/nest/test_camera.py index fd2b5ef0388..6aa25134563 100644 --- a/tests/components/nest/test_camera.py +++ b/tests/components/nest/test_camera.py @@ -165,7 +165,9 @@ async def mock_create_stream(hass: HomeAssistant) -> Generator[AsyncMock]: yield mock_stream -async def async_get_image(hass, width=None, height=None): +async def async_get_image( + hass: HomeAssistant, width: int | None = None, height: int | None = None +) -> bytes: """Get the camera image.""" image = await camera.async_get_image( hass, "camera.my_camera", width=width, height=height @@ -174,7 +176,7 @@ async def async_get_image(hass, width=None, height=None): return image.content -async def fire_alarm(hass, point_in_time): +async def fire_alarm(hass: HomeAssistant, point_in_time: datetime.datetime) -> None: """Fire an alarm and wait for callbacks to run.""" with freeze_time(point_in_time): async_fire_time_changed(hass, point_in_time) diff --git a/tests/components/nest/test_device_trigger.py b/tests/components/nest/test_device_trigger.py index f818713d382..cf0e1c5ecce 100644 --- a/tests/components/nest/test_device_trigger.py +++ b/tests/components/nest/test_device_trigger.py @@ -59,7 +59,9 @@ def make_camera( } -async def setup_automation(hass, device_id, trigger_type): +async def setup_automation( + hass: HomeAssistant, device_id: str, trigger_type: str +) -> bool: """Set up an automation trigger for testing triggering.""" return await async_setup_component( hass, diff --git a/tests/components/notify/common.py b/tests/components/notify/common.py index 418de96d1aa..1b5c0d6d6ba 100644 --- a/tests/components/notify/common.py +++ b/tests/components/notify/common.py @@ -4,6 +4,8 @@ All containing methods are legacy helpers that should not be used by new components. Instead call the service directly. """ +from typing import Any + from homeassistant.components.notify import ( ATTR_DATA, ATTR_MESSAGE, @@ -11,11 +13,14 @@ from homeassistant.components.notify import ( DOMAIN, SERVICE_NOTIFY, ) +from homeassistant.core import HomeAssistant from homeassistant.loader import bind_hass @bind_hass -def send_message(hass, message, title=None, data=None): +def send_message( + hass: HomeAssistant, message: str, title: str | None = None, data: Any = None +) -> None: """Send a notification message.""" info = {ATTR_MESSAGE: message} diff --git a/tests/components/notify/test_legacy.py b/tests/components/notify/test_legacy.py index 8be80650053..79a1b75dcae 100644 --- a/tests/components/notify/test_legacy.py +++ b/tests/components/notify/test_legacy.py @@ -1,7 +1,7 @@ """The tests for legacy notify services.""" import asyncio -from collections.abc import Mapping +from collections.abc import Callable, Coroutine, Mapping from pathlib import Path from typing import Any from unittest.mock import MagicMock, Mock, patch @@ -63,8 +63,16 @@ def mock_notify_platform( hass: HomeAssistant, tmp_path: Path, integration: str = "notify", - async_get_service: Any = None, - get_service: Any = None, + async_get_service: Callable[ + [HomeAssistant, ConfigType, DiscoveryInfoType | None], + Coroutine[Any, Any, notify.BaseNotificationService], + ] + | None = None, + get_service: Callable[ + [HomeAssistant, ConfigType, DiscoveryInfoType | None], + notify.BaseNotificationService, + ] + | None = None, ): """Specialize the mock platform for legacy notify service.""" loaded_platform = MockNotifyPlatform(async_get_service, get_service) @@ -263,7 +271,11 @@ async def test_platform_setup_with_error( ) -> None: """Test service setup with an invalid setup.""" - async def async_get_service(hass, config, discovery_info=None): + async def async_get_service( + hass: HomeAssistant, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = None, + ) -> notify.BaseNotificationService | None: """Return None for an invalid notify service.""" raise Exception("Setup error") # noqa: TRY002 @@ -283,11 +295,15 @@ async def test_platform_setup_with_error( async def test_reload_with_notify_builtin_platform_reload( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, tmp_path: Path + hass: HomeAssistant, tmp_path: Path ) -> None: """Test reload using the legacy notify platform reload method.""" - async def async_get_service(hass, config, discovery_info=None): + async def async_get_service( + hass: HomeAssistant, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = None, + ) -> NotificationService: """Get notify service for mocked platform.""" targetlist = {"a": 1, "b": 2} return NotificationService(hass, targetlist, "testnotify") @@ -314,19 +330,25 @@ async def test_reload_with_notify_builtin_platform_reload( assert hass.services.has_service(notify.DOMAIN, "testnotify_b") -async def test_setup_platform_and_reload( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, tmp_path: Path -) -> None: +async def test_setup_platform_and_reload(hass: HomeAssistant, tmp_path: Path) -> None: """Test service setup and reload.""" get_service_called = Mock() - async def async_get_service(hass, config, discovery_info=None): + async def async_get_service( + hass: HomeAssistant, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = None, + ) -> NotificationService: """Get notify service for mocked platform.""" get_service_called(config, discovery_info) targetlist = {"a": 1, "b": 2} return NotificationService(hass, targetlist, "testnotify") - async def async_get_service2(hass, config, discovery_info=None): + async def async_get_service2( + hass: HomeAssistant, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = None, + ) -> NotificationService: """Get legacy notify service for mocked platform.""" get_service_called(config, discovery_info) targetlist = {"c": 3, "d": 4} @@ -405,18 +427,26 @@ async def test_setup_platform_and_reload( async def test_setup_platform_before_notify_setup( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, tmp_path: Path + hass: HomeAssistant, tmp_path: Path ) -> None: """Test trying to setup a platform before legacy notify service is setup.""" get_service_called = Mock() - async def async_get_service(hass, config, discovery_info=None): + async def async_get_service( + hass: HomeAssistant, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = None, + ) -> NotificationService: """Get notify service for mocked platform.""" get_service_called(config, discovery_info) targetlist = {"a": 1, "b": 2} return NotificationService(hass, targetlist, "testnotify") - async def async_get_service2(hass, config, discovery_info=None): + async def async_get_service2( + hass: HomeAssistant, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = None, + ) -> NotificationService: """Get notify service for mocked platform.""" get_service_called(config, discovery_info) targetlist = {"c": 3, "d": 4} @@ -455,18 +485,26 @@ async def test_setup_platform_before_notify_setup( async def test_setup_platform_after_notify_setup( - hass: HomeAssistant, caplog: pytest.LogCaptureFixture, tmp_path: Path + hass: HomeAssistant, tmp_path: Path ) -> None: """Test trying to setup a platform after legacy notify service is set up.""" get_service_called = Mock() - async def async_get_service(hass, config, discovery_info=None): + async def async_get_service( + hass: HomeAssistant, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = None, + ) -> NotificationService: """Get notify service for mocked platform.""" get_service_called(config, discovery_info) targetlist = {"a": 1, "b": 2} return NotificationService(hass, targetlist, "testnotify") - async def async_get_service2(hass, config, discovery_info=None): + async def async_get_service2( + hass: HomeAssistant, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = None, + ) -> NotificationService: """Get notify service for mocked platform.""" get_service_called(config, discovery_info) targetlist = {"c": 3, "d": 4} diff --git a/tests/components/nx584/test_binary_sensor.py b/tests/components/nx584/test_binary_sensor.py index 9261521f850..d59cbdcf69d 100644 --- a/tests/components/nx584/test_binary_sensor.py +++ b/tests/components/nx584/test_binary_sensor.py @@ -1,5 +1,6 @@ """The tests for the nx584 sensor platform.""" +from typing import Any from unittest import mock from nx584 import client as nx584_client @@ -99,7 +100,9 @@ def test_nx584_sensor_setup_full_config( assert mock_watcher.called -async def _test_assert_graceful_fail(hass, config): +async def _test_assert_graceful_fail( + hass: HomeAssistant, config: dict[str, Any] +) -> None: """Test the failing.""" assert not await async_setup_component(hass, "nx584", config) @@ -114,7 +117,9 @@ async def _test_assert_graceful_fail(hass, config): ({"zone_types": {"notazone": "motion"}}), ], ) -async def test_nx584_sensor_setup_bad_config(hass: HomeAssistant, config) -> None: +async def test_nx584_sensor_setup_bad_config( + hass: HomeAssistant, config: dict[str, Any] +) -> None: """Test the setup with bad configuration.""" await _test_assert_graceful_fail(hass, config)