Fix optional password in Velbus config flow (#140615)

* Fix velbusconfigflow

* add tests

* Paramtize the tests

* Removed duplicate test in favor of another case

* more comments
This commit is contained in:
Maikel Punie 2025-03-18 15:49:27 +01:00 committed by Franck Nijhof
parent 28cad1d085
commit a2102f9b98
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
2 changed files with 27 additions and 41 deletions

View File

@ -63,7 +63,7 @@ class VelbusConfigFlow(ConfigFlow, domain=DOMAIN):
self._device = "tls://" self._device = "tls://"
else: else:
self._device = "" self._device = ""
if user_input[CONF_PASSWORD] != "": if CONF_PASSWORD in user_input and user_input[CONF_PASSWORD] != "":
self._device += f"{user_input[CONF_PASSWORD]}@" self._device += f"{user_input[CONF_PASSWORD]}@"
self._device += f"{user_input[CONF_HOST]}:{user_input[CONF_PORT]}" self._device += f"{user_input[CONF_HOST]}:{user_input[CONF_PORT]}"
self._async_abort_entries_match({CONF_PORT: self._device}) self._async_abort_entries_match({CONF_PORT: self._device})

View File

@ -59,43 +59,30 @@ def mock_controller_connection_failed():
@pytest.mark.usefixtures("controller") @pytest.mark.usefixtures("controller")
async def test_user_network_succes(hass: HomeAssistant) -> None: @pytest.mark.parametrize(
"""Test user network config.""" ("inputParams", "expected"),
# inttial menu show [
result = await hass.config_entries.flow.async_init( (
DOMAIN, context={"source": SOURCE_USER} {
) CONF_TLS: True,
assert result CONF_PASSWORD: "password",
assert result.get("flow_id") },
assert result.get("type") is FlowResultType.MENU "tls://password@velbus:6000",
assert result.get("step_id") == "user" ),
assert result.get("menu_options") == ["network", "usbselect"] (
# select the network option {
result = await hass.config_entries.flow.async_configure( CONF_TLS: True,
result.get("flow_id"), CONF_PASSWORD: "",
{"next_step_id": "network"}, },
) "tls://velbus:6000",
assert result.get("type") is FlowResultType.FORM ),
# fill in the network form ({CONF_TLS: True}, "tls://velbus:6000"),
result = await hass.config_entries.flow.async_configure( ({CONF_TLS: False}, "velbus:6000"),
result.get("flow_id"), ],
{ )
CONF_TLS: False, async def test_user_network_succes(
CONF_HOST: "velbus", hass: HomeAssistant, inputParams: str, expected: str
CONF_PORT: 6000, ) -> None:
CONF_PASSWORD: "",
},
)
assert result
assert result.get("type") is FlowResultType.CREATE_ENTRY
assert result.get("title") == "Velbus Network"
data = result.get("data")
assert data
assert data[CONF_PORT] == "velbus:6000"
@pytest.mark.usefixtures("controller")
async def test_user_network_succes_tls(hass: HomeAssistant) -> None:
"""Test user network config.""" """Test user network config."""
# inttial menu show # inttial menu show
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -116,10 +103,9 @@ async def test_user_network_succes_tls(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result.get("flow_id"), result.get("flow_id"),
{ {
CONF_TLS: True,
CONF_HOST: "velbus", CONF_HOST: "velbus",
CONF_PORT: 6000, CONF_PORT: 6000,
CONF_PASSWORD: "password", **inputParams,
}, },
) )
assert result assert result
@ -127,7 +113,7 @@ async def test_user_network_succes_tls(hass: HomeAssistant) -> None:
assert result.get("title") == "Velbus Network" assert result.get("title") == "Velbus Network"
data = result.get("data") data = result.get("data")
assert data assert data
assert data[CONF_PORT] == "tls://password@velbus:6000" assert data[CONF_PORT] == expected
@pytest.mark.usefixtures("controller") @pytest.mark.usefixtures("controller")