Skip to content

Commit

Permalink
Add support for optional nested dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
raman325 committed Oct 6, 2023
1 parent a79e06a commit 9b476e4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
6 changes: 1 addition & 5 deletions supervisor/addons/addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,7 @@ def test_update_schema(self) -> bool:
)

# create voluptuous
new_schema = vol.Schema(
vol.All(
dict, AddonOptions(self.coresys, new_raw_schema, self.name, self.slug)
)
)
new_schema = vol.Schema(vol.All(dict, AddonOptions(self, new_raw_schema)))

# validate
try:
Expand Down
2 changes: 1 addition & 1 deletion supervisor/addons/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def schema(self) -> AddonOptions:
if isinstance(raw_schema, bool):
raw_schema = {}

return AddonOptions(self.coresys, raw_schema, self.name, self.slug)
return AddonOptions(self, raw_schema)

@property
def schema_ui(self) -> list[dict[any, any]] | None:
Expand Down
25 changes: 18 additions & 7 deletions supervisor/addons/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import logging
from pathlib import Path
import re
from typing import Any
from typing import TYPE_CHECKING, Any

import voluptuous as vol

from ..const import ATTR_OPTIONS
from ..coresys import CoreSys, CoreSysAttributes
from ..exceptions import HardwareNotFound
from ..hardware.const import UdevSubsystem
from ..hardware.data import Device
from ..validate import network_port

if TYPE_CHECKING:
from .model import AddonModel

_LOGGER: logging.Logger = logging.getLogger(__name__)

_STR = "str"
Expand Down Expand Up @@ -58,16 +62,15 @@
class AddonOptions(CoreSysAttributes):
"""Validate Add-ons Options."""

def __init__(
self, coresys: CoreSys, raw_schema: dict[str, Any], name: str, slug: str
):
def __init__(self, addon: "AddonModel", raw_schema: dict[str, Any]):
"""Validate schema."""
self.coresys: CoreSys = coresys
self.addon: "AddonModel" = addon
self.coresys: CoreSys = addon.coresys
self.raw_schema: dict[str, Any] = raw_schema
self.devices: set[Device] = set()
self.pwned: set[str] = set()
self._name = name
self._slug = slug
self._name = addon.name
self._slug = addon.slug

@property
def validate(self) -> vol.Schema:
Expand Down Expand Up @@ -252,6 +255,14 @@ def _check_missing_options(
if isinstance(miss_schema, list) and len(miss_schema) > 0:
miss_schema = miss_schema[0]

# If its a dict that's missing and the dict is missing from add-on's
# options dict, then it's optional
if (
isinstance(miss_schema, dict)
and miss_opt not in self.addon.data[ATTR_OPTIONS]
):
miss_schema = "dict?"

if isinstance(miss_schema, str) and miss_schema.endswith("?"):
continue

Expand Down

0 comments on commit 9b476e4

Please sign in to comment.