@@ -40,6 +40,7 @@ def __init__(self) -> None:
4040 self ._schools : Schools | None = None
4141 self ._loading_task = None
4242 self ._reauth_data : dict | None = None
43+ self ._loading_error : str | None = None
4344
4445 async def _load_schools (self ) -> bool :
4546 """Load schools from cache or fetch new ones from server."""
@@ -63,6 +64,7 @@ async def _load_schools(self) -> bool:
6364 self .__class__ .__name__ ,
6465 __name__ ,
6566 )
67+ self ._loading_error = "cannot_connect"
6668 return False
6769
6870 await schools_storage .async_save (schools_cache .school_list )
@@ -71,17 +73,24 @@ async def _load_schools(self) -> bool:
7173 self .__class__ .__name__ ,
7274 __name__ ,
7375 )
74- else :
75- _LOGGER .info (
76- "[class=%s module=%s] Schools loaded from cache." ,
77- self .__class__ .__name__ ,
78- __name__ ,
79- )
80- return False
76+ return True
77+
78+ _LOGGER .info (
79+ "[class=%s module=%s] Schools loaded from cache." ,
80+ self .__class__ .__name__ ,
81+ __name__ ,
82+ )
83+ return True
8184
8285 async def async_step_user (self , user_input = None ) -> config_entries .ConfigFlowResult :
8386 """Handle the initial step."""
8487
88+ # After progress finishes, HA will re-enter this step; at that point we can
89+ # either continue or show an error with a retry button.
90+ if user_input is not None and user_input .get ("retry" ):
91+ self ._loading_task = None
92+ self ._loading_error = None
93+
8594 if self ._loading_task is None :
8695 self ._loading_task = self .hass .async_create_task (self ._load_schools ())
8796
@@ -91,6 +100,34 @@ async def async_step_user(self, user_input=None) -> config_entries.ConfigFlowRes
91100 progress_task = self ._loading_task ,
92101 )
93102
103+ # Task exists -> either still running or already finished.
104+ if not self ._loading_task .done ():
105+ return self .async_show_progress (
106+ step_id = "user" ,
107+ progress_action = "loading_schools" ,
108+ progress_task = self ._loading_task ,
109+ )
110+
111+ try :
112+ ok = bool (self ._loading_task .result ())
113+ except Exception : # noqa: BLE001 - surface as HA form error
114+ _LOGGER .exception (
115+ "[class=%s module=%s] Unexpected error while loading schools" ,
116+ self .__class__ .__name__ ,
117+ __name__ ,
118+ )
119+ ok = False
120+ self ._loading_error = "unknown"
121+
122+ if not ok :
123+ # Reset task so user can retry.
124+ self ._loading_task = None
125+ return self .async_show_form (
126+ step_id = "user" ,
127+ data_schema = vol .Schema ({vol .Optional ("retry" , default = True ): bool }),
128+ errors = {"base" : self ._loading_error or "cannot_connect" },
129+ )
130+
94131 return self .async_show_progress_done (next_step_id = "complete" )
95132
96133 async def async_step_complete (
0 commit comments