From 61991572d7a028dc010bf40700136794a7bc4139 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Thu, 29 Apr 2021 17:05:09 +0200 Subject: [PATCH] Fix `host_valid()` logic in DuneHD config flow (#49860) --- .../components/dunehd/config_flow.py | 2 +- tests/components/dunehd/test_config_flow.py | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/dunehd/config_flow.py b/homeassistant/components/dunehd/config_flow.py index 998ff408f36..034b1568c97 100644 --- a/homeassistant/components/dunehd/config_flow.py +++ b/homeassistant/components/dunehd/config_flow.py @@ -17,7 +17,7 @@ _LOGGER = logging.getLogger(__name__) def host_valid(host): """Return True if hostname or IP address is valid.""" try: - if ipaddress.ip_address(host).version == (4 or 6): + if ipaddress.ip_address(host).version in [4, 6]: return True except ValueError: if len(host) > 253: diff --git a/tests/components/dunehd/test_config_flow.py b/tests/components/dunehd/test_config_flow.py index b6e57a71668..278c94864b8 100644 --- a/tests/components/dunehd/test_config_flow.py +++ b/tests/components/dunehd/test_config_flow.py @@ -64,6 +64,16 @@ async def test_user_invalid_host(hass): assert result["errors"] == {CONF_HOST: "invalid_host"} +async def test_user_very_long_host(hass): + """Test that errors are shown when the host is longer than 253 chars.""" + long_host = "very_long_host_very_long_host_very_long_host_very_long_host_very_long_host_very_long_host_very_long_host_very_long_host_very_long_host_very_long_host_very_long_host_very_long_host_very_long_host_very_long_host_very_long_host_very_long_host_very_long_host" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER}, data={CONF_HOST: long_host} + ) + + assert result["errors"] == {CONF_HOST: "invalid_host"} + + async def test_user_cannot_connect(hass): """Test that errors are shown when cannot connect to the host.""" with patch("pdunehd.DuneHDPlayer.update_state", return_value={}): @@ -101,3 +111,17 @@ async def test_create_entry(hass): assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == "dunehd-host" assert result["data"] == {CONF_HOST: "dunehd-host"} + + +async def test_create_entry_with_ipv6_address(hass): + """Test that the user step works with device IPv6 address..""" + with patch("pdunehd.DuneHDPlayer.update_state", return_value=DUNEHD_STATE): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data={CONF_HOST: "2001:db8::1428:57ab"}, + ) + + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["title"] == "2001:db8::1428:57ab" + assert result["data"] == {CONF_HOST: "2001:db8::1428:57ab"}