Skip to content

Commit

Permalink
Remove discovery config validation from supervisor (#4937)
Browse files Browse the repository at this point in the history
* Remove discovery config validation from supervisor

* Remove invalid test

* Change validation to require a dictionary for compatibility
  • Loading branch information
mdegat01 authored Mar 5, 2024
1 parent 202ebf6 commit 74a5899
Show file tree
Hide file tree
Showing 39 changed files with 67 additions and 641 deletions.
15 changes: 0 additions & 15 deletions supervisor/addons/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
AddonStartup,
AddonState,
)
from ..discovery.validate import valid_discovery_service
from ..docker.const import Capabilities
from ..validate import (
docker_image,
Expand Down Expand Up @@ -190,20 +189,6 @@ def _warn_addon_config(config: dict[str, Any]):
name,
)

invalid_services: list[str] = []
for service in config.get(ATTR_DISCOVERY, []):
try:
valid_discovery_service(service)
except vol.Invalid:
invalid_services.append(service)

if invalid_services:
_LOGGER.warning(
"Add-on lists the following unknown services for discovery: %s. Please report this to the maintainer of %s",
", ".join(invalid_services),
name,
)

return config


Expand Down
12 changes: 1 addition & 11 deletions supervisor/api/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
AddonState,
)
from ..coresys import CoreSysAttributes
from ..discovery.validate import valid_discovery_service
from ..exceptions import APIError, APIForbidden
from .utils import api_process, api_validate, require_home_assistant

Expand All @@ -24,7 +23,7 @@
SCHEMA_DISCOVERY = vol.Schema(
{
vol.Required(ATTR_SERVICE): str,
vol.Optional(ATTR_CONFIG): vol.Maybe(dict),
vol.Required(ATTR_CONFIG): dict,
}
)

Expand Down Expand Up @@ -71,15 +70,6 @@ async def set_discovery(self, request):
addon: Addon = request[REQUEST_FROM]
service = body[ATTR_SERVICE]

try:
valid_discovery_service(service)
except vol.Invalid:
_LOGGER.warning(
"Received discovery message for unknown service %s from addon %s. Please report this to the maintainer of the add-on",
service,
addon.name,
)

# Access?
if body[ATTR_SERVICE] not in addon.discovery:
_LOGGER.error(
Expand Down
12 changes: 2 additions & 10 deletions supervisor/discovery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
from uuid import UUID, uuid4

import attr
import voluptuous as vol
from voluptuous.humanize import humanize_error

from ..const import ATTR_CONFIG, ATTR_DISCOVERY, FILE_HASSIO_DISCOVERY
from ..coresys import CoreSys, CoreSysAttributes
from ..exceptions import DiscoveryError, HomeAssistantAPIError
from ..exceptions import HomeAssistantAPIError
from ..utils.common import FileConfiguration
from .validate import SCHEMA_DISCOVERY_CONFIG, valid_discovery_config
from .validate import SCHEMA_DISCOVERY_CONFIG

if TYPE_CHECKING:
from ..addons.addon import Addon
Expand Down Expand Up @@ -75,12 +73,6 @@ def list_messages(self) -> list[Message]:

def send(self, addon: Addon, service: str, config: dict[str, Any]) -> Message:
"""Send a discovery message to Home Assistant."""
try:
config = valid_discovery_config(service, config)
except vol.Invalid as err:
_LOGGER.error("Invalid discovery %s config", humanize_error(config, err))
raise DiscoveryError() from err

# Create message
message = Message(addon.slug, service, config)

Expand Down
1 change: 0 additions & 1 deletion supervisor/discovery/services/__init__.py

This file was deleted.

9 changes: 0 additions & 9 deletions supervisor/discovery/services/adguard.py

This file was deleted.

9 changes: 0 additions & 9 deletions supervisor/discovery/services/almond.py

This file was deleted.

14 changes: 0 additions & 14 deletions supervisor/discovery/services/deconz.py

This file was deleted.

9 changes: 0 additions & 9 deletions supervisor/discovery/services/esphome.py

This file was deleted.

16 changes: 0 additions & 16 deletions supervisor/discovery/services/homematic.py

This file was deleted.

13 changes: 0 additions & 13 deletions supervisor/discovery/services/matter.py

This file was deleted.

6 changes: 0 additions & 6 deletions supervisor/discovery/services/motioneye.py

This file was deleted.

26 changes: 0 additions & 26 deletions supervisor/discovery/services/mqtt.py

This file was deleted.

13 changes: 0 additions & 13 deletions supervisor/discovery/services/otbr.py

This file was deleted.

15 changes: 0 additions & 15 deletions supervisor/discovery/services/ozw.py

This file was deleted.

9 changes: 0 additions & 9 deletions supervisor/discovery/services/rtsp_to_webrtc.py

This file was deleted.

9 changes: 0 additions & 9 deletions supervisor/discovery/services/unifi.py

This file was deleted.

14 changes: 0 additions & 14 deletions supervisor/discovery/services/vlc_telnet.py

This file was deleted.

25 changes: 0 additions & 25 deletions supervisor/discovery/services/wyoming.py

This file was deleted.

13 changes: 0 additions & 13 deletions supervisor/discovery/services/zwave_js.py

This file was deleted.

21 changes: 0 additions & 21 deletions supervisor/discovery/validate.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
"""Validate services schema."""
from importlib import import_module
from pathlib import Path

import voluptuous as vol

from ..const import ATTR_ADDON, ATTR_CONFIG, ATTR_DISCOVERY, ATTR_SERVICE, ATTR_UUID
from ..utils.validate import schema_or
from ..validate import uuid_match


def valid_discovery_service(service):
"""Validate service name."""
service_file = Path(__file__).parent.joinpath(f"services/{service}.py")
if not service_file.exists():
raise vol.Invalid(f"Service {service} not found") from None
return service


def valid_discovery_config(service, config):
"""Validate service name."""
try:
service_mod = import_module(f".services.{service}", "supervisor.discovery")
except ImportError:
raise vol.Invalid(f"Service {service} not found") from None

return service_mod.SCHEMA(config)


SCHEMA_DISCOVERY = vol.Schema(
[
vol.Schema(
Expand Down
13 changes: 0 additions & 13 deletions tests/addons/test_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Validate Add-on configs."""

import logging
from unittest.mock import Mock

import pytest
import voluptuous as vol
Expand Down Expand Up @@ -288,14 +286,3 @@ def test_valid_slug():
config["slug"] = "complemento telefónico"
with pytest.raises(vol.Invalid):
assert vd.SCHEMA_ADDON_CONFIG(config)


def test_invalid_discovery(capture_event: Mock, caplog: pytest.LogCaptureFixture):
"""Test invalid discovery."""
config = load_json_fixture("basic-addon-config.json")
config["discovery"] = ["mqtt", "junk", "junk2"]

assert vd.SCHEMA_ADDON_CONFIG(config)

with caplog.at_level(logging.WARNING):
assert "unknown services for discovery: junk, junk2" in caplog.text
Loading

0 comments on commit 74a5899

Please sign in to comment.