8000 husqvarna_automower_ble: Simplify the config flow · alistair23/homeassistant-core@18d01e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 18d01e0

Browse files
committed
husqvarna_automower_ble: Simplify the config flow
Simplify the config flow so that we follow the same method for both Bluetooth discovery and manual setup. This should address: home-assistant#135440 (comment) Signed-off-by: Alistair Francis <alistair@alistair23.me>
1 parent ba324da commit 18d01e0

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

homeassistant/components/husqvarna_automower_ble/config_flow.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ async def async_step_bluetooth(
6868
async def async_step_bluetooth_confirm(
6969
self, user_input: dict[str, Any] | None = None
7070
) -> ConfigFlowResult:
71-
"""Confirm discovery."""
71+
"""Confirm Bluetooth discovery."""
7272

7373
if user_input is not None:
7474
self.pin = user_input[CONF_PIN]
75-
return await self.async_step_confirm()
75+
return await self.async_step_bluetooth_finalise()
7676

7777
return self.async_show_form(
7878
step_id="bluetooth_confirm",
@@ -86,14 +86,14 @@ async def async_step_bluetooth_confirm(
8686
async def async_step_user(
8787
self, user_input: dict[str, Any] | None = None
8888
) -> ConfigFlowResult:
89-
"""Handle the initial step."""
89+
"""Handle the initial manual step."""
9090

9191
if user_input is not None:
9292
self.address = user_input[CONF_ADDRESS]
9393
self.pin = user_input[CONF_PIN]
9494
await self.async_set_unique_id(self.address, raise_on_progress=False)
9595
self._abort_if_unique_id_configured()
96-
return await self.async_step_confirm()
96+
return await self.async_step_finalise()
9797

9898
return self.async_show_form(
9999
step_id="user",
@@ -114,7 +114,7 @@ async def probe_mower(self, device) -> str | None:
114114
channel_id, self.address
115115
).probe_gatts(device)
116116
except (BleakError, TimeoutError) as exception:
117-
LOGGER.exception("Failed to connect to device: %s", exception)
117+
LOGGER.exception("Failed to probe device: %s", exception)
118118
return None
119119

120120
title = manufacturer + " " + device_type
@@ -132,10 +132,12 @@ async def connect_mower(self, device) -> (int, Mower):
132132

133133
return (channel_id, mower)
134134

135-
async def async_step_confirm(
136-
self, user_input: dict[str, Any] | None = None
135+
async def check_mower(
136+
self,
137+
ble_flow: bool,
138+
user_input: dict[str, Any] | None = None,
137139
) -> ConfigFlowResult:
138-
"""Confirm discovery."""
140+
"""Check that the mower exists and is setup."""
139141
device = bluetooth.async_ble_device_from_address(
140142
self.hass, self.address, connectable=True
141143
)
@@ -151,9 +153,25 @@ async def async_step_confirm(
151153
if not await mower.connect(device):
152154
errors["base"] = "invalid_auth"
153155

156+
if ble_flow:
157+
return self.async_show_form(
158+
step_id="bluetooth_confirm",
159+
data_schema=vol.Schema(
160+
{
161+
vol.Required(CONF_ADDRESS): str,
162+
vol.Required(CONF_PIN): str,
163+
},
164+
),
165+
errors=errors,
166+
)
154167
return self.async_show_form(
155-
step_id="reauth_confirm",
156-
data_schema=vol.Schema({vol.Required(CONF_PIN): str}),
168+
step_id="user",
169+
data_schema=vol.Schema(
170+
{
171+
vol.Required(CONF_ADDRESS): str,
172+
vol.Required(CONF_PIN): str,
173+
},
174+
),
157175
errors=errors,
158176
)
159177
except (TimeoutError, BleakError):
@@ -168,6 +186,20 @@ async def async_step_confirm(
168186
},
169187
)
170188

189+
async def async_step_bluetooth_finalise(
190+
self,
191+
user_input: dict[str, Any] | None = None,
192+
) -> ConfigFlowResult:
193+
"""Finalise the Bluetooth setup."""
194+
return await self.check_mower(True, user_input)
195+
196+
async def async_step_finalise(
197+
self,
198+
user_input: dict[str, Any] | None = None,
199+
) -> ConfigFlowResult:
200+
"""Finalise the Manual setup."""
201+
return await self.check_mower(False, user_input)
202+
171203
async def async_step_reauth(
172204
self, entry_data: Mapping[str, Any]
173205
) -> ConfigFlowResult:

tests/components/husqvarna_automower_ble/test_config_flow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async def test_user_selection_incorrect_pin(
9494
)
9595

9696
assert result["type"] is FlowResultType.FORM
97-
assert result["step_id"] == "reauth_confirm"
97+
assert result["step_id"] == "user"
9898

9999
mock_automower_client.connect.return_value = True
100100

0 commit comments

Comments
 (0)
0