1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Add type hints to core tests (#88478)

This commit is contained in:
epenet
2023-02-20 11:42:56 +01:00
committed by GitHub
parent 26755a6841
commit 5f25b71df7
59 changed files with 1068 additions and 547 deletions

View File

@@ -31,6 +31,7 @@ from homeassistant.setup import async_setup_component
from tests.common import (
MockEntity,
MockUser,
async_mock_service,
get_test_home_assistant,
mock_device_registry,
@@ -424,7 +425,7 @@ async def test_service_call_entry_id(hass: HomeAssistant) -> None:
@pytest.mark.parametrize("target", ("all", "none"))
async def test_service_call_all_none(hass, target):
async def test_service_call_all_none(hass: HomeAssistant, target) -> None:
"""Test service call targeting all."""
calls = async_mock_service(hass, "test_domain", "test_service")
@@ -474,7 +475,7 @@ async def test_extract_entity_ids(hass: HomeAssistant) -> None:
)
async def test_extract_entity_ids_from_area(hass, area_mock):
async def test_extract_entity_ids_from_area(hass: HomeAssistant, area_mock) -> None:
"""Test extract_entity_ids method with areas."""
call = ServiceCall("light", "turn_on", {"area_id": "own-area"})
@@ -505,7 +506,7 @@ async def test_extract_entity_ids_from_area(hass, area_mock):
)
async def test_extract_entity_ids_from_devices(hass, area_mock):
async def test_extract_entity_ids_from_devices(hass: HomeAssistant, area_mock) -> None:
"""Test extract_entity_ids method with devices."""
assert await service.async_extract_entity_ids(
hass, ServiceCall("light", "turn_on", {"device_id": "device-no-area-id"})
@@ -551,7 +552,7 @@ async def test_async_get_all_descriptions(hass: HomeAssistant) -> None:
assert "fields" in descriptions[logger.DOMAIN]["set_level"]
async def test_call_with_required_features(hass, mock_entities):
async def test_call_with_required_features(hass: HomeAssistant, mock_entities) -> None:
"""Test service calls invoked only if entity has required features."""
test_service_mock = AsyncMock(return_value=None)
await service.entity_service_call(
@@ -585,7 +586,9 @@ async def test_call_with_required_features(hass, mock_entities):
assert test_service_mock.call_count == 0
async def test_call_with_both_required_features(hass, mock_entities):
async def test_call_with_both_required_features(
hass: HomeAssistant, mock_entities
) -> None:
"""Test service calls invoked only if entity has both features."""
test_service_mock = AsyncMock(return_value=None)
await service.entity_service_call(
@@ -602,7 +605,9 @@ async def test_call_with_both_required_features(hass, mock_entities):
]
async def test_call_with_one_of_required_features(hass, mock_entities):
async def test_call_with_one_of_required_features(
hass: HomeAssistant, mock_entities
) -> None:
"""Test service calls invoked with one entity having the required features."""
test_service_mock = AsyncMock(return_value=None)
await service.entity_service_call(
@@ -623,7 +628,7 @@ async def test_call_with_one_of_required_features(hass, mock_entities):
assert all(entity in actual for entity in expected)
async def test_call_with_sync_func(hass, mock_entities):
async def test_call_with_sync_func(hass: HomeAssistant, mock_entities) -> None:
"""Test invoking sync service calls."""
test_service_mock = Mock(return_value=None)
await service.entity_service_call(
@@ -635,7 +640,7 @@ async def test_call_with_sync_func(hass, mock_entities):
assert test_service_mock.call_count == 1
async def test_call_with_sync_attr(hass, mock_entities):
async def test_call_with_sync_attr(hass: HomeAssistant, mock_entities) -> None:
"""Test invoking sync service calls."""
mock_method = mock_entities["light.kitchen"].sync_method = Mock(return_value=None)
await service.entity_service_call(
@@ -670,7 +675,9 @@ async def test_call_context_user_not_exist(hass: HomeAssistant) -> None:
assert err.value.context.user_id == "non-existing"
async def test_call_context_target_all(hass, mock_handle_entity_call, mock_entities):
async def test_call_context_target_all(
hass: HomeAssistant, mock_handle_entity_call, mock_entities
) -> None:
"""Check we only target allowed entities if targeting all."""
with patch(
"homeassistant.auth.AuthManager.async_get_user",
@@ -697,8 +704,8 @@ async def test_call_context_target_all(hass, mock_handle_entity_call, mock_entit
async def test_call_context_target_specific(
hass, mock_handle_entity_call, mock_entities
):
hass: HomeAssistant, mock_handle_entity_call, mock_entities
) -> None:
"""Check targeting specific entities."""
with patch(
"homeassistant.auth.AuthManager.async_get_user",
@@ -725,8 +732,8 @@ async def test_call_context_target_specific(
async def test_call_context_target_specific_no_auth(
hass, mock_handle_entity_call, mock_entities
):
hass: HomeAssistant, mock_handle_entity_call, mock_entities
) -> None:
"""Check targeting specific entities without auth."""
with pytest.raises(exceptions.Unauthorized) as err, patch(
"homeassistant.auth.AuthManager.async_get_user",
@@ -748,7 +755,9 @@ async def test_call_context_target_specific_no_auth(
assert err.value.entity_id == "light.kitchen"
async def test_call_no_context_target_all(hass, mock_handle_entity_call, mock_entities):
async def test_call_no_context_target_all(
hass: HomeAssistant, mock_handle_entity_call, mock_entities
) -> None:
"""Check we target all if no user context given."""
await service.entity_service_call(
hass,
@@ -766,8 +775,8 @@ async def test_call_no_context_target_all(hass, mock_handle_entity_call, mock_en
async def test_call_no_context_target_specific(
hass, mock_handle_entity_call, mock_entities
):
hass: HomeAssistant, mock_handle_entity_call, mock_entities
) -> None:
"""Check we can target specified entities."""
await service.entity_service_call(
hass,
@@ -785,8 +794,11 @@ async def test_call_no_context_target_specific(
async def test_call_with_match_all(
hass, mock_handle_entity_call, mock_entities, caplog
):
hass: HomeAssistant,
mock_handle_entity_call,
mock_entities,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Check we only target allowed entities if targeting all."""
await service.entity_service_call(
hass,
@@ -801,7 +813,9 @@ async def test_call_with_match_all(
)
async def test_call_with_omit_entity_id(hass, mock_handle_entity_call, mock_entities):
async def test_call_with_omit_entity_id(
hass: HomeAssistant, mock_handle_entity_call, mock_entities
) -> None:
"""Check service call if we do not pass an entity ID."""
await service.entity_service_call(
hass,
@@ -813,7 +827,9 @@ async def test_call_with_omit_entity_id(hass, mock_handle_entity_call, mock_enti
assert len(mock_handle_entity_call.mock_calls) == 0
async def test_register_admin_service(hass, hass_read_only_user, hass_admin_user):
async def test_register_admin_service(
hass: HomeAssistant, hass_read_only_user: MockUser, hass_admin_user: MockUser
) -> None:
"""Test the register admin service."""
calls = []
@@ -880,7 +896,7 @@ async def test_register_admin_service(hass, hass_read_only_user, hass_admin_user
assert calls[0].context.user_id == hass_admin_user.id
async def test_domain_control_not_async(hass, mock_entities):
async def test_domain_control_not_async(hass: HomeAssistant, mock_entities) -> None:
"""Test domain verification in a service call with an unknown user."""
calls = []
@@ -892,7 +908,7 @@ async def test_domain_control_not_async(hass, mock_entities):
service.verify_domain_control(hass, "test_domain")(mock_service_log)
async def test_domain_control_unknown(hass, mock_entities):
async def test_domain_control_unknown(hass: HomeAssistant, mock_entities) -> None:
"""Test domain verification in a service call with an unknown user."""
calls = []
@@ -923,7 +939,9 @@ async def test_domain_control_unknown(hass, mock_entities):
assert len(calls) == 0
async def test_domain_control_unauthorized(hass, hass_read_only_user):
async def test_domain_control_unauthorized(
hass: HomeAssistant, hass_read_only_user: MockUser
) -> None:
"""Test domain verification in a service call with an unauthorized user."""
mock_registry(
hass,
@@ -962,7 +980,9 @@ async def test_domain_control_unauthorized(hass, hass_read_only_user):
assert len(calls) == 0
async def test_domain_control_admin(hass, hass_admin_user):
async def test_domain_control_admin(
hass: HomeAssistant, hass_admin_user: MockUser
) -> None:
"""Test domain verification in a service call with an admin user."""
mock_registry(
hass,
@@ -1114,7 +1134,7 @@ async def test_extract_from_service_filter_out_non_existing_entities(
]
async def test_extract_from_service_area_id(hass, area_mock):
async def test_extract_from_service_area_id(hass: HomeAssistant, area_mock) -> None:
"""Test the extraction using area ID as reference."""
entities = [
MockEntity(name="in_area", entity_id="light.in_area"),
@@ -1149,7 +1169,9 @@ async def test_extract_from_service_area_id(hass, area_mock):
]
async def test_entity_service_call_warn_referenced(hass, caplog):
async def test_entity_service_call_warn_referenced(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test we only warn for referenced entities in entity_service_call."""
call = ServiceCall(
"light",
@@ -1167,7 +1189,9 @@ async def test_entity_service_call_warn_referenced(hass, caplog):
)
async def test_async_extract_entities_warn_referenced(hass, caplog):
async def test_async_extract_entities_warn_referenced(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test we only warn for referenced entities in async_extract_entities."""
call = ServiceCall(
"light",