@@ -708,7 +708,8 @@ async def async_step_wifi_scan(
708708 """Scan for WiFi networks via BLE."""
709709 if user_input is not None :
710710 self .selected_ssid = user_input [CONF_SSID ]
711- return await self .async_step_wifi_credentials ()
711+ password = user_input [CONF_PASSWORD ]
712+ return await self .async_step_do_provision ({"password" : password })
712713
713714 # Scan for WiFi networks via BLE
714715 if TYPE_CHECKING :
@@ -726,22 +727,34 @@ async def async_step_wifi_scan(
726727 LOGGER .exception ("Unexpected exception during WiFi scan" )
727728 return self .async_abort (reason = "unknown" )
728729
729- # Create list of SSIDs for selection
730- # If no networks found, still allow custom SSID entry
731- ssid_options = [network ["ssid" ] for network in self .wifi_networks ]
730+ # Sort by RSSI (strongest signal first - higher/less negative values first)
731+ # and create list of SSIDs for selection
732+ sorted_networks = sorted (
733+ self .wifi_networks , key = lambda n : n ["rssi" ], reverse = True
734+ )
735+ ssid_options = [network ["ssid" ] for network in sorted_networks ]
736+
737+ # Pre-select SSID if returning from failed provisioning attempt
738+ suggested_values : dict [str , Any ] = {}
739+ if self .selected_ssid :
740+ suggested_values [CONF_SSID ] = self .selected_ssid
732741
733742 return self .async_show_form (
734743 step_id = "wifi_scan" ,
735- data_schema = vol .Schema (
736- {
737- vol .Required (CONF_SSID ): SelectSelector (
738- SelectSelectorConfig (
739- options = ssid_options ,
740- mode = SelectSelectorMode .DROPDOWN ,
741- custom_value = True ,
742- )
743- ),
744- }
744+ data_schema = self .add_suggested_values_to_schema (
745+ vol .Schema (
746+ {
747+ vol .Required (CONF_SSID ): SelectSelector (
748+ SelectSelectorConfig (
749+ options = ssid_options ,
750+ mode = SelectSelectorMode .DROPDOWN ,
751+ custom_value = True ,
752+ )
753+ ),
754+ vol .Required (CONF_PASSWORD ): str ,
755+ }
756+ ),
757+ suggested_values ,
745758 ),
746759 )
747760
@@ -769,25 +782,6 @@ async def _async_provision_context(
769782 finally :
770783 provisioning_registry .pop (normalized_mac , None )
771784
772- async def async_step_wifi_credentials (
773- self , user_input : dict [str , Any ] | None = None
774- ) -> ConfigFlowResult :
775- """Get WiFi credentials and provision device."""
776- if user_input is not None :
777- self .selected_ssid = user_input .get (CONF_SSID , self .selected_ssid )
778- password = user_input [CONF_PASSWORD ]
779- return await self .async_step_do_provision ({"password" : password })
780-
781- return self .async_show_form (
782- step_id = "wifi_credentials" ,
783- data_schema = vol .Schema (
784- {
785- vol .Required (CONF_PASSWORD ): str ,
786- }
787- ),
788- description_placeholders = {"ssid" : self .selected_ssid },
789- )
790-
791785 async def _async_secure_device_after_provision (self , host : str , port : int ) -> None :
792786 """Disable AP and/or BLE RPC after successful WiFi provisioning.
793787
@@ -1010,8 +1004,7 @@ async def async_step_provision_failed(
10101004 ) -> ConfigFlowResult :
10111005 """Handle failed provisioning - allow retry."""
10121006 if user_input is not None :
1013- # User wants to retry - clear state and go back to wifi_scan
1014- self .selected_ssid = ""
1007+ # User wants to retry - keep selected_ssid so it's pre-selected
10151008 self .wifi_networks = []
10161009 return await self .async_step_wifi_scan ()
10171010
0 commit comments