mirror of
https://github.com/home-assistant/core.git
synced 2026-02-15 07:36:16 +00:00
Improve husqvarna_automower_ble config flow (#144877)
This commit is contained in:
committed by
GitHub
parent
af9717c1cd
commit
1509c429d6
@@ -21,6 +21,21 @@ from homeassistant.const import CONF_ADDRESS, CONF_CLIENT_ID, CONF_PIN
|
||||
|
||||
from .const import DOMAIN, LOGGER
|
||||
|
||||
BLUETOOTH_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_PIN): str,
|
||||
}
|
||||
)
|
||||
|
||||
USER_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_ADDRESS): str,
|
||||
vol.Required(CONF_PIN): str,
|
||||
}
|
||||
)
|
||||
|
||||
REAUTH_SCHEMA = BLUETOOTH_SCHEMA
|
||||
|
||||
|
||||
def _is_supported(discovery_info: BluetoothServiceInfo):
|
||||
"""Check if device is supported."""
|
||||
@@ -78,6 +93,10 @@ class HusqvarnaAutomowerBleConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
if not _is_supported(discovery_info):
|
||||
return self.async_abort(reason="no_devices_found")
|
||||
|
||||
self.context["title_placeholders"] = {
|
||||
"name": discovery_info.name,
|
||||
"address": discovery_info.address,
|
||||
}
|
||||
self.address = discovery_info.address
|
||||
await self.async_set_unique_id(self.address)
|
||||
self._abort_if_unique_id_configured()
|
||||
@@ -100,12 +119,7 @@ class HusqvarnaAutomowerBleConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
return self.async_show_form(
|
||||
step_id="bluetooth_confirm",
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_PIN): str,
|
||||
},
|
||||
),
|
||||
user_input,
|
||||
BLUETOOTH_SCHEMA, user_input
|
||||
),
|
||||
description_placeholders={"name": self.mower_name or self.address},
|
||||
errors=errors,
|
||||
@@ -129,15 +143,7 @@ class HusqvarnaAutomowerBleConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_ADDRESS): str,
|
||||
vol.Required(CONF_PIN): str,
|
||||
},
|
||||
),
|
||||
user_input,
|
||||
),
|
||||
data_schema=self.add_suggested_values_to_schema(USER_SCHEMA, user_input),
|
||||
errors=errors,
|
||||
)
|
||||
|
||||
@@ -184,7 +190,24 @@ class HusqvarnaAutomowerBleConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
|
||||
title = await self.probe_mower(device)
|
||||
if title is None:
|
||||
return self.async_abort(reason="cannot_connect")
|
||||
if self.source == SOURCE_BLUETOOTH:
|
||||
return self.async_show_form(
|
||||
step_id="bluetooth_confirm",
|
||||
data_schema=BLUETOOTH_SCHEMA,
|
||||
description_placeholders={"name": self.address},
|
||||
errors={"base": "cannot_connect"},
|
||||
)
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
USER_SCHEMA,
|
||||
{
|
||||
CONF_ADDRESS: self.address,
|
||||
CONF_PIN: self.pin,
|
||||
},
|
||||
),
|
||||
errors={"base": "cannot_connect"},
|
||||
)
|
||||
self.mower_name = title
|
||||
|
||||
try:
|
||||
@@ -209,11 +232,7 @@ class HusqvarnaAutomowerBleConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
if self.source == SOURCE_BLUETOOTH:
|
||||
return self.async_show_form(
|
||||
step_id="bluetooth_confirm",
|
||||
data_schema=vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_PIN): str,
|
||||
},
|
||||
),
|
||||
data_schema=BLUETOOTH_SCHEMA,
|
||||
description_placeholders={
|
||||
"name": self.mower_name or self.address
|
||||
},
|
||||
@@ -230,13 +249,7 @@ class HusqvarnaAutomowerBleConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_ADDRESS): str,
|
||||
vol.Required(CONF_PIN): str,
|
||||
},
|
||||
),
|
||||
suggested_values,
|
||||
USER_SCHEMA, suggested_values
|
||||
),
|
||||
errors=errors,
|
||||
)
|
||||
@@ -312,12 +325,7 @@ class HusqvarnaAutomowerBleConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
return self.async_show_form(
|
||||
step_id="reauth_confirm",
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_PIN): str,
|
||||
},
|
||||
),
|
||||
{CONF_PIN: self.pin},
|
||||
REAUTH_SCHEMA, {CONF_PIN: self.pin}
|
||||
),
|
||||
description_placeholders={"name": self.mower_name},
|
||||
errors=errors,
|
||||
|
||||
@@ -49,6 +49,22 @@ async def test_user_selection(hass: HomeAssistant) -> None:
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
# mock connection error
|
||||
with patch(
|
||||
"homeassistant.components.husqvarna_automower_ble.config_flow.HusqvarnaAutomowerBleConfigFlow.probe_mower",
|
||||
return_value=None,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_ADDRESS: "00000000-0000-0000-0000-000000000001",
|
||||
CONF_PIN: "1234",
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
@@ -488,9 +504,8 @@ async def test_exception_probe(
|
||||
result["flow_id"],
|
||||
user_input={CONF_PIN: "1234"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "cannot_connect"
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_exception_connect(
|
||||
|
||||
Reference in New Issue
Block a user