2019-04-13 20:17:01 +00:00
|
|
|
"""Manifest validation."""
|
|
|
|
from typing import Dict
|
2020-01-27 09:42:26 +00:00
|
|
|
from urllib.parse import urlparse
|
2019-04-13 20:17:01 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
from voluptuous.humanize import humanize_error
|
|
|
|
|
|
|
|
from .model import Integration
|
|
|
|
|
2020-01-27 09:42:26 +00:00
|
|
|
DOCUMENTATION_URL_SCHEMA = "https"
|
|
|
|
DOCUMENTATION_URL_HOST = "www.home-assistant.io"
|
|
|
|
DOCUMENTATION_URL_PATH_PREFIX = "/integrations/"
|
2020-04-16 16:00:04 +00:00
|
|
|
DOCUMENTATION_URL_EXCEPTIONS = {"https://www.home-assistant.io/hassio"}
|
2020-01-27 09:42:26 +00:00
|
|
|
|
2020-04-05 15:48:55 +00:00
|
|
|
SUPPORTED_QUALITY_SCALES = ["gold", "internal", "platinum", "silver"]
|
2020-01-07 16:21:56 +00:00
|
|
|
|
2020-01-27 09:42:26 +00:00
|
|
|
|
|
|
|
def documentation_url(value: str) -> str:
|
|
|
|
"""Validate that a documentation url has the correct path and domain."""
|
|
|
|
if value in DOCUMENTATION_URL_EXCEPTIONS:
|
|
|
|
return value
|
|
|
|
|
|
|
|
parsed_url = urlparse(value)
|
|
|
|
if not parsed_url.scheme == DOCUMENTATION_URL_SCHEMA:
|
|
|
|
raise vol.Invalid("Documentation url is not prefixed with https")
|
2020-04-16 16:00:04 +00:00
|
|
|
if parsed_url.netloc == DOCUMENTATION_URL_HOST and not parsed_url.path.startswith(
|
|
|
|
DOCUMENTATION_URL_PATH_PREFIX
|
|
|
|
):
|
2020-01-27 09:42:26 +00:00
|
|
|
raise vol.Invalid(
|
|
|
|
"Documentation url does not begin with www.home-assistant.io/integrations"
|
|
|
|
)
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
MANIFEST_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required("domain"): str,
|
|
|
|
vol.Required("name"): str,
|
|
|
|
vol.Optional("config_flow"): bool,
|
|
|
|
vol.Optional("zeroconf"): [str],
|
|
|
|
vol.Optional("ssdp"): vol.Schema(
|
2019-11-02 19:30:09 +00:00
|
|
|
vol.All([vol.All(vol.Schema({}, extra=vol.ALLOW_EXTRA), vol.Length(min=1))])
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
|
|
|
vol.Optional("homekit"): vol.Schema({vol.Optional("models"): [str]}),
|
2020-01-27 09:42:26 +00:00
|
|
|
vol.Required("documentation"): vol.All(
|
|
|
|
vol.Url(), documentation_url # pylint: disable=no-value-for-parameter
|
|
|
|
),
|
2020-01-07 16:21:56 +00:00
|
|
|
vol.Optional("quality_scale"): vol.In(SUPPORTED_QUALITY_SCALES),
|
2020-03-16 21:47:44 +00:00
|
|
|
vol.Optional("requirements"): [str],
|
|
|
|
vol.Optional("dependencies"): [str],
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional("after_dependencies"): [str],
|
|
|
|
vol.Required("codeowners"): [str],
|
2020-01-24 22:36:22 +00:00
|
|
|
vol.Optional("logo"): vol.Url(), # pylint: disable=no-value-for-parameter
|
|
|
|
vol.Optional("icon"): vol.Url(), # pylint: disable=no-value-for-parameter
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2019-04-13 20:17:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_manifest(integration: Integration):
|
|
|
|
"""Validate manifest."""
|
|
|
|
try:
|
|
|
|
MANIFEST_SCHEMA(integration.manifest)
|
|
|
|
except vol.Invalid as err:
|
|
|
|
integration.add_error(
|
2020-04-05 15:48:55 +00:00
|
|
|
"manifest", f"Invalid manifest: {humanize_error(integration.manifest, err)}"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-13 20:17:01 +00:00
|
|
|
integration.manifest = None
|
|
|
|
return
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if integration.manifest["domain"] != integration.path.name:
|
|
|
|
integration.add_error("manifest", "Domain does not match dir name")
|
2019-04-13 20:17:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate(integrations: Dict[str, Integration], config):
|
|
|
|
"""Handle all integrations manifests."""
|
|
|
|
for integration in integrations.values():
|
|
|
|
if integration.manifest:
|
|
|
|
validate_manifest(integration)
|