1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Improve mysensors config flow (#75122)

* Improve mysensors config flow

* Improve form input order

* Update flow tests
This commit is contained in:
Martin Hjelmare
2022-08-07 01:22:50 +02:00
committed by GitHub
parent 1aa0e64354
commit e864b82c03
5 changed files with 113 additions and 137 deletions

View File

@@ -24,28 +24,32 @@ from homeassistant.components.mysensors.const import (
ConfGatewayType,
)
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.data_entry_flow import FlowResult, FlowResultType
from tests.common import MockConfigEntry
GATEWAY_TYPE_TO_STEP = {
CONF_GATEWAY_TYPE_TCP: "gw_tcp",
CONF_GATEWAY_TYPE_SERIAL: "gw_serial",
CONF_GATEWAY_TYPE_MQTT: "gw_mqtt",
}
async def get_form(
hass: HomeAssistant, gatway_type: ConfGatewayType, expected_step_id: str
hass: HomeAssistant, gateway_type: ConfGatewayType, expected_step_id: str
) -> FlowResult:
"""Get a form for the given gateway type."""
stepuser = await hass.config_entries.flow.async_init(
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert stepuser["type"] == "form"
assert not stepuser["errors"]
assert result["type"] == FlowResultType.MENU
result = await hass.config_entries.flow.async_configure(
stepuser["flow_id"],
{CONF_GATEWAY_TYPE: gatway_type},
result["flow_id"], {"next_step_id": GATEWAY_TYPE_TO_STEP[gateway_type]}
)
await hass.async_block_till_done()
assert result["type"] == "form"
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == expected_step_id
return result
@@ -62,7 +66,7 @@ async def test_config_mqtt(hass: HomeAssistant, mqtt: None) -> None:
"homeassistant.components.mysensors.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
flow_id,
{
CONF_RETAIN: True,
@@ -73,11 +77,11 @@ async def test_config_mqtt(hass: HomeAssistant, mqtt: None) -> None:
)
await hass.async_block_till_done()
if "errors" in result2:
assert not result2["errors"]
assert result2["type"] == "create_entry"
assert result2["title"] == "mqtt"
assert result2["data"] == {
if "errors" in result:
assert not result["errors"]
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "mqtt"
assert result["data"] == {
CONF_DEVICE: "mqtt",
CONF_RETAIN: True,
CONF_TOPIC_IN_PREFIX: "bla",
@@ -91,20 +95,19 @@ async def test_config_mqtt(hass: HomeAssistant, mqtt: None) -> None:
async def test_missing_mqtt(hass: HomeAssistant) -> None:
"""Test configuring a mqtt gateway without mqtt integration setup."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert not result["errors"]
assert result["type"] == FlowResultType.MENU
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_GATEWAY_TYPE: CONF_GATEWAY_TYPE_MQTT},
{"next_step_id": GATEWAY_TYPE_TO_STEP[CONF_GATEWAY_TYPE_MQTT]},
)
assert result["step_id"] == "user"
assert result["type"] == "form"
assert result["errors"] == {"base": "mqtt_required"}
await hass.async_block_till_done()
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "mqtt_required"
async def test_config_serial(hass: HomeAssistant) -> None:
@@ -123,7 +126,7 @@ async def test_config_serial(hass: HomeAssistant) -> None:
"homeassistant.components.mysensors.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
flow_id,
{
CONF_BAUD_RATE: 115200,
@@ -133,11 +136,11 @@ async def test_config_serial(hass: HomeAssistant) -> None:
)
await hass.async_block_till_done()
if "errors" in result2:
assert not result2["errors"]
assert result2["type"] == "create_entry"
assert result2["title"] == "/dev/ttyACM0"
assert result2["data"] == {
if "errors" in result:
assert not result["errors"]
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "/dev/ttyACM0"
assert result["data"] == {
CONF_DEVICE: "/dev/ttyACM0",
CONF_BAUD_RATE: 115200,
CONF_VERSION: "2.4",
@@ -160,7 +163,7 @@ async def test_config_tcp(hass: HomeAssistant) -> None:
"homeassistant.components.mysensors.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
flow_id,
{
CONF_TCP_PORT: 5003,
@@ -170,11 +173,11 @@ async def test_config_tcp(hass: HomeAssistant) -> None:
)
await hass.async_block_till_done()
if "errors" in result2:
assert not result2["errors"]
assert result2["type"] == "create_entry"
assert result2["title"] == "127.0.0.1"
assert result2["data"] == {
if "errors" in result:
assert not result["errors"]
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "127.0.0.1"
assert result["data"] == {
CONF_DEVICE: "127.0.0.1",
CONF_TCP_PORT: 5003,
CONF_VERSION: "2.4",
@@ -197,7 +200,7 @@ async def test_fail_to_connect(hass: HomeAssistant) -> None:
"homeassistant.components.mysensors.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
flow_id,
{
CONF_TCP_PORT: 5003,
@@ -207,9 +210,9 @@ async def test_fail_to_connect(hass: HomeAssistant) -> None:
)
await hass.async_block_till_done()
assert result2["type"] == "form"
assert "errors" in result2
errors = result2["errors"]
assert result["type"] == FlowResultType.FORM
assert "errors" in result
errors = result["errors"]
assert errors
assert errors.get("base") == "cannot_connect"
assert len(mock_setup.mock_calls) == 0
@@ -219,28 +222,6 @@ async def test_fail_to_connect(hass: HomeAssistant) -> None:
@pytest.mark.parametrize(
"gateway_type, expected_step_id, user_input, err_field, err_string",
[
(
CONF_GATEWAY_TYPE_TCP,
"gw_tcp",
{
CONF_TCP_PORT: 600_000,
CONF_DEVICE: "127.0.0.1",
CONF_VERSION: "2.4",
},
CONF_TCP_PORT,
"port_out_of_range",
),
(
CONF_GATEWAY_TYPE_TCP,
"gw_tcp",
{
CONF_TCP_PORT: 0,
CONF_DEVICE: "127.0.0.1",
CONF_VERSION: "2.4",
},
CONF_TCP_PORT,
"port_out_of_range",
),
(
CONF_GATEWAY_TYPE_TCP,
"gw_tcp",
@@ -382,15 +363,15 @@ async def test_config_invalid(
"homeassistant.components.mysensors.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result = await hass.config_entries.flow.async_configure(
flow_id,
user_input,
)
await hass.async_block_till_done()
assert result2["type"] == "form"
assert "errors" in result2
errors = result2["errors"]
assert result["type"] == FlowResultType.FORM
assert "errors" in result
errors = result["errors"]
assert errors
assert err_field in errors
assert errors[err_field] == err_string
@@ -681,10 +662,8 @@ async def test_duplicate(
MockConfigEntry(domain=DOMAIN, data=first_input).add_to_hass(hass)
second_gateway_type = second_input.pop(CONF_GATEWAY_TYPE)
result = await hass.config_entries.flow.async_init(
DOMAIN,
data={CONF_GATEWAY_TYPE: second_gateway_type},
context={"source": config_entries.SOURCE_USER},
result = await get_form(
hass, second_gateway_type, GATEWAY_TYPE_TO_STEP[second_gateway_type]
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],