diff --git a/homeassistant/components/wled/config_flow.py b/homeassistant/components/wled/config_flow.py index 337d608ae11..d7aab20583e 100644 --- a/homeassistant/components/wled/config_flow.py +++ b/homeassistant/components/wled/config_flow.py @@ -6,6 +6,7 @@ from typing import Any import voluptuous as vol from wled import WLED, Device, WLEDConnectionError, WLEDUnsupportedVersionError +import yarl from homeassistant.components import onboarding from homeassistant.config_entries import ( @@ -24,6 +25,15 @@ from .const import CONF_KEEP_MAIN_LIGHT, DEFAULT_KEEP_MAIN_LIGHT, DOMAIN from .coordinator import WLEDConfigEntry +def _normalize_host(host: str) -> str: + """Normalize host by extracting hostname if a URL is provided.""" + try: + return yarl.URL(host).host or host + except ValueError: + pass + return host + + class WLEDFlowHandler(ConfigFlow, domain=DOMAIN): """Handle a WLED config flow.""" @@ -46,8 +56,9 @@ class WLEDFlowHandler(ConfigFlow, domain=DOMAIN): errors = {} if user_input is not None: + host = _normalize_host(user_input[CONF_HOST]) try: - device = await self._async_get_device(user_input[CONF_HOST]) + device = await self._async_get_device(host) except WLEDUnsupportedVersionError: errors["base"] = "unsupported_version" except WLEDConnectionError: @@ -67,16 +78,12 @@ class WLEDFlowHandler(ConfigFlow, domain=DOMAIN): ) return self.async_update_reload_and_abort( entry, - data_updates=user_input, + data_updates={CONF_HOST: host}, ) - self._abort_if_unique_id_configured( - updates={CONF_HOST: user_input[CONF_HOST]} - ) + self._abort_if_unique_id_configured(updates={CONF_HOST: host}) return self.async_create_entry( title=device.info.name, - data={ - CONF_HOST: user_input[CONF_HOST], - }, + data={CONF_HOST: host}, ) data_schema = vol.Schema({vol.Required(CONF_HOST): str}) if self.source == SOURCE_RECONFIGURE: diff --git a/tests/components/wled/test_config_flow.py b/tests/components/wled/test_config_flow.py index 984b511be7a..fe435135774 100644 --- a/tests/components/wled/test_config_flow.py +++ b/tests/components/wled/test_config_flow.py @@ -17,7 +17,18 @@ from tests.common import MockConfigEntry @pytest.mark.usefixtures("mock_setup_entry", "mock_wled") -async def test_full_user_flow_implementation(hass: HomeAssistant) -> None: +@pytest.mark.parametrize( + "host_input", + [ + "192.168.1.123", + "http://192.168.1.123", + "https://192.168.1.123/settings", + "https://192.168.1.123:80/settings", + ], +) +async def test_full_user_flow_implementation( + hass: HomeAssistant, host_input: str +) -> None: """Test the full manual user flow from start to finish.""" result = await hass.config_entries.flow.async_init( DOMAIN, @@ -28,7 +39,7 @@ async def test_full_user_flow_implementation(hass: HomeAssistant) -> None: assert result.get("type") is FlowResultType.FORM result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input={CONF_HOST: "192.168.1.123"} + result["flow_id"], user_input={CONF_HOST: host_input} ) assert result.get("title") == "WLED RGB Light"