Improve type hints in esphome tests (#120674)

This commit is contained in:
epenet 2024-06-27 16:33:14 +02:00 committed by GitHub
parent 3895252965
commit e446875c7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 22 deletions

View File

@ -24,6 +24,7 @@ from aioesphomeapi import (
VoiceAssistantFeature, VoiceAssistantFeature,
) )
import pytest import pytest
from typing_extensions import AsyncGenerator
from zeroconf import Zeroconf from zeroconf import Zeroconf
from homeassistant.components.esphome import dashboard from homeassistant.components.esphome import dashboard
@ -175,7 +176,7 @@ def mock_client(mock_device_info) -> APIClient:
@pytest.fixture @pytest.fixture
async def mock_dashboard(hass): async def mock_dashboard(hass: HomeAssistant) -> AsyncGenerator[dict[str, Any]]:
"""Mock dashboard.""" """Mock dashboard."""
data = {"configured": [], "importable": []} data = {"configured": [], "importable": []}
with patch( with patch(

View File

@ -2,6 +2,7 @@
from ipaddress import ip_address from ipaddress import ip_address
import json import json
from typing import Any
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
from aioesphomeapi import ( from aioesphomeapi import (
@ -329,7 +330,7 @@ async def test_user_invalid_password(hass: HomeAssistant, mock_client) -> None:
async def test_user_dashboard_has_wrong_key( async def test_user_dashboard_has_wrong_key(
hass: HomeAssistant, hass: HomeAssistant,
mock_client, mock_client,
mock_dashboard, mock_dashboard: dict[str, Any],
mock_setup_entry: None, mock_setup_entry: None,
) -> None: ) -> None:
"""Test user step with key from dashboard that is incorrect.""" """Test user step with key from dashboard that is incorrect."""
@ -376,7 +377,7 @@ async def test_user_dashboard_has_wrong_key(
async def test_user_discovers_name_and_gets_key_from_dashboard( async def test_user_discovers_name_and_gets_key_from_dashboard(
hass: HomeAssistant, hass: HomeAssistant,
mock_client, mock_client,
mock_dashboard, mock_dashboard: dict[str, Any],
mock_setup_entry: None, mock_setup_entry: None,
) -> None: ) -> None:
"""Test user step can discover the name and get the key from the dashboard.""" """Test user step can discover the name and get the key from the dashboard."""
@ -429,7 +430,7 @@ async def test_user_discovers_name_and_gets_key_from_dashboard_fails(
hass: HomeAssistant, hass: HomeAssistant,
dashboard_exception: Exception, dashboard_exception: Exception,
mock_client, mock_client,
mock_dashboard, mock_dashboard: dict[str, Any],
mock_setup_entry: None, mock_setup_entry: None,
) -> None: ) -> None:
"""Test user step can discover the name and get the key from the dashboard.""" """Test user step can discover the name and get the key from the dashboard."""
@ -484,7 +485,7 @@ async def test_user_discovers_name_and_gets_key_from_dashboard_fails(
async def test_user_discovers_name_and_dashboard_is_unavailable( async def test_user_discovers_name_and_dashboard_is_unavailable(
hass: HomeAssistant, hass: HomeAssistant,
mock_client, mock_client,
mock_dashboard, mock_dashboard: dict[str, Any],
mock_setup_entry: None, mock_setup_entry: None,
) -> None: ) -> None:
"""Test user step can discover the name but the dashboard is unavailable.""" """Test user step can discover the name but the dashboard is unavailable."""
@ -843,7 +844,7 @@ async def test_reauth_confirm_valid(
async def test_reauth_fixed_via_dashboard( async def test_reauth_fixed_via_dashboard(
hass: HomeAssistant, hass: HomeAssistant,
mock_client, mock_client,
mock_dashboard, mock_dashboard: dict[str, Any],
mock_setup_entry: None, mock_setup_entry: None,
) -> None: ) -> None:
"""Test reauth fixed automatically via dashboard.""" """Test reauth fixed automatically via dashboard."""
@ -894,7 +895,7 @@ async def test_reauth_fixed_via_dashboard(
async def test_reauth_fixed_via_dashboard_add_encryption_remove_password( async def test_reauth_fixed_via_dashboard_add_encryption_remove_password(
hass: HomeAssistant, hass: HomeAssistant,
mock_client, mock_client,
mock_dashboard, mock_dashboard: dict[str, Any],
mock_config_entry, mock_config_entry,
mock_setup_entry: None, mock_setup_entry: None,
) -> None: ) -> None:
@ -938,7 +939,7 @@ async def test_reauth_fixed_via_remove_password(
hass: HomeAssistant, hass: HomeAssistant,
mock_client, mock_client,
mock_config_entry, mock_config_entry,
mock_dashboard, mock_dashboard: dict[str, Any],
mock_setup_entry: None, mock_setup_entry: None,
) -> None: ) -> None:
"""Test reauth fixed automatically by seeing password removed.""" """Test reauth fixed automatically by seeing password removed."""
@ -962,7 +963,7 @@ async def test_reauth_fixed_via_remove_password(
async def test_reauth_fixed_via_dashboard_at_confirm( async def test_reauth_fixed_via_dashboard_at_confirm(
hass: HomeAssistant, hass: HomeAssistant,
mock_client, mock_client,
mock_dashboard, mock_dashboard: dict[str, Any],
mock_setup_entry: None, mock_setup_entry: None,
) -> None: ) -> None:
"""Test reauth fixed automatically via dashboard at confirm step.""" """Test reauth fixed automatically via dashboard at confirm step."""
@ -1153,7 +1154,9 @@ async def test_discovery_dhcp_no_changes(
assert entry.data[CONF_HOST] == "192.168.43.183" assert entry.data[CONF_HOST] == "192.168.43.183"
async def test_discovery_hassio(hass: HomeAssistant, mock_dashboard) -> None: async def test_discovery_hassio(
hass: HomeAssistant, mock_dashboard: dict[str, Any]
) -> None:
"""Test dashboard discovery.""" """Test dashboard discovery."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
"esphome", "esphome",
@ -1181,7 +1184,7 @@ async def test_discovery_hassio(hass: HomeAssistant, mock_dashboard) -> None:
async def test_zeroconf_encryption_key_via_dashboard( async def test_zeroconf_encryption_key_via_dashboard(
hass: HomeAssistant, hass: HomeAssistant,
mock_client, mock_client,
mock_dashboard, mock_dashboard: dict[str, Any],
mock_setup_entry: None, mock_setup_entry: None,
) -> None: ) -> None:
"""Test encryption key retrieved from dashboard.""" """Test encryption key retrieved from dashboard."""
@ -1247,7 +1250,7 @@ async def test_zeroconf_encryption_key_via_dashboard(
async def test_zeroconf_encryption_key_via_dashboard_with_api_encryption_prop( async def test_zeroconf_encryption_key_via_dashboard_with_api_encryption_prop(
hass: HomeAssistant, hass: HomeAssistant,
mock_client, mock_client,
mock_dashboard, mock_dashboard: dict[str, Any],
mock_setup_entry: None, mock_setup_entry: None,
) -> None: ) -> None:
"""Test encryption key retrieved from dashboard with api_encryption property set.""" """Test encryption key retrieved from dashboard with api_encryption property set."""
@ -1313,7 +1316,7 @@ async def test_zeroconf_encryption_key_via_dashboard_with_api_encryption_prop(
async def test_zeroconf_no_encryption_key_via_dashboard( async def test_zeroconf_no_encryption_key_via_dashboard(
hass: HomeAssistant, hass: HomeAssistant,
mock_client, mock_client,
mock_dashboard, mock_dashboard: dict[str, Any],
mock_setup_entry: None, mock_setup_entry: None,
) -> None: ) -> None:
"""Test encryption key not retrieved from dashboard.""" """Test encryption key not retrieved from dashboard."""

View File

@ -16,7 +16,10 @@ from tests.common import MockConfigEntry
async def test_dashboard_storage( async def test_dashboard_storage(
hass: HomeAssistant, init_integration, mock_dashboard, hass_storage: dict[str, Any] hass: HomeAssistant,
init_integration,
mock_dashboard: dict[str, Any],
hass_storage: dict[str, Any],
) -> None: ) -> None:
"""Test dashboard storage.""" """Test dashboard storage."""
assert hass_storage[dashboard.STORAGE_KEY]["data"] == { assert hass_storage[dashboard.STORAGE_KEY]["data"] == {
@ -197,7 +200,9 @@ async def test_new_dashboard_fix_reauth(
assert mock_config_entry.data[CONF_NOISE_PSK] == VALID_NOISE_PSK assert mock_config_entry.data[CONF_NOISE_PSK] == VALID_NOISE_PSK
async def test_dashboard_supports_update(hass: HomeAssistant, mock_dashboard) -> None: async def test_dashboard_supports_update(
hass: HomeAssistant, mock_dashboard: dict[str, Any]
) -> None:
"""Test dashboard supports update.""" """Test dashboard supports update."""
dash = dashboard.async_get_dashboard(hass) dash = dashboard.async_get_dashboard(hass)

View File

@ -1,5 +1,6 @@
"""Tests for the diagnostics data provided by the ESPHome integration.""" """Tests for the diagnostics data provided by the ESPHome integration."""
from typing import Any
from unittest.mock import ANY from unittest.mock import ANY
import pytest import pytest
@ -20,7 +21,7 @@ async def test_diagnostics(
hass: HomeAssistant, hass: HomeAssistant,
hass_client: ClientSessionGenerator, hass_client: ClientSessionGenerator,
init_integration: MockConfigEntry, init_integration: MockConfigEntry,
mock_dashboard, mock_dashboard: dict[str, Any],
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test diagnostics for config entry.""" """Test diagnostics for config entry."""

View File

@ -1,6 +1,7 @@
"""Test ESPHome update entities.""" """Test ESPHome update entities."""
from collections.abc import Awaitable, Callable from collections.abc import Awaitable, Callable
from typing import Any
from unittest.mock import Mock, patch from unittest.mock import Mock, patch
from aioesphomeapi import ( from aioesphomeapi import (
@ -84,7 +85,7 @@ async def test_update_entity(
stub_reconnect, stub_reconnect,
mock_config_entry, mock_config_entry,
mock_device_info, mock_device_info,
mock_dashboard, mock_dashboard: dict[str, Any],
devices_payload, devices_payload,
expected_state, expected_state,
expected_attributes, expected_attributes,
@ -190,7 +191,7 @@ async def test_update_static_info(
[APIClient, list[EntityInfo], list[UserService], list[EntityState]], [APIClient, list[EntityInfo], list[UserService], list[EntityState]],
Awaitable[MockESPHomeDevice], Awaitable[MockESPHomeDevice],
], ],
mock_dashboard, mock_dashboard: dict[str, Any],
) -> None: ) -> None:
"""Test ESPHome update entity.""" """Test ESPHome update entity."""
mock_dashboard["configured"] = [ mock_dashboard["configured"] = [
@ -236,7 +237,7 @@ async def test_update_device_state_for_availability(
expected_disconnect: bool, expected_disconnect: bool,
expected_state: str, expected_state: str,
has_deep_sleep: bool, has_deep_sleep: bool,
mock_dashboard, mock_dashboard: dict[str, Any],
mock_client: APIClient, mock_client: APIClient,
mock_esphome_device: Callable[ mock_esphome_device: Callable[
[APIClient, list[EntityInfo], list[UserService], list[EntityState]], [APIClient, list[EntityInfo], list[UserService], list[EntityState]],
@ -272,7 +273,7 @@ async def test_update_entity_dashboard_not_available_startup(
stub_reconnect, stub_reconnect,
mock_config_entry, mock_config_entry,
mock_device_info, mock_device_info,
mock_dashboard, mock_dashboard: dict[str, Any],
) -> None: ) -> None:
"""Test ESPHome update entity when dashboard is not available at startup.""" """Test ESPHome update entity when dashboard is not available at startup."""
with ( with (
@ -321,7 +322,7 @@ async def test_update_entity_dashboard_discovered_after_startup_but_update_faile
[APIClient, list[EntityInfo], list[UserService], list[EntityState]], [APIClient, list[EntityInfo], list[UserService], list[EntityState]],
Awaitable[MockESPHomeDevice], Awaitable[MockESPHomeDevice],
], ],
mock_dashboard, mock_dashboard: dict[str, Any],
) -> None: ) -> None:
"""Test ESPHome update entity when dashboard is discovered after startup and the first update fails.""" """Test ESPHome update entity when dashboard is discovered after startup and the first update fails."""
with patch( with patch(
@ -386,7 +387,7 @@ async def test_update_becomes_available_at_runtime(
[APIClient, list[EntityInfo], list[UserService], list[EntityState]], [APIClient, list[EntityInfo], list[UserService], list[EntityState]],
Awaitable[MockESPHomeDevice], Awaitable[MockESPHomeDevice],
], ],
mock_dashboard, mock_dashboard: dict[str, Any],
) -> None: ) -> None:
"""Test ESPHome update entity when the dashboard has no device at startup but gets them later.""" """Test ESPHome update entity when the dashboard has no device at startup but gets them later."""
await mock_esphome_device( await mock_esphome_device(