Add typing to tests with single hass argument (2) (#87675)

* Add typing to tests with single hass argument (2)

* a few more
This commit is contained in:
epenet 2023-02-08 08:51:43 +01:00 committed by GitHub
parent 1bbc03d0ba
commit c98b4e3204
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
50 changed files with 816 additions and 671 deletions

View file

@ -40,7 +40,9 @@ DOMAIN = "test_domain"
PLATFORM = "test_platform"
async def test_polling_only_updates_entities_it_should_poll(hass):
async def test_polling_only_updates_entities_it_should_poll(
hass: HomeAssistant,
) -> None:
"""Test the polling of only updated entities."""
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))
@ -61,7 +63,7 @@ async def test_polling_only_updates_entities_it_should_poll(hass):
assert poll_ent.async_update.called
async def test_polling_disabled_by_config_entry(hass):
async def test_polling_disabled_by_config_entry(hass: HomeAssistant) -> None:
"""Test the polling of only updated entities."""
entity_platform = MockEntityPlatform(hass)
entity_platform.config_entry = MockConfigEntry(pref_disable_polling=True)
@ -72,7 +74,7 @@ async def test_polling_disabled_by_config_entry(hass):
assert entity_platform._async_unsub_polling is None
async def test_polling_updates_entities_with_exception(hass):
async def test_polling_updates_entities_with_exception(hass: HomeAssistant) -> None:
"""Test the updated entities that not break with an exception."""
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))
@ -109,7 +111,7 @@ async def test_polling_updates_entities_with_exception(hass):
assert len(update_err) == 1
async def test_update_state_adds_entities(hass):
async def test_update_state_adds_entities(hass: HomeAssistant) -> None:
"""Test if updating poll entities cause an entity to be added works."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
@ -126,7 +128,9 @@ async def test_update_state_adds_entities(hass):
assert len(hass.states.async_entity_ids()) == 2
async def test_update_state_adds_entities_with_update_before_add_true(hass):
async def test_update_state_adds_entities_with_update_before_add_true(
hass: HomeAssistant,
) -> None:
"""Test if call update before add to state machine."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
@ -140,7 +144,9 @@ async def test_update_state_adds_entities_with_update_before_add_true(hass):
assert ent.update.called
async def test_update_state_adds_entities_with_update_before_add_false(hass):
async def test_update_state_adds_entities_with_update_before_add_false(
hass: HomeAssistant,
) -> None:
"""Test if not call update before add to state machine."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
@ -176,7 +182,9 @@ async def test_set_scan_interval_via_platform(mock_track, hass):
assert timedelta(seconds=30) == mock_track.call_args[0][2]
async def test_adding_entities_with_generator_and_thread_callback(hass):
async def test_adding_entities_with_generator_and_thread_callback(
hass: HomeAssistant,
) -> None:
"""Test generator in add_entities that calls thread method.
We should make sure we resolve the generator to a list before passing
@ -193,7 +201,7 @@ async def test_adding_entities_with_generator_and_thread_callback(hass):
await component.async_add_entities(create_entity(i) for i in range(2))
async def test_platform_warn_slow_setup(hass):
async def test_platform_warn_slow_setup(hass: HomeAssistant) -> None:
"""Warn we log when platform setup takes a long time."""
platform = MockPlatform()
@ -235,7 +243,7 @@ async def test_platform_error_slow_setup(hass, caplog):
assert "test_platform is taking longer than 0 seconds" in caplog.text
async def test_updated_state_used_for_entity_id(hass):
async def test_updated_state_used_for_entity_id(hass: HomeAssistant) -> None:
"""Test that first update results used for entity ID generation."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
@ -253,7 +261,7 @@ async def test_updated_state_used_for_entity_id(hass):
assert entity_ids[0] == "test_domain.living_room"
async def test_parallel_updates_async_platform(hass):
async def test_parallel_updates_async_platform(hass: HomeAssistant) -> None:
"""Test async platform does not have parallel_updates limit by default."""
platform = MockPlatform()
@ -279,7 +287,9 @@ async def test_parallel_updates_async_platform(hass):
assert entity.parallel_updates is None
async def test_parallel_updates_async_platform_with_constant(hass):
async def test_parallel_updates_async_platform_with_constant(
hass: HomeAssistant,
) -> None:
"""Test async platform can set parallel_updates limit."""
platform = MockPlatform()
platform.PARALLEL_UPDATES = 2
@ -306,7 +316,7 @@ async def test_parallel_updates_async_platform_with_constant(hass):
assert entity.parallel_updates._value == 2
async def test_parallel_updates_sync_platform(hass):
async def test_parallel_updates_sync_platform(hass: HomeAssistant) -> None:
"""Test sync platform parallel_updates default set to 1."""
platform = MockPlatform()
@ -332,7 +342,7 @@ async def test_parallel_updates_sync_platform(hass):
assert entity.parallel_updates._value == 1
async def test_parallel_updates_no_update_method(hass):
async def test_parallel_updates_no_update_method(hass: HomeAssistant) -> None:
"""Test platform parallel_updates default set to 0."""
platform = MockPlatform()
@ -351,7 +361,9 @@ async def test_parallel_updates_no_update_method(hass):
assert entity.parallel_updates is None
async def test_parallel_updates_sync_platform_with_constant(hass):
async def test_parallel_updates_sync_platform_with_constant(
hass: HomeAssistant,
) -> None:
"""Test sync platform can set parallel_updates limit."""
platform = MockPlatform()
platform.PARALLEL_UPDATES = 2
@ -378,7 +390,7 @@ async def test_parallel_updates_sync_platform_with_constant(hass):
assert entity.parallel_updates._value == 2
async def test_raise_error_on_update(hass):
async def test_raise_error_on_update(hass: HomeAssistant) -> None:
"""Test the add entity if they raise an error on update."""
updates = []
component = EntityComponent(_LOGGER, DOMAIN, hass)
@ -403,7 +415,7 @@ async def test_raise_error_on_update(hass):
assert entity2.platform is not None
async def test_async_remove_with_platform(hass):
async def test_async_remove_with_platform(hass: HomeAssistant) -> None:
"""Remove an entity from a platform."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
entity1 = MockEntity(name="test_1")
@ -413,7 +425,7 @@ async def test_async_remove_with_platform(hass):
assert len(hass.states.async_entity_ids()) == 0
async def test_async_remove_with_platform_update_finishes(hass):
async def test_async_remove_with_platform_update_finishes(hass: HomeAssistant) -> None:
"""Remove an entity when an update finishes after its been removed."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
entity1 = MockEntity(name="test_1")
@ -474,7 +486,7 @@ async def test_not_adding_duplicate_entities_with_unique_id(hass, caplog):
assert entry.original_name == "test1"
async def test_using_prescribed_entity_id(hass):
async def test_using_prescribed_entity_id(hass: HomeAssistant) -> None:
"""Test for using predefined entity ID."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
await component.async_add_entities(
@ -483,7 +495,7 @@ async def test_using_prescribed_entity_id(hass):
assert "hello.world" in hass.states.async_entity_ids()
async def test_using_prescribed_entity_id_with_unique_id(hass):
async def test_using_prescribed_entity_id_with_unique_id(hass: HomeAssistant) -> None:
"""Test for amending predefined entity ID because currently exists."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
@ -495,7 +507,9 @@ async def test_using_prescribed_entity_id_with_unique_id(hass):
assert "test_domain.world_2" in hass.states.async_entity_ids()
async def test_using_prescribed_entity_id_which_is_registered(hass):
async def test_using_prescribed_entity_id_which_is_registered(
hass: HomeAssistant,
) -> None:
"""Test not allowing predefined entity ID that already registered."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
registry = mock_registry(hass)
@ -508,7 +522,7 @@ async def test_using_prescribed_entity_id_which_is_registered(hass):
assert "test_domain.world_2" in hass.states.async_entity_ids()
async def test_name_which_conflict_with_registered(hass):
async def test_name_which_conflict_with_registered(hass: HomeAssistant) -> None:
"""Test not generating conflicting entity ID based on name."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
registry = mock_registry(hass)
@ -521,7 +535,9 @@ async def test_name_which_conflict_with_registered(hass):
assert "test_domain.world_2" in hass.states.async_entity_ids()
async def test_entity_with_name_and_entity_id_getting_registered(hass):
async def test_entity_with_name_and_entity_id_getting_registered(
hass: HomeAssistant,
) -> None:
"""Ensure that entity ID is used for registration."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
await component.async_add_entities(
@ -530,7 +546,7 @@ async def test_entity_with_name_and_entity_id_getting_registered(hass):
assert "test_domain.world" in hass.states.async_entity_ids()
async def test_overriding_name_from_registry(hass):
async def test_overriding_name_from_registry(hass: HomeAssistant) -> None:
"""Test that we can override a name via the Entity Registry."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
mock_registry(
@ -554,7 +570,7 @@ async def test_overriding_name_from_registry(hass):
assert state.name == "Overridden"
async def test_registry_respect_entity_namespace(hass):
async def test_registry_respect_entity_namespace(hass: HomeAssistant) -> None:
"""Test that the registry respects entity namespace."""
mock_registry(hass)
platform = MockEntityPlatform(hass, entity_namespace="ns")
@ -563,7 +579,7 @@ async def test_registry_respect_entity_namespace(hass):
assert entity.entity_id == "test_domain.ns_device_name"
async def test_registry_respect_entity_disabled(hass):
async def test_registry_respect_entity_disabled(hass: HomeAssistant) -> None:
"""Test that the registry respects entity disabled."""
mock_registry(
hass,
@ -606,7 +622,7 @@ async def test_unique_id_conflict_has_priority_over_disabled_entity(hass, caplog
assert entry.original_name == "test1"
async def test_entity_registry_updates_name(hass):
async def test_entity_registry_updates_name(hass: HomeAssistant) -> None:
"""Test that updates on the entity registry update platform entities."""
registry = mock_registry(
hass,
@ -636,7 +652,7 @@ async def test_entity_registry_updates_name(hass):
assert state.name == "after update"
async def test_setup_entry(hass):
async def test_setup_entry(hass: HomeAssistant) -> None:
"""Test we can setup an entry."""
registry = mock_registry(hass)
@ -725,7 +741,7 @@ async def test_setup_entry_platform_not_ready_from_exception(hass, caplog):
assert len(mock_call_later.mock_calls) == 1
async def test_reset_cancels_retry_setup(hass):
async def test_reset_cancels_retry_setup(hass: HomeAssistant) -> None:
"""Test that resetting a platform will cancel scheduled a setup retry."""
async_setup_entry = Mock(side_effect=PlatformNotReady)
platform = MockPlatform(async_setup_entry=async_setup_entry)
@ -747,7 +763,7 @@ async def test_reset_cancels_retry_setup(hass):
assert ent_platform._async_cancel_retry_setup is None
async def test_reset_cancels_retry_setup_when_not_started(hass):
async def test_reset_cancels_retry_setup_when_not_started(hass: HomeAssistant) -> None:
"""Test that resetting a platform will cancel scheduled a setup retry when not yet started."""
hass.state = CoreState.starting
async_setup_entry = Mock(side_effect=PlatformNotReady)
@ -772,7 +788,9 @@ async def test_reset_cancels_retry_setup_when_not_started(hass):
assert ent_platform._async_cancel_retry_setup is None
async def test_stop_shutdown_cancels_retry_setup_and_interval_listener(hass):
async def test_stop_shutdown_cancels_retry_setup_and_interval_listener(
hass: HomeAssistant,
) -> None:
"""Test that shutdown will cancel scheduled a setup retry and interval listener."""
async_setup_entry = Mock(side_effect=PlatformNotReady)
platform = MockPlatform(async_setup_entry=async_setup_entry)
@ -795,7 +813,7 @@ async def test_stop_shutdown_cancels_retry_setup_and_interval_listener(hass):
assert ent_platform._async_cancel_retry_setup is None
async def test_not_fails_with_adding_empty_entities_(hass):
async def test_not_fails_with_adding_empty_entities_(hass: HomeAssistant) -> None:
"""Test for not fails on empty entities list."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
@ -804,7 +822,7 @@ async def test_not_fails_with_adding_empty_entities_(hass):
assert len(hass.states.async_entity_ids()) == 0
async def test_entity_registry_updates_entity_id(hass):
async def test_entity_registry_updates_entity_id(hass: HomeAssistant) -> None:
"""Test that updates on the entity registry update platform entities."""
registry = mock_registry(
hass,
@ -836,7 +854,7 @@ async def test_entity_registry_updates_entity_id(hass):
assert hass.states.get("test_domain.planet") is not None
async def test_entity_registry_updates_invalid_entity_id(hass):
async def test_entity_registry_updates_invalid_entity_id(hass: HomeAssistant) -> None:
"""Test that we can't update to an invalid entity id."""
registry = mock_registry(
hass,
@ -886,7 +904,7 @@ async def test_entity_registry_updates_invalid_entity_id(hass):
assert hass.states.get("diff_domain.world") is None
async def test_device_info_called(hass):
async def test_device_info_called(hass: HomeAssistant) -> None:
"""Test device info is forwarded correctly."""
registry = dr.async_get(hass)
via = registry.async_get_or_create(
@ -950,7 +968,7 @@ async def test_device_info_called(hass):
assert device.via_device_id == via.id
async def test_device_info_not_overrides(hass):
async def test_device_info_not_overrides(hass: HomeAssistant) -> None:
"""Test device info is forwarded correctly."""
registry = dr.async_get(hass)
device = registry.async_get_or_create(
@ -1134,7 +1152,7 @@ async def test_device_info_change_to_no_url(hass, caplog):
assert device.configuration_url is None
async def test_entity_disabled_by_integration(hass):
async def test_entity_disabled_by_integration(hass: HomeAssistant) -> None:
"""Test entity disabled by integration."""
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))
@ -1196,7 +1214,7 @@ async def test_entity_disabled_by_device(hass: HomeAssistant):
assert entry_disabled.disabled_by is er.RegistryEntryDisabler.DEVICE
async def test_entity_hidden_by_integration(hass):
async def test_entity_hidden_by_integration(hass: HomeAssistant) -> None:
"""Test entity hidden by integration."""
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))
@ -1215,7 +1233,7 @@ async def test_entity_hidden_by_integration(hass):
assert entry_hidden.hidden_by is er.RegistryEntryHider.INTEGRATION
async def test_entity_info_added_to_entity_registry(hass):
async def test_entity_info_added_to_entity_registry(hass: HomeAssistant) -> None:
"""Test entity info is written to entity registry."""
component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))
@ -1257,7 +1275,7 @@ async def test_entity_info_added_to_entity_registry(hass):
)
async def test_override_restored_entities(hass):
async def test_override_restored_entities(hass: HomeAssistant) -> None:
"""Test that we allow overriding restored entities."""
registry = mock_registry(hass)
registry.async_get_or_create(
@ -1290,7 +1308,7 @@ async def test_platform_with_no_setup(hass, caplog):
)
async def test_platforms_sharing_services(hass):
async def test_platforms_sharing_services(hass: HomeAssistant) -> None:
"""Test platforms share services."""
entity_platform1 = MockEntityPlatform(
hass, domain="mock_integration", platform_name="mock_platform", platform=None
@ -1333,7 +1351,7 @@ async def test_platforms_sharing_services(hass):
assert entity2 in entities
async def test_invalid_entity_id(hass):
async def test_invalid_entity_id(hass: HomeAssistant) -> None:
"""Test specifying an invalid entity id."""
platform = MockEntityPlatform(hass)
entity = MockEntity(entity_id="invalid_entity_id")
@ -1381,7 +1399,7 @@ async def test_setup_entry_with_entities_that_block_forever(hass, caplog):
assert "test" in caplog.text
async def test_two_platforms_add_same_entity(hass):
async def test_two_platforms_add_same_entity(hass: HomeAssistant) -> None:
"""Test two platforms in the same domain adding an entity with the same name."""
entity_platform1 = MockEntityPlatform(
hass, domain="mock_integration", platform_name="mock_platform", platform=None