Add missing hass type hint in alexa tests (#124064)

* Add missing hass type hint in alexa tests

* One more
This commit is contained in:
epenet 2024-08-16 19:00:44 +02:00 committed by GitHub
parent 8a110abc82
commit 9b11aaf1eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 69 additions and 31 deletions

View File

@ -10,14 +10,14 @@ from tests.test_util.aiohttp import AiohttpClientMocker
async def run_auth_get_access_token( async def run_auth_get_access_token(
hass, hass: HomeAssistant,
aioclient_mock, aioclient_mock: AiohttpClientMocker,
expires_in, expires_in: int,
client_id, client_id: str,
client_secret, client_secret: str,
accept_grant_code, accept_grant_code: str,
refresh_token, refresh_token: str,
): ) -> None:
"""Do auth and request a new token for tests.""" """Do auth and request a new token for tests."""
aioclient_mock.post( aioclient_mock.post(
TEST_TOKEN_URL, TEST_TOKEN_URL,

View File

@ -1,5 +1,8 @@
"""Test helpers for the Alexa integration.""" """Test helpers for the Alexa integration."""
from __future__ import annotations
from typing import Any
from unittest.mock import Mock from unittest.mock import Mock
from uuid import uuid4 from uuid import uuid4
@ -7,7 +10,7 @@ import pytest
from homeassistant.components.alexa import config, smart_home from homeassistant.components.alexa import config, smart_home
from homeassistant.components.alexa.const import CONF_ENDPOINT, CONF_FILTER, CONF_LOCALE from homeassistant.components.alexa.const import CONF_ENDPOINT, CONF_FILTER, CONF_LOCALE
from homeassistant.core import Context, HomeAssistant, callback from homeassistant.core import Context, HomeAssistant, ServiceCall, callback
from homeassistant.helpers import entityfilter from homeassistant.helpers import entityfilter
from tests.common import async_mock_service from tests.common import async_mock_service
@ -62,7 +65,7 @@ class MockConfig(smart_home.AlexaConfig):
"""Accept a grant.""" """Accept a grant."""
def get_default_config(hass): def get_default_config(hass: HomeAssistant) -> MockConfig:
"""Return a MockConfig instance.""" """Return a MockConfig instance."""
return MockConfig(hass) return MockConfig(hass)
@ -93,15 +96,15 @@ def get_new_request(namespace, name, endpoint=None):
async def assert_request_calls_service( async def assert_request_calls_service(
namespace, namespace: str,
name, name: str,
endpoint, endpoint: str,
service, service: str,
hass, hass: HomeAssistant,
response_type="Response", response_type="Response",
payload=None, payload: dict[str, Any] | None = None,
instance=None, instance: str | None = None,
): ) -> tuple[ServiceCall, dict[str, Any]]:
"""Assert an API request calls a hass service.""" """Assert an API request calls a hass service."""
context = Context() context = Context()
request = get_new_request(namespace, name, endpoint) request = get_new_request(namespace, name, endpoint)
@ -129,8 +132,14 @@ async def assert_request_calls_service(
async def assert_request_fails( async def assert_request_fails(
namespace, name, endpoint, service_not_called, hass, payload=None, instance=None namespace: str,
): name: str,
endpoint: str,
service_not_called: str,
hass: HomeAssistant,
payload: dict[str, Any] | None = None,
instance: str | None = None,
) -> None:
"""Assert an API request returns an ErrorResponse.""" """Assert an API request returns an ErrorResponse."""
request = get_new_request(namespace, name, endpoint) request = get_new_request(namespace, name, endpoint)
if payload: if payload:
@ -152,8 +161,12 @@ async def assert_request_fails(
async def assert_power_controller_works( async def assert_power_controller_works(
endpoint, on_service, off_service, hass, timestamp endpoint: str,
): on_service: str,
off_service: str,
hass: HomeAssistant,
timestamp: str,
) -> None:
"""Assert PowerController API requests work.""" """Assert PowerController API requests work."""
_, response = await assert_request_calls_service( _, response = await assert_request_calls_service(
"Alexa.PowerController", "TurnOn", endpoint, on_service, hass "Alexa.PowerController", "TurnOn", endpoint, on_service, hass
@ -169,8 +182,12 @@ async def assert_power_controller_works(
async def assert_scene_controller_works( async def assert_scene_controller_works(
endpoint, activate_service, deactivate_service, hass, timestamp endpoint: str,
): activate_service: str,
deactivate_service: str,
hass: HomeAssistant,
timestamp: str,
) -> None:
"""Assert SceneController API requests work.""" """Assert SceneController API requests work."""
_, response = await assert_request_calls_service( _, response = await assert_request_calls_service(
"Alexa.SceneController", "Alexa.SceneController",
@ -196,7 +213,9 @@ async def assert_scene_controller_works(
assert response["event"]["payload"]["timestamp"] == timestamp assert response["event"]["payload"]["timestamp"] == timestamp
async def reported_properties(hass, endpoint, return_full_response=False): async def reported_properties(
hass: HomeAssistant, endpoint: str, return_full_response: bool = False
) -> ReportedProperties:
"""Use ReportState to get properties and return them. """Use ReportState to get properties and return them.
The result is a ReportedProperties instance, which has methods to make The result is a ReportedProperties instance, which has methods to make

View File

@ -120,7 +120,9 @@ async def test_wrong_version(hass: HomeAssistant) -> None:
await smart_home.async_handle_message(hass, get_default_config(hass), msg) await smart_home.async_handle_message(hass, get_default_config(hass), msg)
async def discovery_test(device, hass, expected_endpoints=1): async def discovery_test(
device, hass: HomeAssistant, expected_endpoints: int = 1
) -> dict[str, Any] | list[dict[str, Any]] | None:
"""Test alexa discovery request.""" """Test alexa discovery request."""
request = get_new_request("Alexa.Discovery", "Discover") request = get_new_request("Alexa.Discovery", "Discover")
@ -2601,8 +2603,15 @@ async def test_stop_valve(
async def assert_percentage_changes( async def assert_percentage_changes(
hass, adjustments, namespace, name, endpoint, parameter, service, changed_parameter hass: HomeAssistant,
): adjustments,
namespace,
name,
endpoint,
parameter,
service,
changed_parameter,
) -> None:
"""Assert an API request making percentage changes works. """Assert an API request making percentage changes works.
AdjustPercentage, AdjustBrightness, etc. are examples of such requests. AdjustPercentage, AdjustBrightness, etc. are examples of such requests.
@ -2616,8 +2625,15 @@ async def assert_percentage_changes(
async def assert_range_changes( async def assert_range_changes(
hass, adjustments, namespace, name, endpoint, service, changed_parameter, instance hass: HomeAssistant,
): adjustments: list[tuple[int | str, int, bool]],
namespace: str,
name: str,
endpoint: str,
service: str,
changed_parameter: str | None,
instance: str,
) -> None:
"""Assert an API request making range changes works. """Assert an API request making range changes works.
AdjustRangeValue are examples of such requests. AdjustRangeValue are examples of such requests.

View File

@ -5,6 +5,7 @@ import json
import logging import logging
from typing import Any from typing import Any
from aiohttp import ClientResponse
import pytest import pytest
from homeassistant.components.alexa import DOMAIN, smart_home from homeassistant.components.alexa import DOMAIN, smart_home
@ -17,7 +18,9 @@ from .test_common import get_new_request
from tests.typing import ClientSessionGenerator from tests.typing import ClientSessionGenerator
async def do_http_discovery(config, hass, hass_client): async def do_http_discovery(
config: dict[str, Any], hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> ClientResponse:
"""Submit a request to the Smart Home HTTP API.""" """Submit a request to the Smart Home HTTP API."""
await async_setup_component(hass, DOMAIN, config) await async_setup_component(hass, DOMAIN, config)
http_client = await hass_client() http_client = await hass_client()