mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Strip whitespaces from host in ping config flow (#130970)
This commit is contained in:
parent
5daf95ec8f
commit
2a1cdf6ff2
@ -27,6 +27,12 @@ from .const import CONF_PING_COUNT, DEFAULT_PING_COUNT, DOMAIN
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_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):
|
class PingConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow for Ping."""
|
"""Handle a config flow for Ping."""
|
||||||
|
|
||||||
@ -46,6 +52,7 @@ class PingConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
user_input = _clean_user_input(user_input)
|
||||||
if not is_ip_address(user_input[CONF_HOST]):
|
if not is_ip_address(user_input[CONF_HOST]):
|
||||||
self.async_abort(reason="invalid_ip_address")
|
self.async_abort(reason="invalid_ip_address")
|
||||||
|
|
||||||
@ -77,7 +84,7 @@ class OptionsFlowHandler(OptionsFlow):
|
|||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Manage the options."""
|
"""Manage the options."""
|
||||||
if user_input is not None:
|
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(
|
return self.async_show_form(
|
||||||
step_id="init",
|
step_id="init",
|
||||||
|
@ -13,11 +13,15 @@ from tests.common import MockConfigEntry
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("host", "expected_title"),
|
("host", "expected"),
|
||||||
[("192.618.178.1", "192.618.178.1")],
|
[
|
||||||
|
("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")
|
@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."""
|
"""Test we get the form."""
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
@ -35,21 +39,25 @@ async def test_form(hass: HomeAssistant, host, expected_title) -> None:
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result["title"] == expected_title
|
assert result["title"] == expected
|
||||||
assert result["data"] == {}
|
assert result["data"] == {}
|
||||||
assert result["options"] == {
|
assert result["options"] == {
|
||||||
"count": 5,
|
"count": 5,
|
||||||
"host": host,
|
"host": expected,
|
||||||
"consider_home": 180,
|
"consider_home": 180,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("host", "count", "expected_title"),
|
("host", "expected_host"),
|
||||||
[("192.618.178.1", 10, "192.618.178.1")],
|
[
|
||||||
|
("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")
|
@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."""
|
"""Test options flow."""
|
||||||
|
|
||||||
config_entry = MockConfigEntry(
|
config_entry = MockConfigEntry(
|
||||||
@ -57,8 +65,8 @@ async def test_options(hass: HomeAssistant, host, count, expected_title) -> None
|
|||||||
source=config_entries.SOURCE_USER,
|
source=config_entries.SOURCE_USER,
|
||||||
data={},
|
data={},
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
options={"count": count, "host": host, "consider_home": 180},
|
options={"count": 1, "host": "192.168.1.1", "consider_home": 180},
|
||||||
title=expected_title,
|
title="192.168.1.1",
|
||||||
)
|
)
|
||||||
config_entry.add_to_hass(hass)
|
config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
@ -72,15 +80,15 @@ async def test_options(hass: HomeAssistant, host, count, expected_title) -> None
|
|||||||
result = await hass.config_entries.options.async_configure(
|
result = await hass.config_entries.options.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{
|
{
|
||||||
"host": "10.10.10.1",
|
"host": host,
|
||||||
"count": count,
|
"count": 10,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||||
assert result["data"] == {
|
assert result["data"] == {
|
||||||
"count": count,
|
"count": 10,
|
||||||
"host": "10.10.10.1",
|
"host": expected_host,
|
||||||
"consider_home": 180,
|
"consider_home": 180,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user