Normalize url entered in fibaro integration setup dialog (#81996)
* Normalize url entered in fibaro integration setup dialog * Improvements as suggested in code review * Fix spelling in comments
This commit is contained in:
parent
93401df73f
commit
ff1ec7a028
2 changed files with 34 additions and 0 deletions
|
@ -54,6 +54,18 @@ async def _validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str
|
|||
}
|
||||
|
||||
|
||||
def _normalize_url(url: str) -> str:
|
||||
"""Try to fix errors in the entered url.
|
||||
|
||||
We know that the url should be in the format http://<HOST>/api/
|
||||
"""
|
||||
if url.endswith("/api"):
|
||||
return f"{url}/"
|
||||
if not url.endswith("/api/"):
|
||||
return f"{url}api/" if url.endswith("/") else f"{url}/api/"
|
||||
return url
|
||||
|
||||
|
||||
class FibaroConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for Fibaro."""
|
||||
|
||||
|
@ -71,6 +83,7 @@ class FibaroConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
|
||||
if user_input is not None:
|
||||
try:
|
||||
user_input[CONF_URL] = _normalize_url(user_input[CONF_URL])
|
||||
info = await _validate_input(self.hass, user_input)
|
||||
except FibaroConnectFailed:
|
||||
errors["base"] = "cannot_connect"
|
||||
|
|
|
@ -6,6 +6,7 @@ import pytest
|
|||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.fibaro import DOMAIN
|
||||
from homeassistant.components.fibaro.config_flow import _normalize_url
|
||||
from homeassistant.components.fibaro.const import CONF_IMPORT_PLUGINS
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_URL, CONF_USERNAME
|
||||
|
||||
|
@ -362,3 +363,23 @@ async def test_reauth_auth_failure(hass):
|
|||
assert result["type"] == "form"
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
assert result["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
|
||||
async def test_normalize_url_does_not_touch_valid_url():
|
||||
"""Test that a correctly entered url is not touched."""
|
||||
assert _normalize_url(TEST_URL) == TEST_URL
|
||||
|
||||
|
||||
async def test_normalize_url_add_missing_slash_at_the_end():
|
||||
"""Test that a / is added at the end."""
|
||||
assert _normalize_url("http://192.168.1.1/api") == "http://192.168.1.1/api/"
|
||||
|
||||
|
||||
async def test_normalize_url_add_api():
|
||||
"""Test that api/ is added."""
|
||||
assert _normalize_url("http://192.168.1.1/") == "http://192.168.1.1/api/"
|
||||
|
||||
|
||||
async def test_normalize_url_add_api_with_leading_slash():
|
||||
"""Test that /api/ is added."""
|
||||
assert _normalize_url("http://192.168.1.1") == "http://192.168.1.1/api/"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue