Skip to content

Commit 8b758c4

Browse files
authored
Combine Shelly BLE WiFi provisioning SSID and password steps (home-assistant#157199)
1 parent f439471 commit 8b758c4

File tree

3 files changed

+94
-257
lines changed

3 files changed

+94
-257
lines changed

homeassistant/components/shelly/config_flow.py

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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

homeassistant/components/shelly/strings.json

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,16 @@
100100
},
101101
"description": "Before setup, battery-powered devices must be woken up, you can now wake the device up using a button on it."
102102
},
103-
"wifi_credentials": {
104-
"data": {
105-
"password": "[%key:common::config_flow::data::password%]"
106-
},
107-
"data_description": {
108-
"password": "Password for the WiFi network."
109-
},
110-
"description": "Enter the password for {ssid}."
111-
},
112103
"wifi_scan": {
113104
"data": {
105+
"password": "[%key:common::config_flow::data::password%]",
114106
"ssid": "WiFi network"
115107
},
116108
"data_description": {
109+
"password": "Password for the WiFi network.",
117110
"ssid": "Select a WiFi network from the list or enter a custom SSID for hidden networks."
118111
},
119-
"description": "Select a WiFi network from the list or enter a custom SSID for hidden networks."
112+
"description": "Select a WiFi network and enter the password to provision the device."
120113
},
121114
"wifi_scan_failed": {
122115
"description": "Failed to scan for WiFi networks via Bluetooth. The device may be out of range or Bluetooth connection failed. Would you like to try again?"

0 commit comments

Comments
 (0)