Add missing hass type hint in component tests (n) (#124225)

This commit is contained in:
epenet 2024-08-20 12:48:06 +02:00 committed by GitHub
parent db92f29c00
commit 93f791e5d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 75 additions and 23 deletions

View File

@ -165,7 +165,9 @@ async def mock_create_stream(hass: HomeAssistant) -> Generator[AsyncMock]:
yield mock_stream 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.""" """Get the camera image."""
image = await camera.async_get_image( image = await camera.async_get_image(
hass, "camera.my_camera", width=width, height=height 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 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.""" """Fire an alarm and wait for callbacks to run."""
with freeze_time(point_in_time): with freeze_time(point_in_time):
async_fire_time_changed(hass, point_in_time) async_fire_time_changed(hass, point_in_time)

View File

@ -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.""" """Set up an automation trigger for testing triggering."""
return await async_setup_component( return await async_setup_component(
hass, hass,

View File

@ -4,6 +4,8 @@ All containing methods are legacy helpers that should not be used by new
components. Instead call the service directly. components. Instead call the service directly.
""" """
from typing import Any
from homeassistant.components.notify import ( from homeassistant.components.notify import (
ATTR_DATA, ATTR_DATA,
ATTR_MESSAGE, ATTR_MESSAGE,
@ -11,11 +13,14 @@ from homeassistant.components.notify import (
DOMAIN, DOMAIN,
SERVICE_NOTIFY, SERVICE_NOTIFY,
) )
from homeassistant.core import HomeAssistant
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
@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.""" """Send a notification message."""
info = {ATTR_MESSAGE: message} info = {ATTR_MESSAGE: message}

View File

@ -1,7 +1,7 @@
"""The tests for legacy notify services.""" """The tests for legacy notify services."""
import asyncio import asyncio
from collections.abc import Mapping from collections.abc import Callable, Coroutine, Mapping
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
from unittest.mock import MagicMock, Mock, patch from unittest.mock import MagicMock, Mock, patch
@ -63,8 +63,16 @@ def mock_notify_platform(
hass: HomeAssistant, hass: HomeAssistant,
tmp_path: Path, tmp_path: Path,
integration: str = "notify", integration: str = "notify",
async_get_service: Any = None, async_get_service: Callable[
get_service: Any = None, [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.""" """Specialize the mock platform for legacy notify service."""
loaded_platform = MockNotifyPlatform(async_get_service, get_service) loaded_platform = MockNotifyPlatform(async_get_service, get_service)
@ -263,7 +271,11 @@ async def test_platform_setup_with_error(
) -> None: ) -> None:
"""Test service setup with an invalid setup.""" """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.""" """Return None for an invalid notify service."""
raise Exception("Setup error") # noqa: TRY002 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( async def test_reload_with_notify_builtin_platform_reload(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, tmp_path: Path hass: HomeAssistant, tmp_path: Path
) -> None: ) -> None:
"""Test reload using the legacy notify platform reload method.""" """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.""" """Get notify service for mocked platform."""
targetlist = {"a": 1, "b": 2} targetlist = {"a": 1, "b": 2}
return NotificationService(hass, targetlist, "testnotify") 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") assert hass.services.has_service(notify.DOMAIN, "testnotify_b")
async def test_setup_platform_and_reload( async def test_setup_platform_and_reload(hass: HomeAssistant, tmp_path: Path) -> None:
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, tmp_path: Path
) -> None:
"""Test service setup and reload.""" """Test service setup and reload."""
get_service_called = Mock() 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 notify service for mocked platform."""
get_service_called(config, discovery_info) get_service_called(config, discovery_info)
targetlist = {"a": 1, "b": 2} targetlist = {"a": 1, "b": 2}
return NotificationService(hass, targetlist, "testnotify") 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 legacy notify service for mocked platform."""
get_service_called(config, discovery_info) get_service_called(config, discovery_info)
targetlist = {"c": 3, "d": 4} targetlist = {"c": 3, "d": 4}
@ -405,18 +427,26 @@ async def test_setup_platform_and_reload(
async def test_setup_platform_before_notify_setup( async def test_setup_platform_before_notify_setup(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, tmp_path: Path hass: HomeAssistant, tmp_path: Path
) -> None: ) -> None:
"""Test trying to setup a platform before legacy notify service is setup.""" """Test trying to setup a platform before legacy notify service is setup."""
get_service_called = Mock() 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 notify service for mocked platform."""
get_service_called(config, discovery_info) get_service_called(config, discovery_info)
targetlist = {"a": 1, "b": 2} targetlist = {"a": 1, "b": 2}
return NotificationService(hass, targetlist, "testnotify") 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 notify service for mocked platform."""
get_service_called(config, discovery_info) get_service_called(config, discovery_info)
targetlist = {"c": 3, "d": 4} 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( async def test_setup_platform_after_notify_setup(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, tmp_path: Path hass: HomeAssistant, tmp_path: Path
) -> None: ) -> None:
"""Test trying to setup a platform after legacy notify service is set up.""" """Test trying to setup a platform after legacy notify service is set up."""
get_service_called = Mock() 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 notify service for mocked platform."""
get_service_called(config, discovery_info) get_service_called(config, discovery_info)
targetlist = {"a": 1, "b": 2} targetlist = {"a": 1, "b": 2}
return NotificationService(hass, targetlist, "testnotify") 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 notify service for mocked platform."""
get_service_called(config, discovery_info) get_service_called(config, discovery_info)
targetlist = {"c": 3, "d": 4} targetlist = {"c": 3, "d": 4}

View File

@ -1,5 +1,6 @@
"""The tests for the nx584 sensor platform.""" """The tests for the nx584 sensor platform."""
from typing import Any
from unittest import mock from unittest import mock
from nx584 import client as nx584_client from nx584 import client as nx584_client
@ -99,7 +100,9 @@ def test_nx584_sensor_setup_full_config(
assert mock_watcher.called 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.""" """Test the failing."""
assert not await async_setup_component(hass, "nx584", config) 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"}}), ({"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.""" """Test the setup with bad configuration."""
await _test_assert_graceful_fail(hass, config) await _test_assert_graceful_fail(hass, config)