Skip to content

Commit d445b32

Browse files
authored
Accept URLs in WLED Host input (home-assistant#157793)
Co-authored-by: mik-laj <[email protected]>
1 parent 7b6df1a commit d445b32

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

homeassistant/components/wled/config_flow.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import voluptuous as vol
88
from wled import WLED, Device, WLEDConnectionError, WLEDUnsupportedVersionError
9+
import yarl
910

1011
from homeassistant.components import onboarding
1112
from homeassistant.config_entries import (
@@ -24,6 +25,15 @@
2425
from .coordinator import WLEDConfigEntry
2526

2627

28+
def _normalize_host(host: str) -> str:
29+
"""Normalize host by extracting hostname if a URL is provided."""
30+
try:
31+
return yarl.URL(host).host or host
32+
except ValueError:
33+
pass
34+
return host
35+
36+
2737
class WLEDFlowHandler(ConfigFlow, domain=DOMAIN):
2838
"""Handle a WLED config flow."""
2939

@@ -46,8 +56,9 @@ async def async_step_user(
4656
errors = {}
4757

4858
if user_input is not None:
59+
host = _normalize_host(user_input[CONF_HOST])
4960
try:
50-
device = await self._async_get_device(user_input[CONF_HOST])
61+
device = await self._async_get_device(host)
5162
except WLEDUnsupportedVersionError:
5263
errors["base"] = "unsupported_version"
5364
except WLEDConnectionError:
@@ -67,16 +78,12 @@ async def async_step_user(
6778
)
6879
return self.async_update_reload_and_abort(
6980
entry,
70-
data_updates=user_input,
81+
data_updates={CONF_HOST: host},
7182
)
72-
self._abort_if_unique_id_configured(
73-
updates={CONF_HOST: user_input[CONF_HOST]}
74-
)
83+
self._abort_if_unique_id_configured(updates={CONF_HOST: host})
7584
return self.async_create_entry(
7685
title=device.info.name,
77-
data={
78-
CONF_HOST: user_input[CONF_HOST],
79-
},
86+
data={CONF_HOST: host},
8087
)
8188
data_schema = vol.Schema({vol.Required(CONF_HOST): str})
8289
if self.source == SOURCE_RECONFIGURE:

tests/components/wled/test_config_flow.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,18 @@
1717

1818

1919
@pytest.mark.usefixtures("mock_setup_entry", "mock_wled")
20-
async def test_full_user_flow_implementation(hass: HomeAssistant) -> None:
20+
@pytest.mark.parametrize(
21+
"host_input",
22+
[
23+
"192.168.1.123",
24+
"http://192.168.1.123",
25+
"https://192.168.1.123/settings",
26+
"https://192.168.1.123:80/settings",
27+
],
28+
)
29+
async def test_full_user_flow_implementation(
30+
hass: HomeAssistant, host_input: str
31+
) -> None:
2132
"""Test the full manual user flow from start to finish."""
2233
result = await hass.config_entries.flow.async_init(
2334
DOMAIN,
@@ -28,7 +39,7 @@ async def test_full_user_flow_implementation(hass: HomeAssistant) -> None:
2839
assert result.get("type") is FlowResultType.FORM
2940

3041
result = await hass.config_entries.flow.async_configure(
31-
result["flow_id"], user_input={CONF_HOST: "192.168.1.123"}
42+
result["flow_id"], user_input={CONF_HOST: host_input}
3243
)
3344

3445
assert result.get("title") == "WLED RGB Light"

0 commit comments

Comments
 (0)