From ba3872ff872a69d4aa53cad80876325d83f1ea89 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sun, 18 Aug 2024 15:42:41 +0200 Subject: [PATCH] Add missing hass type in tests/*.py (#124048) --- tests/common.py | 17 ++++++++++++----- tests/test_bootstrap.py | 24 ++++++++++++------------ tests/test_config.py | 3 ++- tests/test_config_entries.py | 35 ++++++++++++++++++----------------- tests/test_loader.py | 24 ++++++++++++++++++------ tests/test_setup.py | 21 ++++++++++++--------- 6 files changed, 74 insertions(+), 50 deletions(-) diff --git a/tests/common.py b/tests/common.py index 3a4c3f2851c..ecf8c4f5418 100644 --- a/tests/common.py +++ b/tests/common.py @@ -52,7 +52,7 @@ from homeassistant.components import device_automation, persistent_notification from homeassistant.components.device_automation import ( # noqa: F401 _async_get_device_automation_capabilities as async_get_device_automation_capabilities, ) -from homeassistant.config import async_process_component_config +from homeassistant.config import IntegrationConfigInfo, async_process_component_config from homeassistant.config_entries import ConfigEntry, ConfigFlow from homeassistant.const import ( DEVICE_DEFAULT_NAME, @@ -439,14 +439,16 @@ mock_service = threadsafe_callback_factory(async_mock_service) @callback -def async_mock_intent(hass, intent_typ): +def async_mock_intent(hass: HomeAssistant, intent_typ: str) -> list[intent.Intent]: """Set up a fake intent handler.""" - intents = [] + intents: list[intent.Intent] = [] class MockIntentHandler(intent.IntentHandler): intent_type = intent_typ - async def async_handle(self, intent_obj): + async def async_handle( + self, intent_obj: intent.Intent + ) -> intent.IntentResponse: """Handle the intent.""" intents.append(intent_obj) return intent_obj.create_response() @@ -1159,7 +1161,12 @@ def assert_setup_component(count, domain=None): """ config = {} - async def mock_psc(hass, config_input, integration, component=None): + async def mock_psc( + hass: HomeAssistant, + config_input: ConfigType, + integration: loader.Integration, + component: loader.ComponentProtocol | None = None, + ) -> IntegrationConfigInfo: """Mock the prepare_setup_component to capture config.""" domain_input = integration.domain integration_config_info = await async_process_component_config( diff --git a/tests/test_bootstrap.py b/tests/test_bootstrap.py index 278bfc631fd..a32d7d1e50b 100644 --- a/tests/test_bootstrap.py +++ b/tests/test_bootstrap.py @@ -213,7 +213,7 @@ async def test_setup_after_deps_all_present(hass: HomeAssistant) -> None: order = [] def gen_domain_setup(domain): - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: order.append(domain) return True @@ -260,7 +260,7 @@ async def test_setup_after_deps_in_stage_1_ignored(hass: HomeAssistant) -> None: order = [] def gen_domain_setup(domain): - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: order.append(domain) return True @@ -315,7 +315,7 @@ async def test_setup_after_deps_manifests_are_loaded_even_if_not_setup( order = [] def gen_domain_setup(domain): - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: order.append(domain) return True @@ -392,7 +392,7 @@ async def test_setup_frontend_before_recorder(hass: HomeAssistant) -> None: order = [] def gen_domain_setup(domain): - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: order.append(domain) return True @@ -471,7 +471,7 @@ async def test_setup_after_deps_via_platform(hass: HomeAssistant) -> None: after_dep_event = asyncio.Event() def gen_domain_setup(domain): - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: if domain == "after_dep_of_platform_int": await after_dep_event.wait() @@ -520,7 +520,7 @@ async def test_setup_after_deps_not_trigger_load(hass: HomeAssistant) -> None: order = [] def gen_domain_setup(domain): - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: order.append(domain) return True @@ -559,7 +559,7 @@ async def test_setup_after_deps_not_present(hass: HomeAssistant) -> None: order = [] def gen_domain_setup(domain): - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: order.append(domain) return True @@ -969,7 +969,7 @@ async def test_empty_integrations_list_is_only_sent_at_the_end_of_bootstrap( order = [] def gen_domain_setup(domain): - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: order.append(domain) await asyncio.sleep(0.05) @@ -1029,7 +1029,7 @@ async def test_warning_logged_on_wrap_up_timeout( task: asyncio.Task | None = None def gen_domain_setup(domain): - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: nonlocal task async def _not_marked_background_task(): @@ -1067,7 +1067,7 @@ async def test_tasks_logged_that_block_stage_1( """Test we log tasks that delay stage 1 startup.""" def gen_domain_setup(domain): - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def _not_marked_background_task(): await asyncio.sleep(0.2) @@ -1110,7 +1110,7 @@ async def test_tasks_logged_that_block_stage_2( done_future = hass.loop.create_future() def gen_domain_setup(domain): - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def _not_marked_background_task(): await done_future @@ -1452,7 +1452,7 @@ async def test_setup_does_base_platforms_first(hass: HomeAssistant) -> None: order = [] def gen_domain_setup(domain): - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: order.append(domain) return True diff --git a/tests/test_config.py b/tests/test_config.py index c7039cabe8b..02f8e1fc078 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -36,6 +36,7 @@ from homeassistant.core import ( DOMAIN as HOMEASSISTANT_DOMAIN, ConfigSource, HomeAssistant, + State, ) from homeassistant.exceptions import ConfigValidationError, HomeAssistantError from homeassistant.helpers import ( @@ -579,7 +580,7 @@ def test_customize_glob_is_ordered() -> None: assert isinstance(conf["customize_glob"], OrderedDict) -async def _compute_state(hass, config): +async def _compute_state(hass: HomeAssistant, config: dict[str, Any]) -> State | None: await config_util.async_process_ha_core_config(hass, config) entity = Entity() diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index 9983886ce44..dccebff13e5 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -18,6 +18,7 @@ from syrupy.assertion import SnapshotAssertion from homeassistant import config_entries, data_entry_flow, loader from homeassistant.components import dhcp from homeassistant.components.hassio import HassioServiceInfo +from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( EVENT_COMPONENT_LOADED, EVENT_HOMEASSISTANT_STARTED, @@ -104,12 +105,12 @@ async def test_setup_race_only_setup_once(hass: HomeAssistant) -> None: fast_config_entry_setup_future = hass.loop.create_future() slow_setup_future = hass.loop.create_future() - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Mock setup.""" await slow_setup_future return True - async def async_setup_entry(hass, entry): + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock setup entry.""" slow = entry.data["slow"] if slow: @@ -122,7 +123,7 @@ async def test_setup_race_only_setup_once(hass: HomeAssistant) -> None: await fast_config_entry_setup_future return True - async def async_unload_entry(hass, entry): + async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock unload entry.""" return True @@ -582,7 +583,7 @@ async def test_remove_entry_raises( ) -> None: """Test if a component raises while removing entry.""" - async def mock_unload_entry(hass, entry): + async def mock_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock unload entry function.""" raise Exception("BROKEN") # noqa: TRY002 @@ -1326,7 +1327,7 @@ async def test_update_entry_options_and_trigger_listener( entry.add_to_manager(manager) update_listener_calls = [] - async def update_listener(hass, entry): + async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None: """Test function.""" assert entry.options == {"second": True} update_listener_calls.append(None) @@ -1491,7 +1492,7 @@ async def test_reload_during_setup_retrying_waits(hass: HomeAssistant) -> None: load_attempts = [] sleep_duration = 0 - async def _mock_setup_entry(hass, entry): + async def _mock_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock setup entry.""" nonlocal sleep_duration await asyncio.sleep(sleep_duration) @@ -1536,7 +1537,7 @@ async def test_create_entry_options( ) -> None: """Test a config entry being created with options.""" - async def mock_async_setup(hass, config): + async def mock_async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Mock setup.""" hass.async_create_task( hass.config_entries.flow.async_init( @@ -3234,7 +3235,7 @@ async def test_async_setup_init_entry_completes_before_loaded_event_fires( """Test a config entry being initialized during integration setup before the loaded event fires.""" load_events = async_capture_events(hass, EVENT_COMPONENT_LOADED) - async def mock_async_setup(hass, config): + async def mock_async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Mock setup.""" hass.async_create_task( hass.config_entries.flow.async_init( @@ -3292,7 +3293,7 @@ async def test_async_setup_update_entry(hass: HomeAssistant) -> None: entry = MockConfigEntry(domain="comp", data={"value": "initial"}) entry.add_to_hass(hass) - async def mock_async_setup(hass, config): + async def mock_async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Mock setup.""" hass.async_create_task( hass.config_entries.flow.async_init( @@ -3303,7 +3304,7 @@ async def test_async_setup_update_entry(hass: HomeAssistant) -> None: ) return True - async def mock_async_setup_entry(hass, entry): + async def mock_async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock setting up an entry.""" assert entry.data["value"] == "updated" return True @@ -3791,7 +3792,7 @@ async def test_setup_raise_entry_error_from_first_coordinator_update( entry = MockConfigEntry(title="test_title", domain="test") entry.add_to_hass(hass) - async def async_setup_entry(hass, entry): + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock setup entry with a simple coordinator.""" async def _async_update_data(): @@ -3831,7 +3832,7 @@ async def test_setup_not_raise_entry_error_from_future_coordinator_update( entry = MockConfigEntry(title="test_title", domain="test") entry.add_to_hass(hass) - async def async_setup_entry(hass, entry): + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock setup entry with a simple coordinator.""" async def _async_update_data(): @@ -3910,7 +3911,7 @@ async def test_setup_raise_auth_failed_from_first_coordinator_update( entry = MockConfigEntry(title="test_title", domain="test") entry.add_to_hass(hass) - async def async_setup_entry(hass, entry): + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock setup entry with a simple coordinator.""" async def _async_update_data(): @@ -3962,7 +3963,7 @@ async def test_setup_raise_auth_failed_from_future_coordinator_update( entry = MockConfigEntry(title="test_title", domain="test") entry.add_to_hass(hass) - async def async_setup_entry(hass, entry): + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock setup entry with a simple coordinator.""" async def _async_update_data(): @@ -4409,12 +4410,12 @@ async def test_unique_id_update_while_setup_in_progress( ) -> None: """Test we handle the case where the config entry is updated while setup is in progress.""" - async def mock_setup_entry(hass, entry): + async def mock_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock setting up entry.""" await asyncio.sleep(0.1) return True - async def mock_unload_entry(hass, entry): + async def mock_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock unloading an entry.""" return True @@ -5463,7 +5464,7 @@ async def test_reload_during_setup(hass: HomeAssistant) -> None: in_setup = False setup_calls = 0 - async def mock_async_setup_entry(hass, entry): + async def mock_async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock setting up an entry.""" nonlocal in_setup nonlocal setup_calls diff --git a/tests/test_loader.py b/tests/test_loader.py index ae5280b2dcd..01305dde002 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -658,7 +658,9 @@ def _get_test_integration( ) -def _get_test_integration_with_application_credentials(hass, name): +def _get_test_integration_with_application_credentials( + hass: HomeAssistant, name: str +) -> loader.Integration: """Return a generated test integration with application_credentials support.""" return loader.Integration( hass, @@ -678,7 +680,9 @@ def _get_test_integration_with_application_credentials(hass, name): ) -def _get_test_integration_with_zeroconf_matcher(hass, name, config_flow): +def _get_test_integration_with_zeroconf_matcher( + hass: HomeAssistant, name: str, config_flow: bool +) -> loader.Integration: """Return a generated test integration with a zeroconf matcher.""" return loader.Integration( hass, @@ -697,7 +701,9 @@ def _get_test_integration_with_zeroconf_matcher(hass, name, config_flow): ) -def _get_test_integration_with_legacy_zeroconf_matcher(hass, name, config_flow): +def _get_test_integration_with_legacy_zeroconf_matcher( + hass: HomeAssistant, name: str, config_flow: bool +) -> loader.Integration: """Return a generated test integration with a legacy zeroconf matcher.""" return loader.Integration( hass, @@ -724,7 +730,9 @@ def _get_test_integration_with_legacy_zeroconf_matcher(hass, name, config_flow): ) -def _get_test_integration_with_dhcp_matcher(hass, name, config_flow): +def _get_test_integration_with_dhcp_matcher( + hass: HomeAssistant, name: str, config_flow: bool +) -> loader.Integration: """Return a generated test integration with a dhcp matcher.""" return loader.Integration( hass, @@ -748,7 +756,9 @@ def _get_test_integration_with_dhcp_matcher(hass, name, config_flow): ) -def _get_test_integration_with_bluetooth_matcher(hass, name, config_flow): +def _get_test_integration_with_bluetooth_matcher( + hass: HomeAssistant, name: str, config_flow: bool +) -> loader.Integration: """Return a generated test integration with a bluetooth matcher.""" return loader.Integration( hass, @@ -767,7 +777,9 @@ def _get_test_integration_with_bluetooth_matcher(hass, name, config_flow): ) -def _get_test_integration_with_usb_matcher(hass, name, config_flow): +def _get_test_integration_with_usb_matcher( + hass: HomeAssistant, name: str, config_flow: bool +) -> loader.Integration: """Return a generated test integration with a usb matcher.""" return loader.Integration( hass, diff --git a/tests/test_setup.py b/tests/test_setup.py index 4b7df9563ba..c50f8392d66 100644 --- a/tests/test_setup.py +++ b/tests/test_setup.py @@ -9,6 +9,7 @@ import pytest import voluptuous as vol from homeassistant import config_entries, loader, setup +from homeassistant.config_entries import ConfigEntry from homeassistant.const import EVENT_COMPONENT_LOADED, EVENT_HOMEASSISTANT_START from homeassistant.core import ( DOMAIN as HOMEASSISTANT_DOMAIN, @@ -23,6 +24,7 @@ from homeassistant.helpers.dispatcher import ( async_dispatcher_send, ) from homeassistant.helpers.issue_registry import IssueRegistry +from homeassistant.helpers.typing import ConfigType from .common import ( MockConfigEntry, @@ -298,9 +300,10 @@ async def test_component_not_setup_twice_if_loaded_during_other_setup( """Test component setup while waiting for lock is not set up twice.""" result = [] - async def async_setup(hass, config): + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Tracking Setup.""" result.append(1) + return True mock_integration(hass, MockModule("comp", async_setup=async_setup)) @@ -345,7 +348,7 @@ async def test_component_exception_setup(hass: HomeAssistant) -> None: """Test component that raises exception during setup.""" setup.async_set_domains_to_be_loaded(hass, {"comp"}) - def exception_setup(hass, config): + def exception_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Raise exception.""" raise Exception("fail!") # noqa: TRY002 @@ -359,7 +362,7 @@ async def test_component_base_exception_setup(hass: HomeAssistant) -> None: """Test component that raises exception during setup.""" setup.async_set_domains_to_be_loaded(hass, {"comp"}) - def exception_setup(hass, config): + def exception_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Raise exception.""" raise BaseException("fail!") # noqa: TRY002 @@ -377,7 +380,7 @@ async def test_component_setup_with_validation_and_dependency( ) -> None: """Test all config is passed to dependencies.""" - def config_check_setup(hass, config): + def config_check_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Test that config is passed in.""" if config.get("comp_a", {}).get("valid", False): return True @@ -499,7 +502,7 @@ async def test_all_work_done_before_start(hass: HomeAssistant) -> None: """Test all init work done till start.""" call_order = [] - async def component1_setup(hass, config): + async def component1_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up mock component.""" await discovery.async_discover( hass, "test_component2", {}, "test_component2", {} @@ -509,7 +512,7 @@ async def test_all_work_done_before_start(hass: HomeAssistant) -> None: ) return True - def component_track_setup(hass, config): + def component_track_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up mock component.""" call_order.append(1) return True @@ -585,7 +588,7 @@ async def test_when_setup_already_loaded(hass: HomeAssistant) -> None: """Test when setup.""" calls = [] - async def mock_callback(hass, component): + async def mock_callback(hass: HomeAssistant, component: str) -> None: """Mock callback.""" calls.append(component) @@ -613,7 +616,7 @@ async def test_async_when_setup_or_start_already_loaded(hass: HomeAssistant) -> """Test when setup or start.""" calls = [] - async def mock_callback(hass, component): + async def mock_callback(hass: HomeAssistant, component: str) -> None: """Mock callback.""" calls.append(component) @@ -659,7 +662,7 @@ async def test_parallel_entry_setup(hass: HomeAssistant, mock_handlers) -> None: calls = [] - async def mock_async_setup_entry(hass, entry): + async def mock_async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Mock setting up an entry.""" calls.append(entry.data["value"]) await asyncio.sleep(0)