Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions tests/components/nest/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion tests/components/nest/test_device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion tests/components/notify/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
components. Instead call the service directly.
"""

from typing import Any

from homeassistant.components.notify import (
ATTR_DATA,
ATTR_MESSAGE,
ATTR_TITLE,
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}

Expand Down
72 changes: 55 additions & 17 deletions tests/components/notify/test_legacy.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -283,11 +295,15 @@ async def async_get_service(hass, config, discovery_info=None):


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")
Expand All @@ -314,19 +330,25 @@ async def async_get_service(hass, config, discovery_info=None):
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}
Expand Down Expand Up @@ -405,18 +427,26 @@ async def async_get_service2(hass, config, discovery_info=None):


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}
Expand Down Expand Up @@ -455,18 +485,26 @@ async def async_get_service2(hass, config, discovery_info=None):


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}
Expand Down
9 changes: 7 additions & 2 deletions tests/components/nx584/test_binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down