1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-20 02:48:57 +00:00

Accept URLs in WLED Host input (#157793)

Co-authored-by: mik-laj <12058428+mik-laj@users.noreply.github.com>
This commit is contained in:
Kamil Breguła
2025-12-12 18:55:03 +01:00
committed by GitHub
parent 7b6df1a8a0
commit d445b320de
2 changed files with 28 additions and 10 deletions

View File

@@ -6,6 +6,7 @@ from typing import Any
import voluptuous as vol import voluptuous as vol
from wled import WLED, Device, WLEDConnectionError, WLEDUnsupportedVersionError from wled import WLED, Device, WLEDConnectionError, WLEDUnsupportedVersionError
import yarl
from homeassistant.components import onboarding from homeassistant.components import onboarding
from homeassistant.config_entries import ( 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 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): class WLEDFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle a WLED config flow.""" """Handle a WLED config flow."""
@@ -46,8 +56,9 @@ class WLEDFlowHandler(ConfigFlow, domain=DOMAIN):
errors = {} errors = {}
if user_input is not None: if user_input is not None:
host = _normalize_host(user_input[CONF_HOST])
try: try:
device = await self._async_get_device(user_input[CONF_HOST]) device = await self._async_get_device(host)
except WLEDUnsupportedVersionError: except WLEDUnsupportedVersionError:
errors["base"] = "unsupported_version" errors["base"] = "unsupported_version"
except WLEDConnectionError: except WLEDConnectionError:
@@ -67,16 +78,12 @@ class WLEDFlowHandler(ConfigFlow, domain=DOMAIN):
) )
return self.async_update_reload_and_abort( return self.async_update_reload_and_abort(
entry, 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( return self.async_create_entry(
title=device.info.name, title=device.info.name,
data={ data={CONF_HOST: host},
CONF_HOST: user_input[CONF_HOST],
},
) )
data_schema = vol.Schema({vol.Required(CONF_HOST): str}) data_schema = vol.Schema({vol.Required(CONF_HOST): str})
if self.source == SOURCE_RECONFIGURE: if self.source == SOURCE_RECONFIGURE:

View File

@@ -17,7 +17,18 @@ from tests.common import MockConfigEntry
@pytest.mark.usefixtures("mock_setup_entry", "mock_wled") @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.""" """Test the full manual user flow from start to finish."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@@ -28,7 +39,7 @@ async def test_full_user_flow_implementation(hass: HomeAssistant) -> None:
assert result.get("type") is FlowResultType.FORM assert result.get("type") is FlowResultType.FORM
result = await hass.config_entries.flow.async_configure( 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" assert result.get("title") == "WLED RGB Light"