@@ -1137,3 +1137,116 @@ def test_compute_code_challenge_invalid_code_verifier(code_verifier: str) -> Non
11371137 config_entry_oauth2_flow .LocalOAuth2ImplementationWithPkce .compute_code_challenge (
11381138 code_verifier
11391139 )
1140+
1141+
1142+ async def test_async_get_config_entry_implementation_with_failing_provider_and_succeeding_provider (
1143+ hass : HomeAssistant ,
1144+ local_impl : config_entry_oauth2_flow .LocalOAuth2Implementation ,
1145+ ) -> None :
1146+ """Test async_get_config_entry_implementation when one provider fails but another succeeds."""
1147+
1148+ async def failing_cloud_provider (
1149+ _hass : HomeAssistant , _domain : str
1150+ ) -> list [config_entry_oauth2_flow .AbstractOAuth2Implementation ]:
1151+ """Provider that raises an exception."""
1152+ raise config_entry_oauth2_flow .ImplementationUnavailableError
1153+
1154+ async def successful_local_provider (
1155+ _hass : HomeAssistant , _domain : str
1156+ ) -> list [config_entry_oauth2_flow .AbstractOAuth2Implementation ]:
1157+ """Provider that returns implementations."""
1158+ return [local_impl ]
1159+
1160+ config_entry_oauth2_flow .async_add_implementation_provider (
1161+ hass , "cloud" , failing_cloud_provider
1162+ )
1163+ config_entry_oauth2_flow .async_add_implementation_provider (
1164+ hass , "application_credentials" , successful_local_provider
1165+ )
1166+
1167+ config_entry = MockConfigEntry (
1168+ domain = TEST_DOMAIN ,
1169+ data = {
1170+ "auth_implementation" : local_impl .domain ,
1171+ },
1172+ )
1173+
1174+ # This should succeed and return the local implementation
1175+ # even though the failing cloud provider raised an exception.
1176+ implementation = (
1177+ await config_entry_oauth2_flow .async_get_config_entry_implementation (
1178+ hass , config_entry
1179+ )
1180+ )
1181+ assert implementation is local_impl
1182+
1183+
1184+ async def test_async_get_config_entry_implementation_with_failing_provider (
1185+ hass : HomeAssistant ,
1186+ ) -> None :
1187+ """Test async_get_config_entry_implementation when one provider fails and the other is empty."""
1188+
1189+ async def failing_cloud_provider (
1190+ _hass : HomeAssistant , _domain : str
1191+ ) -> list [config_entry_oauth2_flow .AbstractOAuth2Implementation ]:
1192+ """Provider that raises an exception."""
1193+ raise config_entry_oauth2_flow .ImplementationUnavailableError
1194+
1195+ async def empty_local_provider (
1196+ _hass : HomeAssistant , _domain : str
1197+ ) -> list [config_entry_oauth2_flow .AbstractOAuth2Implementation ]:
1198+ """Provider that returns implementations."""
1199+ return []
1200+
1201+ config_entry_oauth2_flow .async_add_implementation_provider (
1202+ hass , "cloud" , failing_cloud_provider
1203+ )
1204+ config_entry_oauth2_flow .async_add_implementation_provider (
1205+ hass , "application_credentials" , empty_local_provider
1206+ )
1207+
1208+ config_entry = MockConfigEntry (
1209+ domain = TEST_DOMAIN ,
1210+ data = {
1211+ "auth_implementation" : TEST_DOMAIN ,
1212+ },
1213+ )
1214+
1215+ # This should fail since the local provider returned an empty list
1216+ # and the cloud provider raised an exception.
1217+ with pytest .raises (config_entry_oauth2_flow .ImplementationUnavailableError ):
1218+ await config_entry_oauth2_flow .async_get_config_entry_implementation (
1219+ hass , config_entry
1220+ )
1221+
1222+
1223+ async def test_async_get_config_entry_implementation_missing_provider (
1224+ hass : HomeAssistant ,
1225+ ) -> None :
1226+ """Test async_get_config_entry_implementation when both providers are empty."""
1227+
1228+ async def empty_provider (
1229+ _hass : HomeAssistant , _domain : str
1230+ ) -> list [config_entry_oauth2_flow .AbstractOAuth2Implementation ]:
1231+ """Provider that returns implementations."""
1232+ return []
1233+
1234+ config_entry_oauth2_flow .async_add_implementation_provider (
1235+ hass , "cloud" , empty_provider
1236+ )
1237+ config_entry_oauth2_flow .async_add_implementation_provider (
1238+ hass , "application_credentials" , empty_provider
1239+ )
1240+
1241+ config_entry = MockConfigEntry (
1242+ domain = TEST_DOMAIN ,
1243+ data = {
1244+ "auth_implementation" : TEST_DOMAIN ,
1245+ },
1246+ )
1247+
1248+ # This should fail since both providers are empty.
1249+ with pytest .raises (ValueError , match = "Implementation not available" ):
1250+ await config_entry_oauth2_flow .async_get_config_entry_implementation (
1251+ hass , config_entry
1252+ )
0 commit comments