From 47be13e6bfc996cf1af8baad887fcf03605dd20d Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 12 Feb 2026 15:34:31 +0100 Subject: [PATCH] Improve error validation in service tests (#162851) --- tests/components/google_photos/test_services.py | 8 +++++--- tests/components/google_sheets/test_init.py | 9 ++++++--- tests/components/immich/test_services.py | 6 ++++-- tests/components/mastodon/test_services.py | 6 ++++-- tests/components/mealie/test_services.py | 6 ++++-- tests/components/ohme/test_services.py | 3 ++- tests/components/onedrive/test_services.py | 6 ++++-- tests/components/overseerr/test_services.py | 6 ++++-- tests/components/radarr/test_services.py | 6 ++++-- tests/components/risco/test_services.py | 6 ++++-- tests/components/stookwijzer/test_services.py | 6 ++++-- .../swiss_public_transport/test_services.py | 11 ++++------- tests/components/transmission/test_services.py | 3 ++- 13 files changed, 51 insertions(+), 31 deletions(-) diff --git a/tests/components/google_photos/test_services.py b/tests/components/google_photos/test_services.py index 902a24a1f25..25a10c49992 100644 --- a/tests/components/google_photos/test_services.py +++ b/tests/components/google_photos/test_services.py @@ -24,7 +24,7 @@ from homeassistant.components.google_photos.services import ( from homeassistant.config_entries import ConfigEntryState from homeassistant.const import CONF_FILENAME from homeassistant.core import HomeAssistant -from homeassistant.exceptions import HomeAssistantError +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from tests.common import MockConfigEntry @@ -146,7 +146,7 @@ async def test_upload_service_config_entry_not_found( config_entry: MockConfigEntry, ) -> None: """Test upload service call with a config entry that does not exist.""" - with pytest.raises(HomeAssistantError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, UPLOAD_SERVICE, @@ -158,6 +158,7 @@ async def test_upload_service_config_entry_not_found( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_found" @pytest.mark.usefixtures("setup_integration") @@ -171,7 +172,7 @@ async def test_config_entry_not_loaded( assert config_entry.state is ConfigEntryState.NOT_LOADED - with pytest.raises(HomeAssistantError, match="service_config_entry_not_loaded"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, UPLOAD_SERVICE, @@ -183,6 +184,7 @@ async def test_config_entry_not_loaded( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_loaded" @pytest.mark.usefixtures("setup_integration") diff --git a/tests/components/google_sheets/test_init.py b/tests/components/google_sheets/test_init.py index 7c3a260a1a4..afe12688872 100644 --- a/tests/components/google_sheets/test_init.py +++ b/tests/components/google_sheets/test_init.py @@ -368,7 +368,7 @@ async def test_append_sheet_invalid_config_entry( assert config_entry2.state is ConfigEntryState.LOADED # Exercise service call on a config entry that does not exist - with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, "append_sheet", @@ -379,13 +379,14 @@ async def test_append_sheet_invalid_config_entry( }, blocking=True, ) + assert err.value.translation_key == "service_config_entry_not_found" # Unload the config entry invoke the service on the unloaded entry id await hass.config_entries.async_unload(config_entry2.entry_id) await hass.async_block_till_done() assert config_entry2.state is ConfigEntryState.NOT_LOADED - with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, "append_sheet", @@ -396,6 +397,7 @@ async def test_append_sheet_invalid_config_entry( }, blocking=True, ) + assert err.value.translation_key == "service_config_entry_not_loaded" async def test_get_sheet_invalid_config_entry( @@ -427,7 +429,7 @@ async def test_get_sheet_invalid_config_entry( assert config_entry2.state is ConfigEntryState.LOADED # Exercise service call on a config entry that does not exist - with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_GET_SHEET, @@ -439,6 +441,7 @@ async def test_get_sheet_invalid_config_entry( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_found" # Unload the config entry invoke the service on the unloaded entry id await hass.config_entries.async_unload(config_entry2.entry_id) diff --git a/tests/components/immich/test_services.py b/tests/components/immich/test_services.py index 4aa8b35baae..d907061dd99 100644 --- a/tests/components/immich/test_services.py +++ b/tests/components/immich/test_services.py @@ -96,7 +96,7 @@ async def test_upload_file_config_entry_not_found( """Test upload_file service raising config_entry_not_found.""" await setup_integration(hass, mock_config_entry) - with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_UPLOAD_FILE, @@ -109,6 +109,7 @@ async def test_upload_file_config_entry_not_found( }, blocking=True, ) + assert err.value.translation_key == "service_config_entry_not_found" async def test_upload_file_config_entry_not_loaded( @@ -120,7 +121,7 @@ async def test_upload_file_config_entry_not_loaded( mock_config_entry.disabled_by = er.RegistryEntryDisabler.USER await setup_integration(hass, mock_config_entry) - with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_UPLOAD_FILE, @@ -133,6 +134,7 @@ async def test_upload_file_config_entry_not_loaded( }, blocking=True, ) + assert err.value.translation_key == "service_config_entry_not_loaded" async def test_upload_file_only_local_media_supported( diff --git a/tests/components/mastodon/test_services.py b/tests/components/mastodon/test_services.py index 02cc6fdc5cb..bdd798376f5 100644 --- a/tests/components/mastodon/test_services.py +++ b/tests/components/mastodon/test_services.py @@ -311,7 +311,7 @@ async def test_service_entry_availability( payload = {"status": "test toot"} - with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_POST, @@ -319,8 +319,9 @@ async def test_service_entry_availability( blocking=True, return_response=False, ) + assert err.value.translation_key == "service_config_entry_not_loaded" - with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_POST, @@ -328,3 +329,4 @@ async def test_service_entry_availability( blocking=True, return_response=False, ) + assert err.value.translation_key == "service_config_entry_not_found" diff --git a/tests/components/mealie/test_services.py b/tests/components/mealie/test_services.py index f4c96f82f44..0c31d783cee 100644 --- a/tests/components/mealie/test_services.py +++ b/tests/components/mealie/test_services.py @@ -542,7 +542,7 @@ async def test_service_entry_availability( await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, service, @@ -550,8 +550,9 @@ async def test_service_entry_availability( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_loaded" - with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, service, @@ -559,3 +560,4 @@ async def test_service_entry_availability( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_found" diff --git a/tests/components/ohme/test_services.py b/tests/components/ohme/test_services.py index 466d88ffd93..cddc1f0d06e 100644 --- a/tests/components/ohme/test_services.py +++ b/tests/components/ohme/test_services.py @@ -85,7 +85,7 @@ async def test_list_charge_slots_exception( await setup_integration(hass, mock_config_entry) # Test error - with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_LIST_CHARGE_SLOTS, @@ -93,3 +93,4 @@ async def test_list_charge_slots_exception( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_found" diff --git a/tests/components/onedrive/test_services.py b/tests/components/onedrive/test_services.py index 5c7674026b1..df50a32b687 100644 --- a/tests/components/onedrive/test_services.py +++ b/tests/components/onedrive/test_services.py @@ -123,7 +123,7 @@ async def test_upload_service_config_entry_not_found( ) -> None: """Test upload service call with a config entry that does not exist.""" await setup_integration(hass, mock_config_entry) - with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, UPLOAD_SERVICE, @@ -135,6 +135,7 @@ async def test_upload_service_config_entry_not_found( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_found" async def test_config_entry_not_loaded( @@ -148,7 +149,7 @@ async def test_config_entry_not_loaded( assert mock_config_entry.state is ConfigEntryState.NOT_LOADED - with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, UPLOAD_SERVICE, @@ -160,6 +161,7 @@ async def test_config_entry_not_loaded( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_loaded" @pytest.mark.parametrize("upload_file", [MockUploadFile(is_allowed_path=False)]) diff --git a/tests/components/overseerr/test_services.py b/tests/components/overseerr/test_services.py index d719f768cbc..4ceee0196d5 100644 --- a/tests/components/overseerr/test_services.py +++ b/tests/components/overseerr/test_services.py @@ -135,7 +135,7 @@ async def test_service_entry_availability( await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, service, @@ -143,8 +143,9 @@ async def test_service_entry_availability( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_loaded" - with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, service, @@ -152,3 +153,4 @@ async def test_service_entry_availability( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_found" diff --git a/tests/components/radarr/test_services.py b/tests/components/radarr/test_services.py index 92dbb7916a8..3ba2783dafb 100644 --- a/tests/components/radarr/test_services.py +++ b/tests/components/radarr/test_services.py @@ -137,7 +137,7 @@ async def test_services_invalid_entry( # Set up at least one entry so the service gets registered await setup_integration(hass, aioclient_mock) - with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, service, @@ -145,6 +145,7 @@ async def test_services_invalid_entry( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_found" @pytest.mark.parametrize( @@ -163,7 +164,7 @@ async def test_services_entry_not_loaded( # Now create a second entry that isn't loaded unloaded_entry = create_entry(hass) - with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, service, @@ -171,3 +172,4 @@ async def test_services_entry_not_loaded( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_loaded" diff --git a/tests/components/risco/test_services.py b/tests/components/risco/test_services.py index f978d8cb943..e6681a34c29 100644 --- a/tests/components/risco/test_services.py +++ b/tests/components/risco/test_services.py @@ -61,10 +61,11 @@ async def test_set_time_service_with_invalid_entry( ATTR_CONFIG_ENTRY_ID: "invalid_entry_id", } - with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True ) + assert err.value.translation_key == "service_config_entry_not_found" async def test_set_time_service_with_not_loaded_entry( @@ -80,10 +81,11 @@ async def test_set_time_service_with_not_loaded_entry( ATTR_CONFIG_ENTRY_ID: local_config_entry.entry_id, } - with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True ) + assert err.value.translation_key == "service_config_entry_not_loaded" async def test_set_time_service_with_cloud_entry( diff --git a/tests/components/stookwijzer/test_services.py b/tests/components/stookwijzer/test_services.py index 251037ceef6..8310ac78493 100644 --- a/tests/components/stookwijzer/test_services.py +++ b/tests/components/stookwijzer/test_services.py @@ -40,7 +40,7 @@ async def test_service_entry_not_loaded( mock_config_entry2 = MockConfigEntry(domain=DOMAIN) mock_config_entry2.add_to_hass(hass) - with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_GET_FORECAST, @@ -48,6 +48,7 @@ async def test_service_entry_not_loaded( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_loaded" @pytest.mark.usefixtures("init_integration") @@ -57,7 +58,7 @@ async def test_service_integration_not_found( mock_config_entry: MockConfigEntry, ) -> None: """Test error handling when integration not in registry.""" - with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_GET_FORECAST, @@ -65,3 +66,4 @@ async def test_service_integration_not_found( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_found" diff --git a/tests/components/swiss_public_transport/test_services.py b/tests/components/swiss_public_transport/test_services.py index 779ca183938..d4a7104041b 100644 --- a/tests/components/swiss_public_transport/test_services.py +++ b/tests/components/swiss_public_transport/test_services.py @@ -200,9 +200,7 @@ async def test_service_call_load_unload( await hass.config_entries.async_unload(config_entry.entry_id) await hass.async_block_till_done() - with pytest.raises( - ServiceValidationError, match="service_config_entry_not_loaded" - ): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( domain=DOMAIN, service=SERVICE_FETCH_CONNECTIONS, @@ -212,11 +210,9 @@ async def test_service_call_load_unload( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_loaded" - with pytest.raises( - ServiceValidationError, - match="service_config_entry_not_found", - ): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( domain=DOMAIN, service=SERVICE_FETCH_CONNECTIONS, @@ -226,3 +222,4 @@ async def test_service_call_load_unload( blocking=True, return_response=True, ) + assert err.value.translation_key == "service_config_entry_not_found" diff --git a/tests/components/transmission/test_services.py b/tests/components/transmission/test_services.py index e426aa792f1..7d7683fbeec 100644 --- a/tests/components/transmission/test_services.py +++ b/tests/components/transmission/test_services.py @@ -59,7 +59,7 @@ async def test_service_integration_not_found( await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() - with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): + with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, SERVICE_ADD_TORRENT, @@ -69,6 +69,7 @@ async def test_service_integration_not_found( }, blocking=True, ) + assert err.value.translation_key == "service_config_entry_not_found" @pytest.mark.parametrize(