Allow homeassistant in MQTT configuration_url schema (#96107)

This commit is contained in:
Jan Bouwhuis
2023-07-22 22:50:58 +02:00
committed by GitHub
parent 75f3054cc2
commit 9424d11408
3 changed files with 57 additions and 3 deletions

View File

@@ -25,6 +25,7 @@ from uuid import UUID
import voluptuous as vol
import voluptuous_serialize
from homeassistant.backports.enum import StrEnum
from homeassistant.const import (
ATTR_AREA_ID,
ATTR_DEVICE_ID,
@@ -106,6 +107,22 @@ from . import script_variables as script_variables_helper, template as template_
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))
@@ -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)