Fix addon options reset to defaults (#6397)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Stefan Agner 2025-12-05 13:53:51 +01:00 committed by GitHub
parent 6f12d2cb6f
commit 5d02b09a0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 7 deletions

View File

@ -307,13 +307,17 @@ class APIAddons(CoreSysAttributes):
# Validate/Process Body
body = await api_validate(SCHEMA_OPTIONS, request)
if ATTR_OPTIONS in body:
try:
addon.options = addon.schema(body[ATTR_OPTIONS])
except vol.Invalid as ex:
raise AddonConfigurationInvalidError(
addon=addon.slug,
validation_error=humanize_error(body[ATTR_OPTIONS], ex),
) from None
# None resets options to defaults, otherwise validate the options
if body[ATTR_OPTIONS] is None:
addon.options = None
else:
try:
addon.options = addon.schema(body[ATTR_OPTIONS])
except vol.Invalid as ex:
raise AddonConfigurationInvalidError(
addon=addon.slug,
validation_error=humanize_error(body[ATTR_OPTIONS], ex),
) from None
if ATTR_BOOT in body:
if addon.boot_config == AddonBootConfig.MANUAL_ONLY:
raise AddonBootConfigCannotChangeError(

View File

@ -562,6 +562,27 @@ async def test_addon_set_options(api_client: TestClient, install_addon_example:
assert install_addon_example.options == {"message": "test"}
async def test_addon_reset_options(
api_client: TestClient, install_addon_example: Addon
):
"""Test resetting options for an addon to defaults.
Fixes SUPERVISOR-171F.
"""
# First set some custom options
install_addon_example.options = {"message": "custom"}
assert install_addon_example.persist["options"] == {"message": "custom"}
# Reset to defaults by sending null
resp = await api_client.post(
"/addons/local_example/options", json={"options": None}
)
assert resp.status == 200
# Persisted options should be empty (meaning defaults will be used)
assert install_addon_example.persist["options"] == {}
async def test_addon_set_options_error(
api_client: TestClient, install_addon_example: Addon
):