Add type hints to integration tests (part 24) (#88307)

This commit is contained in:
epenet 2023-02-17 16:34:53 +01:00 committed by GitHub
parent f3e4783a5e
commit aa50096a31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
49 changed files with 627 additions and 359 deletions

View file

@ -23,8 +23,8 @@ from homeassistant.helpers.json import json_loads
from homeassistant.loader import async_get_integration
from homeassistant.setup import DATA_SETUP_TIME, async_setup_component
from tests.common import MockEntity, MockEntityPlatform, async_mock_service
from tests.typing import WebSocketGenerator
from tests.common import MockEntity, MockEntityPlatform, MockUser, async_mock_service
from tests.typing import ClientSessionGenerator, WebSocketGenerator
STATE_KEY_SHORT_NAMES = {
"entity_id": "e",
@ -59,7 +59,7 @@ def _apply_entities_changes(state_dict: dict, change_dict: dict) -> None:
del state_dict[STATE_KEY_LONG_NAMES[key]][item]
async def test_fire_event(hass, websocket_client):
async def test_fire_event(hass: HomeAssistant, websocket_client) -> None:
"""Test fire event command."""
runs = []
@ -88,7 +88,7 @@ async def test_fire_event(hass, websocket_client):
assert runs[0].data == {"hello": "world"}
async def test_fire_event_without_data(hass, websocket_client):
async def test_fire_event_without_data(hass: HomeAssistant, websocket_client) -> None:
"""Test fire event command."""
runs = []
@ -116,7 +116,7 @@ async def test_fire_event_without_data(hass, websocket_client):
assert runs[0].data == {}
async def test_call_service(hass, websocket_client):
async def test_call_service(hass: HomeAssistant, websocket_client) -> None:
"""Test call service command."""
calls = async_mock_service(hass, "domain_test", "test_service")
@ -145,7 +145,9 @@ async def test_call_service(hass, websocket_client):
@pytest.mark.parametrize("command", ("call_service", "call_service_action"))
async def test_call_service_blocking(hass, websocket_client, command):
async def test_call_service_blocking(
hass: HomeAssistant, websocket_client, command
) -> None:
"""Test call service commands block, except for homeassistant restart / stop."""
with patch(
"homeassistant.core.ServiceRegistry.async_call", autospec=True
@ -221,7 +223,7 @@ async def test_call_service_blocking(hass, websocket_client, command):
)
async def test_call_service_target(hass, websocket_client):
async def test_call_service_target(hass: HomeAssistant, websocket_client) -> None:
"""Test call service command with target."""
calls = async_mock_service(hass, "domain_test", "test_service")
@ -257,7 +259,9 @@ async def test_call_service_target(hass, websocket_client):
assert call.context.as_dict() == msg["result"]["context"]
async def test_call_service_target_template(hass, websocket_client):
async def test_call_service_target_template(
hass: HomeAssistant, websocket_client
) -> None:
"""Test call service command with target does not allow template."""
await websocket_client.send_json(
{
@ -279,7 +283,7 @@ async def test_call_service_target_template(hass, websocket_client):
assert msg["error"]["code"] == const.ERR_INVALID_FORMAT
async def test_call_service_not_found(hass, websocket_client):
async def test_call_service_not_found(hass: HomeAssistant, websocket_client) -> None:
"""Test call service command."""
await websocket_client.send_json(
{
@ -298,7 +302,9 @@ async def test_call_service_not_found(hass, websocket_client):
assert msg["error"]["code"] == const.ERR_NOT_FOUND
async def test_call_service_child_not_found(hass, websocket_client):
async def test_call_service_child_not_found(
hass: HomeAssistant, websocket_client
) -> None:
"""Test not reporting not found errors if it's not the called service."""
async def serv_handler(call):
@ -325,7 +331,7 @@ async def test_call_service_child_not_found(hass, websocket_client):
async def test_call_service_schema_validation_error(
hass: HomeAssistant, websocket_client
):
) -> None:
"""Test call service command with invalid service data."""
calls = []
@ -394,7 +400,7 @@ async def test_call_service_schema_validation_error(
assert len(calls) == 0
async def test_call_service_error(hass, websocket_client):
async def test_call_service_error(hass: HomeAssistant, websocket_client) -> None:
"""Test call service command with error."""
@callback
@ -441,7 +447,9 @@ async def test_call_service_error(hass, websocket_client):
assert msg["error"]["message"] == "value_error"
async def test_subscribe_unsubscribe_events(hass, websocket_client):
async def test_subscribe_unsubscribe_events(
hass: HomeAssistant, websocket_client
) -> None:
"""Test subscribe/unsubscribe events command."""
init_count = sum(hass.bus.async_listeners().values())
@ -485,7 +493,7 @@ async def test_subscribe_unsubscribe_events(hass, websocket_client):
assert sum(hass.bus.async_listeners().values()) == init_count
async def test_get_states(hass, websocket_client):
async def test_get_states(hass: HomeAssistant, websocket_client) -> None:
"""Test get_states command."""
hass.states.async_set("greeting.hello", "world")
hass.states.async_set("greeting.bye", "universe")
@ -504,7 +512,7 @@ async def test_get_states(hass, websocket_client):
assert msg["result"] == states
async def test_get_services(hass, websocket_client):
async def test_get_services(hass: HomeAssistant, websocket_client) -> None:
"""Test get_services command."""
await websocket_client.send_json({"id": 5, "type": "get_services"})
@ -515,7 +523,7 @@ async def test_get_services(hass, websocket_client):
assert msg["result"] == hass.services.async_services()
async def test_get_config(hass, websocket_client):
async def test_get_config(hass: HomeAssistant, websocket_client) -> None:
"""Test get_config command."""
await websocket_client.send_json({"id": 5, "type": "get_config"})
@ -542,7 +550,7 @@ async def test_get_config(hass, websocket_client):
assert msg["result"] == hass.config.as_dict()
async def test_ping(websocket_client):
async def test_ping(websocket_client) -> None:
"""Test get_panels command."""
await websocket_client.send_json({"id": 5, "type": "ping"})
@ -552,8 +560,10 @@ async def test_ping(websocket_client):
async def test_call_service_context_with_user(
hass, hass_client_no_auth, hass_access_token
):
hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator,
hass_access_token: str,
) -> None:
"""Test that the user is set in the service call context."""
assert await async_setup_component(hass, "websocket_api", {})
@ -592,7 +602,9 @@ async def test_call_service_context_with_user(
assert call.context.user_id == refresh_token.user.id
async def test_subscribe_requires_admin(websocket_client, hass_admin_user):
async def test_subscribe_requires_admin(
websocket_client, hass_admin_user: MockUser
) -> None:
"""Test subscribing events without being admin."""
hass_admin_user.groups = []
await websocket_client.send_json(
@ -604,7 +616,9 @@ async def test_subscribe_requires_admin(websocket_client, hass_admin_user):
assert msg["error"]["code"] == const.ERR_UNAUTHORIZED
async def test_states_filters_visible(hass, hass_admin_user, websocket_client):
async def test_states_filters_visible(
hass: HomeAssistant, hass_admin_user: MockUser, websocket_client
) -> None:
"""Test we only get entities that we're allowed to see."""
hass_admin_user.mock_policy({"entities": {"entity_ids": {"test.entity": True}}})
hass.states.async_set("test.entity", "hello")
@ -620,7 +634,7 @@ async def test_states_filters_visible(hass, hass_admin_user, websocket_client):
assert msg["result"][0]["entity_id"] == "test.entity"
async def test_get_states_not_allows_nan(hass, websocket_client):
async def test_get_states_not_allows_nan(hass: HomeAssistant, websocket_client) -> None:
"""Test get_states command converts NaN to None."""
hass.states.async_set("greeting.hello", "world")
hass.states.async_set("greeting.bad", "data", {"hello": float("NaN")})
@ -643,8 +657,8 @@ async def test_get_states_not_allows_nan(hass, websocket_client):
async def test_subscribe_unsubscribe_events_whitelist(
hass, websocket_client, hass_admin_user
):
hass: HomeAssistant, websocket_client, hass_admin_user: MockUser
) -> None:
"""Test subscribe/unsubscribe events on whitelist."""
hass_admin_user.groups = []
@ -680,8 +694,8 @@ async def test_subscribe_unsubscribe_events_whitelist(
async def test_subscribe_unsubscribe_events_state_changed(
hass, websocket_client, hass_admin_user
):
hass: HomeAssistant, websocket_client, hass_admin_user: MockUser
) -> None:
"""Test subscribe/unsubscribe state_changed events."""
hass_admin_user.groups = []
hass_admin_user.mock_policy({"entities": {"entity_ids": {"light.permitted": True}}})
@ -706,8 +720,8 @@ async def test_subscribe_unsubscribe_events_state_changed(
async def test_subscribe_entities_with_unserializable_state(
hass, websocket_client, hass_admin_user
):
hass: HomeAssistant, websocket_client, hass_admin_user: MockUser
) -> None:
"""Test subscribe entities with an unserializeable state."""
class CannotSerializeMe:
@ -822,7 +836,9 @@ async def test_subscribe_entities_with_unserializable_state(
}
async def test_subscribe_unsubscribe_entities(hass, websocket_client, hass_admin_user):
async def test_subscribe_unsubscribe_entities(
hass: HomeAssistant, websocket_client, hass_admin_user: MockUser
) -> None:
"""Test subscribe/unsubscribe entities."""
hass.states.async_set("light.permitted", "off", {"color": "red"})
@ -987,8 +1003,8 @@ async def test_subscribe_unsubscribe_entities(hass, websocket_client, hass_admin
async def test_subscribe_unsubscribe_entities_specific_entities(
hass, websocket_client, hass_admin_user
):
hass: HomeAssistant, websocket_client, hass_admin_user: MockUser
) -> None:
"""Test subscribe/unsubscribe entities with a list of entity ids."""
hass.states.async_set("light.permitted", "off", {"color": "red"})
@ -1048,7 +1064,9 @@ async def test_subscribe_unsubscribe_entities_specific_entities(
}
async def test_render_template_renders_template(hass, websocket_client):
async def test_render_template_renders_template(
hass: HomeAssistant, websocket_client
) -> None:
"""Test simple template is rendered and updated."""
hass.states.async_set("light.test", "on")
@ -1095,7 +1113,9 @@ async def test_render_template_renders_template(hass, websocket_client):
}
async def test_render_template_with_timeout_and_variables(hass, websocket_client):
async def test_render_template_with_timeout_and_variables(
hass: HomeAssistant, websocket_client
) -> None:
"""Test a template with a timeout and variables renders without error."""
await websocket_client.send_json(
{
@ -1128,8 +1148,8 @@ async def test_render_template_with_timeout_and_variables(hass, websocket_client
async def test_render_template_manual_entity_ids_no_longer_needed(
hass, websocket_client
):
hass: HomeAssistant, websocket_client
) -> None:
"""Test that updates to specified entity ids cause a template rerender."""
hass.states.async_set("light.test", "on")
@ -1185,7 +1205,9 @@ async def test_render_template_manual_entity_ids_no_longer_needed(
"{{ now() | unknown_filter }}",
],
)
async def test_render_template_with_error(hass, websocket_client, caplog, template):
async def test_render_template_with_error(
hass: HomeAssistant, websocket_client, caplog: pytest.LogCaptureFixture, template
) -> None:
"""Test a template with an error."""
await websocket_client.send_json(
{"id": 5, "type": "render_template", "template": template, "strict": True}
@ -1211,8 +1233,8 @@ async def test_render_template_with_error(hass, websocket_client, caplog, templa
],
)
async def test_render_template_with_timeout_and_error(
hass, websocket_client, caplog, template
):
hass: HomeAssistant, websocket_client, caplog: pytest.LogCaptureFixture, template
) -> None:
"""Test a template with an error with a timeout."""
await websocket_client.send_json(
{
@ -1234,7 +1256,9 @@ async def test_render_template_with_timeout_and_error(
assert "TemplateError" not in caplog.text
async def test_render_template_error_in_template_code(hass, websocket_client, caplog):
async def test_render_template_error_in_template_code(
hass: HomeAssistant, websocket_client, caplog: pytest.LogCaptureFixture
) -> None:
"""Test a template that will throw in template.py."""
await websocket_client.send_json(
{"id": 5, "type": "render_template", "template": "{{ now() | random }}"}
@ -1249,7 +1273,9 @@ async def test_render_template_error_in_template_code(hass, websocket_client, ca
assert "TemplateError" not in caplog.text
async def test_render_template_with_delayed_error(hass, websocket_client, caplog):
async def test_render_template_with_delayed_error(
hass: HomeAssistant, websocket_client, caplog: pytest.LogCaptureFixture
) -> None:
"""Test a template with an error that only happens after a state change."""
hass.states.async_set("sensor.test", "on")
await hass.async_block_till_done()
@ -1299,7 +1325,9 @@ async def test_render_template_with_delayed_error(hass, websocket_client, caplog
assert "TemplateError" not in caplog.text
async def test_render_template_with_timeout(hass, websocket_client, caplog):
async def test_render_template_with_timeout(
hass: HomeAssistant, websocket_client, caplog: pytest.LogCaptureFixture
) -> None:
"""Test a template that will timeout."""
slow_template_str = """
@ -1328,7 +1356,9 @@ async def test_render_template_with_timeout(hass, websocket_client, caplog):
assert "TemplateError" not in caplog.text
async def test_render_template_returns_with_match_all(hass, websocket_client):
async def test_render_template_returns_with_match_all(
hass: HomeAssistant, websocket_client
) -> None:
"""Test that a template that would match with all entities still return success."""
await websocket_client.send_json(
{"id": 5, "type": "render_template", "template": "State is: {{ 42 }}"}
@ -1340,7 +1370,7 @@ async def test_render_template_returns_with_match_all(hass, websocket_client):
assert msg["success"]
async def test_manifest_list(hass, websocket_client):
async def test_manifest_list(hass: HomeAssistant, websocket_client) -> None:
"""Test loading manifests."""
http = await async_get_integration(hass, "http")
websocket_api = await async_get_integration(hass, "websocket_api")
@ -1357,7 +1387,9 @@ async def test_manifest_list(hass, websocket_client):
]
async def test_manifest_list_specific_integrations(hass, websocket_client):
async def test_manifest_list_specific_integrations(
hass: HomeAssistant, websocket_client
) -> None:
"""Test loading manifests for specific integrations."""
websocket_api = await async_get_integration(hass, "websocket_api")
@ -1376,7 +1408,7 @@ async def test_manifest_list_specific_integrations(hass, websocket_client):
]
async def test_manifest_get(hass, websocket_client):
async def test_manifest_get(hass: HomeAssistant, websocket_client) -> None:
"""Test getting a manifest."""
hue = await async_get_integration(hass, "hue")
@ -1402,7 +1434,9 @@ async def test_manifest_get(hass, websocket_client):
assert msg["error"]["code"] == "not_found"
async def test_entity_source_admin(hass, websocket_client, hass_admin_user):
async def test_entity_source_admin(
hass: HomeAssistant, websocket_client, hass_admin_user: MockUser
) -> None:
"""Check that we fetch sources correctly."""
platform = MockEntityPlatform(hass)
@ -1521,7 +1555,7 @@ async def test_entity_source_admin(hass, websocket_client, hass_admin_user):
assert msg["error"]["code"] == const.ERR_UNAUTHORIZED
async def test_subscribe_trigger(hass, websocket_client):
async def test_subscribe_trigger(hass: HomeAssistant, websocket_client) -> None:
"""Test subscribing to a trigger."""
init_count = sum(hass.bus.async_listeners().values())
@ -1575,7 +1609,7 @@ async def test_subscribe_trigger(hass, websocket_client):
assert sum(hass.bus.async_listeners().values()) == init_count
async def test_test_condition(hass, websocket_client):
async def test_test_condition(hass: HomeAssistant, websocket_client) -> None:
"""Test testing a condition."""
hass.states.async_set("hello.world", "paulus")
@ -1635,7 +1669,7 @@ async def test_test_condition(hass, websocket_client):
assert msg["result"]["result"] is False
async def test_execute_script(hass, websocket_client):
async def test_execute_script(hass: HomeAssistant, websocket_client) -> None:
"""Test testing a condition."""
calls = async_mock_service(hass, "domain_test", "test_service")
@ -1693,8 +1727,8 @@ async def test_execute_script(hass, websocket_client):
async def test_subscribe_unsubscribe_bootstrap_integrations(
hass, websocket_client, hass_admin_user
):
hass: HomeAssistant, websocket_client, hass_admin_user: MockUser
) -> None:
"""Test subscribe/unsubscribe bootstrap_integrations."""
await websocket_client.send_json(
{"id": 7, "type": "subscribe_bootstrap_integrations"}
@ -1714,7 +1748,9 @@ async def test_subscribe_unsubscribe_bootstrap_integrations(
assert msg["event"] == message
async def test_integration_setup_info(hass, websocket_client, hass_admin_user):
async def test_integration_setup_info(
hass: HomeAssistant, websocket_client, hass_admin_user: MockUser
) -> None:
"""Test subscribe/unsubscribe bootstrap_integrations."""
hass.data[DATA_SETUP_TIME] = {
"august": datetime.timedelta(seconds=12.5),
@ -1743,7 +1779,7 @@ async def test_integration_setup_info(hass, websocket_client, hass_admin_user):
("action", {"service": "domain_test.test_service"}),
),
)
async def test_validate_config_works(websocket_client, key, config):
async def test_validate_config_works(websocket_client, key, config) -> None:
"""Test config validation."""
await websocket_client.send_json({"id": 7, "type": "validate_config", key: config})
@ -1781,7 +1817,7 @@ async def test_validate_config_works(websocket_client, key, config):
),
),
)
async def test_validate_config_invalid(websocket_client, key, config, error):
async def test_validate_config_invalid(websocket_client, key, config, error) -> None:
"""Test config validation."""
await websocket_client.send_json({"id": 7, "type": "validate_config", key: config})
@ -1792,7 +1828,9 @@ async def test_validate_config_invalid(websocket_client, key, config, error):
assert msg["result"] == {key: {"valid": False, "error": error}}
async def test_message_coalescing(hass, websocket_client, hass_admin_user):
async def test_message_coalescing(
hass: HomeAssistant, websocket_client, hass_admin_user: MockUser
) -> None:
"""Test enabling message coalescing."""
await websocket_client.send_json(
{
@ -1863,8 +1901,8 @@ async def test_message_coalescing(hass, websocket_client, hass_admin_user):
async def test_message_coalescing_not_supported_by_websocket_client(
hass, websocket_client, hass_admin_user
):
hass: HomeAssistant, websocket_client, hass_admin_user: MockUser
) -> None:
"""Test enabling message coalescing not supported by websocket client."""
await websocket_client.send_json({"id": 7, "type": "subscribe_entities"})
@ -1904,7 +1942,9 @@ async def test_message_coalescing_not_supported_by_websocket_client(
await hass.async_block_till_done()
async def test_client_message_coalescing(hass, websocket_client, hass_admin_user):
async def test_client_message_coalescing(
hass: HomeAssistant, websocket_client, hass_admin_user: MockUser
) -> None:
"""Test client message coalescing."""
await websocket_client.send_json(
[