Improve type hints in mobile_app tests (#123793)

This commit is contained in:
epenet 2024-08-13 15:19:08 +02:00 committed by GitHub
parent ae74fdf252
commit 04b1d2414d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 147 additions and 73 deletions

View file

@ -1,6 +1,7 @@
"""Tests for mobile_app component."""
from http import HTTPStatus
from typing import Any
from aiohttp.test_utils import TestClient
import pytest
@ -15,7 +16,9 @@ from tests.typing import ClientSessionGenerator
@pytest.fixture
async def create_registrations(hass, webhook_client):
async def create_registrations(
hass: HomeAssistant, webhook_client: TestClient
) -> tuple[dict[str, Any], dict[str, Any]]:
"""Return two new registrations."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@ -37,7 +40,7 @@ async def create_registrations(hass, webhook_client):
@pytest.fixture
async def push_registration(hass, webhook_client):
async def push_registration(hass: HomeAssistant, webhook_client: TestClient):
"""Return registration with push notifications enabled."""
await async_setup_component(hass, DOMAIN, {DOMAIN: {}})

View file

@ -1,7 +1,9 @@
"""Entity tests for mobile_app."""
from http import HTTPStatus
from typing import Any
from aiohttp.test_utils import TestClient
import pytest
from homeassistant.const import STATE_UNKNOWN
@ -12,8 +14,8 @@ from homeassistant.helpers import device_registry as dr
async def test_sensor(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
create_registrations,
webhook_client,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that sensors can be registered and updated."""
webhook_id = create_registrations[1]["webhook_id"]
@ -98,7 +100,9 @@ async def test_sensor(
async def test_sensor_must_register(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that sensors must be registered before updating."""
webhook_id = create_registrations[1]["webhook_id"]
@ -122,8 +126,8 @@ async def test_sensor_must_register(
async def test_sensor_id_no_dupes(
hass: HomeAssistant,
create_registrations,
webhook_client,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that a duplicate unique ID in registration updates the sensor."""
@ -185,7 +189,9 @@ async def test_sensor_id_no_dupes(
async def test_register_sensor_no_state(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that sensors can be registered, when there is no (unknown) state."""
webhook_id = create_registrations[1]["webhook_id"]
@ -244,7 +250,9 @@ async def test_register_sensor_no_state(
async def test_update_sensor_no_state(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that sensors can be updated, when there is no (unknown) state."""
webhook_id = create_registrations[1]["webhook_id"]

View file

@ -1,12 +1,17 @@
"""Test mobile app device tracker."""
from http import HTTPStatus
from typing import Any
from aiohttp.test_utils import TestClient
from homeassistant.core import HomeAssistant
async def test_sending_location(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test sending a location via a webhook."""
resp = await webhook_client.post(
@ -76,7 +81,9 @@ async def test_sending_location(
async def test_restoring_location(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test sending a location via a webhook."""
resp = await webhook_client.post(

View file

@ -1,8 +1,10 @@
"""Entity tests for mobile_app."""
from http import HTTPStatus
from typing import Any
from unittest.mock import patch
from aiohttp.test_utils import TestClient
import pytest
from homeassistant.components.sensor import SensorDeviceClass
@ -14,7 +16,11 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM
from homeassistant.util.unit_system import (
METRIC_SYSTEM,
US_CUSTOMARY_SYSTEM,
UnitSystem,
)
@pytest.mark.parametrize(
@ -28,12 +34,12 @@ async def test_sensor(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
create_registrations,
webhook_client,
unit_system,
state_unit,
state1,
state2,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
unit_system: UnitSystem,
state_unit: UnitOfTemperature,
state1: str,
state2: str,
) -> None:
"""Test that sensors can be registered and updated."""
hass.config.units = unit_system
@ -149,13 +155,13 @@ async def test_sensor(
)
async def test_sensor_migration(
hass: HomeAssistant,
create_registrations,
webhook_client,
unique_id,
unit_system,
state_unit,
state1,
state2,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
unique_id: str,
unit_system: UnitSystem,
state_unit: UnitOfTemperature,
state1: str,
state2: str,
) -> None:
"""Test migration to RestoreSensor."""
hass.config.units = unit_system
@ -243,7 +249,9 @@ async def test_sensor_migration(
async def test_sensor_must_register(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that sensors must be registered before updating."""
webhook_id = create_registrations[1]["webhook_id"]
@ -265,8 +273,8 @@ async def test_sensor_must_register(
async def test_sensor_id_no_dupes(
hass: HomeAssistant,
create_registrations,
webhook_client,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that a duplicate unique ID in registration updates the sensor."""
@ -331,7 +339,9 @@ async def test_sensor_id_no_dupes(
async def test_register_sensor_no_state(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that sensors can be registered, when there is no (unknown) state."""
webhook_id = create_registrations[1]["webhook_id"]
@ -390,7 +400,9 @@ async def test_register_sensor_no_state(
async def test_update_sensor_no_state(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that sensors can be updated, when there is no (unknown) state."""
webhook_id = create_registrations[1]["webhook_id"]
@ -464,11 +476,11 @@ async def test_update_sensor_no_state(
)
async def test_sensor_datetime(
hass: HomeAssistant,
create_registrations,
webhook_client,
device_class,
native_value,
state_value,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
device_class: SensorDeviceClass,
native_value: str,
state_value: str,
) -> None:
"""Test that sensors can be registered and updated."""
webhook_id = create_registrations[1]["webhook_id"]
@ -505,8 +517,8 @@ async def test_sensor_datetime(
async def test_default_disabling_entity(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
create_registrations,
webhook_client,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that sensors can be disabled by default upon registration."""
webhook_id = create_registrations[1]["webhook_id"]
@ -543,8 +555,8 @@ async def test_default_disabling_entity(
async def test_updating_disabled_sensor(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
create_registrations,
webhook_client,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that sensors return error if disabled in instance."""
webhook_id = create_registrations[1]["webhook_id"]

View file

@ -1,10 +1,13 @@
"""Webhook tests for mobile_app."""
from binascii import unhexlify
from collections.abc import Callable
from http import HTTPStatus
import json
from typing import Any
from unittest.mock import ANY, patch
from aiohttp.test_utils import TestClient
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
import pytest
@ -31,7 +34,7 @@ from tests.components.conversation import MockAgent
@pytest.fixture
async def homeassistant(hass):
async def homeassistant(hass: HomeAssistant) -> None:
"""Load the homeassistant integration."""
await async_setup_component(hass, "homeassistant", {})
@ -93,7 +96,8 @@ def decrypt_payload_legacy(secret_key, encrypted_data):
async def test_webhook_handle_render_template(
create_registrations, webhook_client
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that we render templates properly."""
resp = await webhook_client.post(
@ -121,7 +125,9 @@ async def test_webhook_handle_render_template(
async def test_webhook_handle_call_services(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that we call services properly."""
calls = async_mock_service(hass, "test", "mobile_app")
@ -137,7 +143,9 @@ async def test_webhook_handle_call_services(
async def test_webhook_handle_fire_event(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that we can fire events."""
events = []
@ -161,7 +169,7 @@ async def test_webhook_handle_fire_event(
assert events[0].data["hello"] == "yo world"
async def test_webhook_update_registration(webhook_client) -> None:
async def test_webhook_update_registration(webhook_client: TestClient) -> None:
"""Test that a we can update an existing registration via webhook."""
register_resp = await webhook_client.post(
"/api/mobile_app/registrations", json=REGISTER_CLEARTEXT
@ -186,7 +194,9 @@ async def test_webhook_update_registration(webhook_client) -> None:
async def test_webhook_handle_get_zones(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that we can get zones properly."""
# Zone is already loaded as part of the fixture,
@ -238,7 +248,9 @@ async def test_webhook_handle_get_zones(
async def test_webhook_handle_get_config(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that we can get config properly."""
webhook_id = create_registrations[1]["webhook_id"]
@ -299,7 +311,9 @@ async def test_webhook_handle_get_config(
async def test_webhook_returns_error_incorrect_json(
webhook_client, create_registrations, caplog: pytest.LogCaptureFixture
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that an error is returned when JSON is invalid."""
resp = await webhook_client.post(
@ -323,7 +337,11 @@ async def test_webhook_returns_error_incorrect_json(
],
)
async def test_webhook_handle_decryption(
hass: HomeAssistant, webhook_client, create_registrations, msg, generate_response
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
msg: dict[str, Any],
generate_response: Callable[[HomeAssistant], dict[str, Any]],
) -> None:
"""Test that we can encrypt/decrypt properly."""
key = create_registrations[0]["secret"]
@ -346,7 +364,8 @@ async def test_webhook_handle_decryption(
async def test_webhook_handle_decryption_legacy(
webhook_client, create_registrations
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that we can encrypt/decrypt properly."""
key = create_registrations[0]["secret"]
@ -369,7 +388,9 @@ async def test_webhook_handle_decryption_legacy(
async def test_webhook_handle_decryption_fail(
webhook_client, create_registrations, caplog: pytest.LogCaptureFixture
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that we can encrypt/decrypt properly."""
key = create_registrations[0]["secret"]
@ -412,7 +433,9 @@ async def test_webhook_handle_decryption_fail(
async def test_webhook_handle_decryption_legacy_fail(
webhook_client, create_registrations, caplog: pytest.LogCaptureFixture
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test that we can encrypt/decrypt properly."""
key = create_registrations[0]["secret"]
@ -455,7 +478,8 @@ async def test_webhook_handle_decryption_legacy_fail(
async def test_webhook_handle_decryption_legacy_upgrade(
webhook_client, create_registrations
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that we can encrypt/decrypt properly."""
key = create_registrations[0]["secret"]
@ -510,7 +534,8 @@ async def test_webhook_handle_decryption_legacy_upgrade(
async def test_webhook_requires_encryption(
webhook_client, create_registrations
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that encrypted registrations only accept encrypted data."""
resp = await webhook_client.post(
@ -527,7 +552,9 @@ async def test_webhook_requires_encryption(
async def test_webhook_update_location_without_locations(
hass: HomeAssistant, webhook_client, create_registrations
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that location can be updated."""
@ -564,7 +591,9 @@ async def test_webhook_update_location_without_locations(
async def test_webhook_update_location_with_gps(
hass: HomeAssistant, webhook_client, create_registrations
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that location can be updated."""
resp = await webhook_client.post(
@ -586,7 +615,9 @@ async def test_webhook_update_location_with_gps(
async def test_webhook_update_location_with_gps_without_accuracy(
hass: HomeAssistant, webhook_client, create_registrations
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that location can be updated."""
resp = await webhook_client.post(
@ -604,7 +635,9 @@ async def test_webhook_update_location_with_gps_without_accuracy(
async def test_webhook_update_location_with_location_name(
hass: HomeAssistant, webhook_client, create_registrations
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that location can be updated."""
@ -666,7 +699,9 @@ async def test_webhook_update_location_with_location_name(
async def test_webhook_enable_encryption(
hass: HomeAssistant, webhook_client, create_registrations
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that encryption can be added to a reg initially created without."""
webhook_id = create_registrations[1]["webhook_id"]
@ -717,7 +752,9 @@ async def test_webhook_enable_encryption(
async def test_webhook_camera_stream_non_existent(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test fetching camera stream URLs for a non-existent camera."""
webhook_id = create_registrations[1]["webhook_id"]
@ -736,7 +773,9 @@ async def test_webhook_camera_stream_non_existent(
async def test_webhook_camera_stream_non_hls(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test fetching camera stream URLs for a non-HLS/stream-supporting camera."""
hass.states.async_set("camera.non_stream_camera", "idle", {"supported_features": 0})
@ -761,7 +800,9 @@ async def test_webhook_camera_stream_non_hls(
async def test_webhook_camera_stream_stream_available(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test fetching camera stream URLs for an HLS/stream-supporting camera."""
hass.states.async_set(
@ -791,7 +832,9 @@ async def test_webhook_camera_stream_stream_available(
async def test_webhook_camera_stream_stream_available_but_errors(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test fetching camera stream URLs for an HLS/stream-supporting camera but that streaming errors."""
hass.states.async_set(
@ -823,8 +866,8 @@ async def test_webhook_camera_stream_stream_available_but_errors(
async def test_webhook_handle_scan_tag(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
create_registrations,
webhook_client,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that we can scan tags."""
device = device_registry.async_get_device(identifiers={(DOMAIN, "mock-device-id")})
@ -847,7 +890,9 @@ async def test_webhook_handle_scan_tag(
async def test_register_sensor_limits_state_class(
hass: HomeAssistant, create_registrations, webhook_client
hass: HomeAssistant,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that we limit state classes to sensors only."""
webhook_id = create_registrations[1]["webhook_id"]
@ -890,8 +935,8 @@ async def test_register_sensor_limits_state_class(
async def test_reregister_sensor(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
create_registrations,
webhook_client,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that we can add more info in re-registration."""
webhook_id = create_registrations[1]["webhook_id"]
@ -992,11 +1037,11 @@ async def test_reregister_sensor(
assert entry.original_icon is None
@pytest.mark.usefixtures("homeassistant")
async def test_webhook_handle_conversation_process(
hass: HomeAssistant,
homeassistant,
create_registrations,
webhook_client,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
mock_conversation_agent: MockAgent,
) -> None:
"""Test that we can converse."""
@ -1042,9 +1087,8 @@ async def test_webhook_handle_conversation_process(
async def test_sending_sensor_state(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
create_registrations,
webhook_client,
caplog: pytest.LogCaptureFixture,
create_registrations: tuple[dict[str, Any], dict[str, Any]],
webhook_client: TestClient,
) -> None:
"""Test that we can register and send sensor state as number and None."""
webhook_id = create_registrations[1]["webhook_id"]