Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow homeassistant in MQTT configuration_url schema #96107

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def validate_device_has_at_least_one_identifier(value: ConfigType) -> ConfigType
vol.Optional(CONF_SW_VERSION): cv.string,
vol.Optional(CONF_VIA_DEVICE): cv.string,
vol.Optional(CONF_SUGGESTED_AREA): cv.string,
vol.Optional(CONF_CONFIGURATION_URL): cv.url,
vol.Optional(CONF_CONFIGURATION_URL): cv.configuration_url,
}
),
validate_device_has_at_least_one_identifier,
Expand Down
29 changes: 27 additions & 2 deletions homeassistant/helpers/config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import voluptuous as vol
import voluptuous_serialize

from homeassistant.backports.enum import StrEnum
from homeassistant.const import (
ATTR_AREA_ID,
ATTR_DEVICE_ID,
Expand Down Expand Up @@ -106,6 +107,22 @@

TIME_PERIOD_ERROR = "offset {} should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F'"


class UrlProtocolSchema(StrEnum):
"""Valid URL protocol schema values."""

HTTP = "http"
HTTPS = "https"
HOMEASSISTANT = "homeassistant"


EXTERNAL_URL_PROTOCOL_SCHEMA_LIST = frozenset(
{UrlProtocolSchema.HTTP, UrlProtocolSchema.HTTPS}
)
CONFIGURATION_URL_PROTOCOL_SCHEMA_LIST = frozenset(
{UrlProtocolSchema.HOMEASSISTANT, UrlProtocolSchema.HTTP, UrlProtocolSchema.HTTPS}
)

# Home Assistant types
byte = vol.All(vol.Coerce(int), vol.Range(min=0, max=255))
small_float = vol.All(vol.Coerce(float), vol.Range(min=0, max=1))
Expand Down Expand Up @@ -728,16 +745,24 @@ def socket_timeout(value: Any | None) -> object:


# pylint: disable=no-value-for-parameter
def url(value: Any) -> str:
def url(
value: Any,
_schema_list: frozenset[UrlProtocolSchema] = EXTERNAL_URL_PROTOCOL_SCHEMA_LIST,
) -> str:
"""Validate an URL."""
url_in = str(value)

if urlparse(url_in).scheme in ["http", "https"]:
if urlparse(url_in).scheme in _schema_list:
return cast(str, vol.Schema(vol.Url())(url_in))

raise vol.Invalid("invalid url")


def configuration_url(value: Any) -> str:
"""Validate an URL that allows the homeassistant schema."""
return url(value, CONFIGURATION_URL_PROTOCOL_SCHEMA_LIST)


def url_no_path(value: Any) -> str:
"""Validate a url without a path."""
url_in = url(value)
Expand Down
29 changes: 29 additions & 0 deletions tests/helpers/test_config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,35 @@ def test_url() -> None:
assert schema(value)


def test_configuration_url() -> None:
"""Test URL."""
schema = vol.Schema(cv.configuration_url)

for value in (
"invalid",
None,
100,
"htp://ha.io",
"http//ha.io",
"http://??,**",
"https://??,**",
"homeassistant://??,**",
):
with pytest.raises(vol.MultipleInvalid):
schema(value)

for value in (
"http://localhost",
"https://localhost/test/index.html",
"http://home-assistant.io",
"http://home-assistant.io/test/",
"https://community.home-assistant.io/",
"homeassistant://api",
"homeassistant://api/hassio_ingress/XXXXXXX",
):
assert schema(value)


def test_url_no_path() -> None:
"""Test URL."""
schema = vol.Schema(cv.url_no_path)
Expand Down