Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip whitespaces from host in ping config flow #130970

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion homeassistant/components/ping/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
_LOGGER = logging.getLogger(__name__)


def _clean_user_input(user_input: dict[str, Any]) -> dict[str, Any]:
"""Clean up the user input."""
user_input[CONF_HOST] = user_input[CONF_HOST].strip()
return user_input


class PingConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Ping."""

Expand All @@ -46,6 +52,7 @@ async def async_step_user(
),
)

user_input = _clean_user_input(user_input)
if not is_ip_address(user_input[CONF_HOST]):
self.async_abort(reason="invalid_ip_address")

Expand Down Expand Up @@ -77,7 +84,7 @@ async def async_step_init(
) -> ConfigFlowResult:
"""Manage the options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
return self.async_create_entry(title="", data=_clean_user_input(user_input))

return self.async_show_form(
step_id="init",
Expand Down
36 changes: 22 additions & 14 deletions tests/components/ping/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@


@pytest.mark.parametrize(
("host", "expected_title"),
[("192.618.178.1", "192.618.178.1")],
("host", "expected"),
[
("192.618.178.1", "192.618.178.1"),
(" 192.618.178.1 ", "192.618.178.1"),
(" demo.host ", "demo.host"),
],
)
@pytest.mark.usefixtures("patch_setup")
async def test_form(hass: HomeAssistant, host, expected_title) -> None:
async def test_form(hass: HomeAssistant, host, expected) -> None:
"""Test we get the form."""

result = await hass.config_entries.flow.async_init(
Expand All @@ -35,30 +39,34 @@ async def test_form(hass: HomeAssistant, host, expected_title) -> None:
await hass.async_block_till_done()

assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == expected_title
assert result["title"] == expected
assert result["data"] == {}
assert result["options"] == {
"count": 5,
"host": host,
"host": expected,
"consider_home": 180,
}


@pytest.mark.parametrize(
("host", "count", "expected_title"),
[("192.618.178.1", 10, "192.618.178.1")],
("host", "expected_host"),
[
("192.618.178.1", "192.618.178.1"),
(" 192.618.178.1 ", "192.618.178.1"),
(" demo.host ", "demo.host"),
],
)
@pytest.mark.usefixtures("patch_setup")
async def test_options(hass: HomeAssistant, host, count, expected_title) -> None:
async def test_options(hass: HomeAssistant, host: str, expected_host: str) -> None:
"""Test options flow."""

config_entry = MockConfigEntry(
version=1,
source=config_entries.SOURCE_USER,
data={},
domain=DOMAIN,
options={"count": count, "host": host, "consider_home": 180},
title=expected_title,
options={"count": 1, "host": "192.168.1.1", "consider_home": 180},
title="192.168.1.1",
)
config_entry.add_to_hass(hass)

Expand All @@ -72,15 +80,15 @@ async def test_options(hass: HomeAssistant, host, count, expected_title) -> None
result = await hass.config_entries.options.async_configure(
result["flow_id"],
{
"host": "10.10.10.1",
"count": count,
"host": host,
"count": 10,
},
)
await hass.async_block_till_done()

assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == {
"count": count,
"host": "10.10.10.1",
"count": 10,
"host": expected_host,
"consider_home": 180,
}