Add type hints to tests (#89497)

This commit is contained in:
epenet 2023-03-10 12:06:50 +01:00 committed by GitHub
parent b8bda93d87
commit a0f725dfcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 109 additions and 66 deletions

View File

@ -86,7 +86,7 @@ async def test_name(thermostat) -> None:
assert thermostat.name == "Ecobee"
async def test_aux_heat_not_supported_by_default(hass):
async def test_aux_heat_not_supported_by_default(hass: HomeAssistant) -> None:
"""Default setup should not support Aux heat."""
await setup_platform(hass, const.Platform.CLIMATE)
state = hass.states.get(ENTITY_ID)
@ -100,7 +100,7 @@ async def test_aux_heat_not_supported_by_default(hass):
)
async def test_aux_heat_supported_with_heat_pump(hass):
async def test_aux_heat_supported_with_heat_pump(hass: HomeAssistant) -> None:
"""Aux Heat should be supported if thermostat has heatpump."""
mock_get_thermostat = mock.Mock()
mock_get_thermostat.return_value = GENERIC_THERMOSTAT_INFO_WITH_HEATPUMP
@ -242,7 +242,7 @@ async def test_extra_state_attributes(ecobee_fixture, thermostat) -> None:
} == thermostat.extra_state_attributes
async def test_is_aux_heat_on(hass):
async def test_is_aux_heat_on(hass: HomeAssistant) -> None:
"""Test aux heat property is only enabled for auxHeatOnly."""
mock_get_thermostat = mock.Mock()
mock_get_thermostat.return_value = copy.deepcopy(
@ -255,7 +255,7 @@ async def test_is_aux_heat_on(hass):
assert state.attributes[climate.ATTR_AUX_HEAT] == "on"
async def test_is_aux_heat_off(hass):
async def test_is_aux_heat_off(hass: HomeAssistant) -> None:
"""Test aux heat property is only enabled for auxHeatOnly."""
mock_get_thermostat = mock.Mock()
mock_get_thermostat.return_value = GENERIC_THERMOSTAT_INFO_WITH_HEATPUMP

View File

@ -1,5 +1,4 @@
"""Tests for the sensors provided by the EnergyZero integration."""
from unittest.mock import MagicMock
from energyzero import EnergyZeroNoDataError

View File

@ -335,7 +335,7 @@ async def test_ingress_missing_peername(
async def test_forwarding_paths_as_requested(
hassio_noauth_client, aioclient_mock
hassio_noauth_client, aioclient_mock: AiohttpClientMocker
) -> None:
"""Test incomnig URLs with double encoding go out as dobule encoded."""
# This double encoded string should be forwarded double-encoded too.

View File

@ -283,7 +283,7 @@ async def test_alerts(
)
async def test_alerts_refreshed_on_component_load(
hass: HomeAssistant,
hass_ws_client,
hass_ws_client: WebSocketGenerator,
aioclient_mock: AiohttpClientMocker,
ha_version,
supervisor_info,

View File

@ -5,7 +5,7 @@ import json
from homeassistant.components.homematicip_cloud.helpers import is_error_response
async def test_is_error_response():
async def test_is_error_response() -> None:
"""Test, if an response is a normal result or an error."""
assert not is_error_response("True")
assert not is_error_response(True)

View File

@ -12,13 +12,14 @@ from homeassistant.components.lock import (
LockEntityFeature,
)
from homeassistant.const import ATTR_SUPPORTED_FEATURES
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.setup import async_setup_component
from .helper import async_manipulate_test_data, get_and_check_entity_basics
async def test_manually_configured_platform(hass):
async def test_manually_configured_platform(hass: HomeAssistant) -> None:
"""Test that we do not set up an access point."""
assert await async_setup_component(
hass, DOMAIN, {DOMAIN: {"platform": HMIPC_DOMAIN}}
@ -26,7 +27,9 @@ async def test_manually_configured_platform(hass):
assert not hass.data.get(HMIPC_DOMAIN)
async def test_hmip_doorlockdrive(hass, default_mock_hap_factory):
async def test_hmip_doorlockdrive(
hass: HomeAssistant, default_mock_hap_factory
) -> None:
"""Test HomematicipDoorLockDrive."""
entity_id = "lock.haustuer"
entity_name = "Haustuer"
@ -82,7 +85,9 @@ async def test_hmip_doorlockdrive(hass, default_mock_hap_factory):
assert ha_state.state == STATE_UNLOCKING
async def test_hmip_doorlockdrive_handle_errors(hass, default_mock_hap_factory):
async def test_hmip_doorlockdrive_handle_errors(
hass: HomeAssistant, default_mock_hap_factory
) -> None:
"""Test HomematicipDoorLockDrive."""
entity_id = "lock.haustuer"
entity_name = "Haustuer"

View File

@ -1,5 +1,4 @@
"""Test the pjlink media player platform."""
from datetime import timedelta
import socket
from unittest.mock import create_autospec, patch
@ -11,6 +10,7 @@ import pytest
import homeassistant.components.media_player as media_player
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt
@ -48,7 +48,9 @@ def mocked_projector(projector_from_address):
@pytest.mark.parametrize("side_effect", [socket.timeout, OSError])
async def test_offline_initialization(projector_from_address, hass, side_effect):
async def test_offline_initialization(
projector_from_address, hass: HomeAssistant, side_effect
) -> None:
"""Test initialization of a device that is offline."""
with assert_setup_component(1, media_player.DOMAIN):
@ -71,7 +73,7 @@ async def test_offline_initialization(projector_from_address, hass, side_effect)
assert state.state == "unavailable"
async def test_initialization(projector_from_address, hass):
async def test_initialization(projector_from_address, hass: HomeAssistant) -> None:
"""Test a device that is available."""
with assert_setup_component(1, media_player.DOMAIN):
@ -108,7 +110,9 @@ async def test_initialization(projector_from_address, hass):
@pytest.mark.parametrize("power_state", ["on", "warm-up"])
async def test_on_state_init(projector_from_address, hass, power_state):
async def test_on_state_init(
projector_from_address, hass: HomeAssistant, power_state
) -> None:
"""Test a device that is available."""
with assert_setup_component(1, media_player.DOMAIN):
@ -139,7 +143,7 @@ async def test_on_state_init(projector_from_address, hass, power_state):
assert state.attributes["source"] == "HDMI 1"
async def test_api_error(projector_from_address, hass):
async def test_api_error(projector_from_address, hass: HomeAssistant) -> None:
"""Test invalid api responses."""
with assert_setup_component(1, media_player.DOMAIN):
@ -171,7 +175,7 @@ async def test_api_error(projector_from_address, hass):
assert state.state == "off"
async def test_update_unavailable(projector_from_address, hass):
async def test_update_unavailable(projector_from_address, hass: HomeAssistant) -> None:
"""Test update to a device that is unavailable."""
with assert_setup_component(1, media_player.DOMAIN):
@ -209,7 +213,7 @@ async def test_update_unavailable(projector_from_address, hass):
assert state.state == "unavailable"
async def test_unavailable_time(mocked_projector, hass):
async def test_unavailable_time(mocked_projector, hass: HomeAssistant) -> None:
"""Test unavailable time projector error."""
assert await async_setup_component(
@ -240,7 +244,7 @@ async def test_unavailable_time(mocked_projector, hass):
assert "is_volume_muted" not in state.attributes
async def test_turn_off(mocked_projector, hass):
async def test_turn_off(mocked_projector, hass: HomeAssistant) -> None:
"""Test turning off beamer."""
assert await async_setup_component(
@ -265,7 +269,7 @@ async def test_turn_off(mocked_projector, hass):
mocked_projector.set_power.assert_called_with("off")
async def test_turn_on(mocked_projector, hass):
async def test_turn_on(mocked_projector, hass: HomeAssistant) -> None:
"""Test turning on beamer."""
assert await async_setup_component(
@ -290,7 +294,7 @@ async def test_turn_on(mocked_projector, hass):
mocked_projector.set_power.assert_called_with("on")
async def test_mute(mocked_projector, hass):
async def test_mute(mocked_projector, hass: HomeAssistant) -> None:
"""Test muting beamer."""
assert await async_setup_component(
@ -315,7 +319,7 @@ async def test_mute(mocked_projector, hass):
mocked_projector.set_mute.assert_called_with(MUTE_AUDIO, True)
async def test_unmute(mocked_projector, hass):
async def test_unmute(mocked_projector, hass: HomeAssistant) -> None:
"""Test unmuting beamer."""
assert await async_setup_component(
@ -340,7 +344,7 @@ async def test_unmute(mocked_projector, hass):
mocked_projector.set_mute.assert_called_with(MUTE_AUDIO, False)
async def test_select_source(mocked_projector, hass):
async def test_select_source(mocked_projector, hass: HomeAssistant) -> None:
"""Test selecting source."""
assert await async_setup_component(

View File

@ -9,10 +9,11 @@ from homeassistant.components import camera
from homeassistant.components.camera import Image
from homeassistant.components.prosegur.const import DOMAIN
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
async def test_camera(hass, init_integration):
async def test_camera(hass: HomeAssistant, init_integration) -> None:
"""Test prosegur get_image."""
image = await camera.async_get_image(hass, "camera.test_cam")
@ -20,7 +21,12 @@ async def test_camera(hass, init_integration):
assert image == Image(content_type="image/jpeg", content=b"ABC")
async def test_camera_fail(hass, init_integration, mock_install, caplog):
async def test_camera_fail(
hass: HomeAssistant,
init_integration,
mock_install,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test prosegur get_image fails."""
mock_install.get_image = AsyncMock(
@ -37,7 +43,9 @@ async def test_camera_fail(hass, init_integration, mock_install, caplog):
assert "Image test_cam doesn't exist" in caplog.text
async def test_request_image(hass, init_integration, mock_install):
async def test_request_image(
hass: HomeAssistant, init_integration, mock_install
) -> None:
"""Test the camera request image service."""
await hass.services.async_call(
@ -50,7 +58,12 @@ async def test_request_image(hass, init_integration, mock_install):
assert mock_install.request_image.called
async def test_request_image_fail(hass, init_integration, mock_install, caplog):
async def test_request_image_fail(
hass: HomeAssistant,
init_integration,
mock_install,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the camera request image service fails."""
mock_install.request_image = AsyncMock(side_effect=ProsegurException())

View File

@ -1,11 +1,18 @@
"""Test Prosegur diagnostics."""
from unittest.mock import patch
from homeassistant.core import HomeAssistant
from tests.components.diagnostics import get_diagnostics_for_config_entry
from tests.typing import ClientSessionGenerator
async def test_diagnostics(hass, hass_client, init_integration, mock_install):
async def test_diagnostics(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
init_integration,
mock_install,
) -> None:
"""Test generating diagnostics for a config entry."""
with patch(

View File

@ -1805,7 +1805,7 @@ def record_states(hass):
return zero, four, states
def test_cache_key_for_generate_statistics_during_period_stmt():
def test_cache_key_for_generate_statistics_during_period_stmt() -> None:
"""Test cache key for _generate_statistics_during_period_stmt."""
columns = select(StatisticsShortTerm.metadata_id, StatisticsShortTerm.start_ts)
stmt = _generate_statistics_during_period_stmt(
@ -1835,7 +1835,7 @@ def test_cache_key_for_generate_statistics_during_period_stmt():
assert cache_key_1 != cache_key_3
def test_cache_key_for_generate_get_metadata_stmt():
def test_cache_key_for_generate_get_metadata_stmt() -> None:
"""Test cache key for _generate_get_metadata_stmt."""
stmt_mean = _generate_get_metadata_stmt([0], "mean")
stmt_mean2 = _generate_get_metadata_stmt([1], "mean")
@ -1846,7 +1846,7 @@ def test_cache_key_for_generate_get_metadata_stmt():
assert stmt_mean._generate_cache_key() != stmt_none._generate_cache_key()
def test_cache_key_for_generate_max_mean_min_statistic_in_sub_period_stmt():
def test_cache_key_for_generate_max_mean_min_statistic_in_sub_period_stmt() -> None:
"""Test cache key for _generate_max_mean_min_statistic_in_sub_period_stmt."""
columns = select(StatisticsShortTerm.metadata_id, StatisticsShortTerm.start_ts)
stmt = _generate_max_mean_min_statistic_in_sub_period_stmt(
@ -1883,7 +1883,7 @@ def test_cache_key_for_generate_max_mean_min_statistic_in_sub_period_stmt():
assert cache_key_1 != cache_key_3
def test_cache_key_for_generate_statistics_at_time_stmt():
def test_cache_key_for_generate_statistics_at_time_stmt() -> None:
"""Test cache key for _generate_statistics_at_time_stmt."""
columns = select(StatisticsShortTerm.metadata_id, StatisticsShortTerm.start_ts)
stmt = _generate_statistics_at_time_stmt(columns, StatisticsShortTerm, {0}, 0.0)

View File

@ -225,7 +225,7 @@ async def test_template_position(hass: HomeAssistant, start_ha) -> None:
},
],
)
async def test_template_not_optimistic(hass, start_ha):
async def test_template_not_optimistic(hass: HomeAssistant, start_ha) -> None:
"""Test the is_closed attribute."""
state = hass.states.get("cover.test_template_cover")
assert state.state == STATE_UNKNOWN

View File

@ -70,7 +70,9 @@ async def test_create_entry(recorder_mock: Recorder, hass: HomeAssistant) -> Non
(FatalHttpException(404), ERR_CLIENT),
],
)
async def test_create_entry_exceptions(recorder_mock, hass, exception, expected_error):
async def test_create_entry_exceptions(
recorder_mock: Recorder, hass: HomeAssistant, exception, expected_error
) -> None:
"""Test create entry from user input."""
test_data = {
CONF_ACCESS_TOKEN: "valid",
@ -93,7 +95,9 @@ async def test_create_entry_exceptions(recorder_mock, hass, exception, expected_
assert result["errors"][CONF_ACCESS_TOKEN] == expected_error
async def test_flow_entry_already_exists(recorder_mock, hass, config_entry):
async def test_flow_entry_already_exists(
recorder_mock: Recorder, hass: HomeAssistant, config_entry
) -> None:
"""Test user input for config_entry that already exists."""
test_data = {
CONF_ACCESS_TOKEN: "valid",

View File

@ -91,7 +91,9 @@ async def test_calendar_entity_unique_id(
@patch("homeassistant.components.todoist.calendar.TodoistAPIAsync")
async def test_update_entity_for_custom_project_with_labels_on(todoist_api, hass, api):
async def test_update_entity_for_custom_project_with_labels_on(
todoist_api, hass: HomeAssistant, api
) -> None:
"""Test that the calendar's state is on for a custom project using labels."""
todoist_api.return_value = api
assert await setup.async_setup_component(

View File

@ -1,5 +1,4 @@
"""Tests for the Twente Milieu sensors."""
import pytest
from syrupy.assertion import SnapshotAssertion

View File

@ -1103,7 +1103,7 @@ async def test_state_template(hass: HomeAssistant) -> None:
assert hass.states.get("media_player.tv").state == STATE_OFF
async def test_browse_media(hass: HomeAssistant):
async def test_browse_media(hass: HomeAssistant) -> None:
"""Test browse media."""
await async_setup_component(
hass, "media_player", {"media_player": {"platform": "demo"}}
@ -1133,7 +1133,7 @@ async def test_browse_media(hass: HomeAssistant):
assert result == MOCK_BROWSE_MEDIA
async def test_browse_media_override(hass: HomeAssistant):
async def test_browse_media_override(hass: HomeAssistant) -> None:
"""Test browse media override."""
await async_setup_component(
hass, "media_player", {"media_player": {"platform": "demo"}}

View File

@ -3,8 +3,12 @@ from homeassistant.components.weather.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from tests.typing import WebSocketGenerator
async def test_device_class_units(hass: HomeAssistant, hass_ws_client) -> None:
async def test_device_class_units(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
) -> None:
"""Test we can get supported units."""
assert await async_setup_component(hass, DOMAIN, {})

View File

@ -306,8 +306,8 @@ async def test_gateway_initialize_failure_transient(
],
)
async def test_gateway_initialize_bellows_thread(
device_path, thread_state, config_override, hass, coordinator
):
device_path, thread_state, config_override, hass: HomeAssistant, coordinator
) -> None:
"""Test ZHA disabling the UART thread when connecting to a TCP coordinator."""
zha_gateway = get_zha_gateway(hass)
assert zha_gateway is not None

View File

@ -323,7 +323,7 @@ def test_weighted_match(
model,
quirk_class,
match_name,
):
) -> None:
"""Test weightedd match."""
s = mock.sentinel
@ -435,7 +435,7 @@ def test_multi_sensor_match(channel, entity_registry: er.EntityRegistry) -> None
}
def test_quirk_classes():
def test_quirk_classes() -> None:
"""Make sure that quirk_classes in components matches are valid."""
def find_quirk_class(base_obj, quirk_mod, quirk_cls):

View File

@ -1460,8 +1460,8 @@ async def test_parse_qr_code_string(
async def test_try_parse_dsk_from_qr_code_string(
hass, integration, client, hass_ws_client
):
hass: HomeAssistant, integration, client, hass_ws_client: WebSocketGenerator
) -> None:
"""Test try_parse_dsk_from_qr_code_string websocket command."""
entry = integration
ws_client = await hass_ws_client(hass)
@ -1524,7 +1524,9 @@ async def test_try_parse_dsk_from_qr_code_string(
assert msg["error"]["code"] == ERR_NOT_LOADED
async def test_supports_feature(hass, integration, client, hass_ws_client):
async def test_supports_feature(
hass: HomeAssistant, integration, client, hass_ws_client: WebSocketGenerator
) -> None:
"""Test supports_feature websocket command."""
entry = integration
ws_client = await hass_ws_client(hass)
@ -3888,8 +3890,8 @@ async def test_subscribe_firmware_update_status_initial_value(
async def test_subscribe_controller_firmware_update_status(
hass, integration, client, hass_ws_client
):
hass: HomeAssistant, integration, client, hass_ws_client: WebSocketGenerator
) -> None:
"""Test the subscribe_firmware_update_status websocket command for a node."""
ws_client = await hass_ws_client(hass)
device = get_device(hass, client.driver.controller.nodes[1])
@ -3954,8 +3956,8 @@ async def test_subscribe_controller_firmware_update_status(
async def test_subscribe_controller_firmware_update_status_initial_value(
hass, client, integration, hass_ws_client
):
hass: HomeAssistant, client, integration, hass_ws_client: WebSocketGenerator
) -> None:
"""Test subscribe_firmware_update_status cmd with in progress update for node."""
ws_client = await hass_ws_client(hass)
device = get_device(hass, client.driver.controller.nodes[1])

View File

@ -103,7 +103,9 @@ async def test_dynamic_climate_data_discovery_template_failure(
)
async def test_merten_507801(hass, client, merten_507801, integration):
async def test_merten_507801(
hass: HomeAssistant, client, merten_507801, integration
) -> None:
"""Test that Merten 507801 multilevel switch value is discovered as a cover."""
node = merten_507801
assert node.device_class.specific.label == "Unused"
@ -116,8 +118,8 @@ async def test_merten_507801(hass, client, merten_507801, integration):
async def test_merten_507801_disabled_enitites(
hass, client, merten_507801, integration
):
hass: HomeAssistant, client, merten_507801, integration
) -> None:
"""Test that Merten 507801 entities created by endpoint 2 are disabled."""
registry = er.async_get(hass)
entity_ids = [

View File

@ -223,7 +223,7 @@ def area_mock(hass):
)
async def test_call_from_config(hass: HomeAssistant):
async def test_call_from_config(hass: HomeAssistant) -> None:
"""Test the sync wrapper of service.async_call_from_config."""
calls = async_mock_service(hass, "test_domain", "test_service")
config = {
@ -238,7 +238,7 @@ async def test_call_from_config(hass: HomeAssistant):
assert calls[0].data == {"hello": "goodbye", "entity_id": ["hello.world"]}
async def test_service_call(hass: HomeAssistant):
async def test_service_call(hass: HomeAssistant) -> None:
"""Test service call with templating."""
calls = async_mock_service(hass, "test_domain", "test_service")
config = {
@ -307,7 +307,7 @@ async def test_service_call(hass: HomeAssistant):
}
async def test_service_template_service_call(hass: HomeAssistant):
async def test_service_template_service_call(hass: HomeAssistant) -> None:
"""Test legacy service_template call with templating."""
calls = async_mock_service(hass, "test_domain", "test_service")
config = {
@ -322,7 +322,7 @@ async def test_service_template_service_call(hass: HomeAssistant):
assert calls[0].data == {"hello": "goodbye", "entity_id": ["hello.world"]}
async def test_passing_variables_to_templates(hass: HomeAssistant):
async def test_passing_variables_to_templates(hass: HomeAssistant) -> None:
"""Test passing variables to templates."""
calls = async_mock_service(hass, "test_domain", "test_service")
config = {
@ -344,7 +344,7 @@ async def test_passing_variables_to_templates(hass: HomeAssistant):
assert calls[0].data == {"hello": "goodbye", "entity_id": ["hello.world"]}
async def test_bad_template(hass: HomeAssistant):
async def test_bad_template(hass: HomeAssistant) -> None:
"""Test passing bad template."""
calls = async_mock_service(hass, "test_domain", "test_service")
config = {
@ -366,7 +366,7 @@ async def test_bad_template(hass: HomeAssistant):
assert len(calls) == 0
async def test_split_entity_string(hass: HomeAssistant):
async def test_split_entity_string(hass: HomeAssistant) -> None:
"""Test splitting of entity string."""
calls = async_mock_service(hass, "test_domain", "test_service")
await service.async_call_from_config(
@ -380,7 +380,7 @@ async def test_split_entity_string(hass: HomeAssistant):
assert ["hello.world", "sensor.beer"] == calls[-1].data.get("entity_id")
async def test_not_mutate_input(hass: HomeAssistant):
async def test_not_mutate_input(hass: HomeAssistant) -> None:
"""Test for immutable input."""
async_mock_service(hass, "test_domain", "test_service")
config = {
@ -403,7 +403,7 @@ async def test_not_mutate_input(hass: HomeAssistant):
@patch("homeassistant.helpers.service._LOGGER.error")
async def test_fail_silently_if_no_service(mock_log, hass: HomeAssistant):
async def test_fail_silently_if_no_service(mock_log, hass: HomeAssistant) -> None:
"""Test failing if service is missing."""
await service.async_call_from_config(hass, None)
assert mock_log.call_count == 1

View File

@ -75,7 +75,7 @@ def test_async_add_hass_job_schedule_callback() -> None:
assert len(hass.add_job.mock_calls) == 0
def test_async_add_hass_job_coro_named(hass) -> None:
def test_async_add_hass_job_coro_named(hass: HomeAssistant) -> None:
"""Test that we schedule coroutines and add jobs to the job pool with a name."""
async def mycoro():

View File

@ -492,7 +492,9 @@ def test_representing_yaml_loaded_data(
@pytest.mark.parametrize("hass_config_yaml", ["key: thing1\nkey: thing2"])
def test_duplicate_key(caplog, try_both_loaders, mock_hass_config_yaml: None) -> None:
def test_duplicate_key(
caplog: pytest.LogCaptureFixture, try_both_loaders, mock_hass_config_yaml: None
) -> None:
"""Test duplicate dict keys."""
load_yaml_config_file(YAML_CONFIG_FILE)
assert "contains duplicate key" in caplog.text
@ -503,7 +505,7 @@ def test_duplicate_key(caplog, try_both_loaders, mock_hass_config_yaml: None) ->
[{YAML_CONFIG_FILE: "key: !secret a", yaml.SECRET_YAML: "a: 1\nb: !secret a"}],
)
def test_no_recursive_secrets(
caplog, try_both_loaders, mock_hass_config_yaml: None
caplog: pytest.LogCaptureFixture, try_both_loaders, mock_hass_config_yaml: None
) -> None:
"""Test that loading of secrets from the secrets file fails correctly."""
with pytest.raises(HomeAssistantError) as e: