@@ -238,7 +238,7 @@ async def _async_check_reachable(self, host: str, connection_type: str) -> bool:
238238 writer .close ()
239239 await writer .wait_closed ()
240240 return True
241- except ( TimeoutError , socket .gaierror , ConnectionRefusedError , OSError ) :
241+ except TimeoutError , socket .gaierror , ConnectionRefusedError , OSError :
242242 continue
243243
244244 return False
@@ -602,14 +602,24 @@ async def _test_connection(self, data: dict[str, Any]) -> str | None:
602602 _LOGGER .warning ("API error during connection test: %s" , err )
603603 return "cannot_connect"
604604 except Exception as err : # noqa: BLE001
605- _LOGGER .exception ("Unexpected error during connection test for %s: %s" , data .get (CONF_USERNAME ), err )
605+ _LOGGER .exception (
606+ "Unexpected error during connection test for %s: %s" ,
607+ data .get (CONF_USERNAME ),
608+ err ,
609+ )
606610 return "unknown"
607611
608612 async def async_step_provision_user (
609- self , user_input : dict [str , Any ] | None = None , errors : dict [str , str ] | None = None
613+ self ,
614+ user_input : dict [str , Any ] | None = None ,
615+ errors : dict [str , str ] | None = None ,
610616 ) -> ConfigFlowResult :
611617 """Step to ask if user wants to provision a dedicated user."""
612- _LOGGER .info ("Entering async_step_provision_user: input=%s, errors=%s" , user_input , errors )
618+ _LOGGER .info (
619+ "Entering async_step_provision_user: input=%s, errors=%s" ,
620+ user_input ,
621+ errors ,
622+ )
613623 if user_input is not None :
614624 mode = user_input .get ("mode" )
615625 if mode == "create" or mode == "reset" :
@@ -681,25 +691,38 @@ async def async_step_do_provision(self) -> ConfigFlowResult:
681691 await client .connect ()
682692 # The provisioning script will restart services in background
683693 # it's possible the connection drops exactly when/after sending SUCCESS
684- success = await client .provision_user ("homeassistant" , self ._generated_password )
694+ success = await client .provision_user (
695+ "homeassistant" , self ._generated_password
696+ )
685697 if not success :
686698 self ._provision_error = "Provisioning script returned failure. Check router logs (logread)."
687699 await client .disconnect ()
688700 except TimeoutError :
689- _LOGGER .warning ("Provisioning timed out for %s. It might have succeeded if services are restarting." , self ._data .get (CONF_HOST ))
701+ _LOGGER .warning (
702+ "Provisioning timed out for %s. It might have succeeded if services are restarting." ,
703+ self ._data .get (CONF_HOST ),
704+ )
690705 # We don't mark as success here, but if the script worked,
691706 # the next step (testing new user) might still work
692707 self ._provision_error = "Timeout during provisioning. The router might be slow or restarting services."
693708 except Exception as err :
694709 err_msg = str (err ).lower ()
695710 # If we get a connection drop, it's highly likely service restarts triggered it
696- if any (m in err_msg for m in ["connection reset" , "broken pipe" , "closed" , "eof" ]):
697- _LOGGER .info ("Connection dropped during provisioning for %s - this is expected during service restarts." , self ._data .get (CONF_HOST ))
711+ if any (
712+ m in err_msg
713+ for m in ["connection reset" , "broken pipe" , "closed" , "eof" ]
714+ ):
715+ _LOGGER .info (
716+ "Connection dropped during provisioning for %s - this is expected during service restarts." ,
717+ self ._data .get (CONF_HOST ),
718+ )
698719 # We assume success if the command was at least sent and no explicit error returned
699720 # The next step 'display_new_user' does a thorough re-connect test
700721 success = True
701722 else :
702- _LOGGER .error ("Provisioning failed for %s: %s" , self ._data .get (CONF_HOST ), err )
723+ _LOGGER .error (
724+ "Provisioning failed for %s: %s" , self ._data .get (CONF_HOST ), err
725+ )
703726 self ._provision_error = str (err )
704727
705728 if success :
@@ -712,7 +735,9 @@ async def async_step_do_provision(self) -> ConfigFlowResult:
712735 return self .async_show_form (
713736 step_id = "provision_failed" ,
714737 errors = {"base" : "provision_failed" },
715- description_placeholders = {"error" : self ._provision_error or "Unknown error" },
738+ description_placeholders = {
739+ "error" : self ._provision_error or "Unknown error"
740+ },
716741 )
717742
718743 async def async_step_provision_failed (
@@ -733,26 +758,33 @@ async def async_step_display_new_user(
733758 # Wait for services to fully restart after provisioning
734759 # Slower devices need more time for rpcd to come back up
735760 # We wait 10s now initially as it's a critical phase
736- _LOGGER .info ("Provisioning finished. Waiting 10s for router services to restart..." )
761+ _LOGGER .info (
762+ "Provisioning finished. Waiting 10s for router services to restart..."
763+ )
737764 await asyncio .sleep (10 )
738765
739766 # Re-check permissions with new user with retries
740767 new_user_success = False
741768 for attempt in range (10 ):
742769 _LOGGER .info (
743770 "Testing connection with new user 'homeassistant' (attempt %s/10)" ,
744- attempt + 1
771+ attempt + 1 ,
745772 )
746773 # Use a fresh connection test to avoid session leakage
747774 error = await self ._test_connection (self ._data )
748775 if not error :
749- _LOGGER .info ("Connection with new user successful on attempt %s" , attempt + 1 )
776+ _LOGGER .info (
777+ "Connection with new user successful on attempt %s" ,
778+ attempt + 1 ,
779+ )
750780 new_user_success = True
751781 break
752782
753783 _LOGGER .warning (
754784 "Auth attempt %s failed for %s: %s. Router might still be restarting services. Waiting 5s..." ,
755- attempt + 1 , self ._data .get (CONF_HOST ), error
785+ attempt + 1 ,
786+ self ._data .get (CONF_HOST ),
787+ error ,
756788 )
757789 await asyncio .sleep (5 )
758790
@@ -762,7 +794,7 @@ async def async_step_display_new_user(
762794 "Config might have applied but services didn't pick it up or user creation failed. "
763795 "Check your router logs for 'ha-openwrt' tags. Last error: %s" ,
764796 self ._data .get (CONF_HOST ),
765- error
797+ error ,
766798 )
767799 return await self .async_step_provision_user (
768800 errors = {"base" : error or "invalid_auth" }
0 commit comments