mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Fix managed app configuration resetting existing options (#182441)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -274,14 +274,15 @@ class AddonManager:
|
||||
self,
|
||||
addon_config: dict[str, Any],
|
||||
) -> None:
|
||||
"""Configure the manager add-on, if needed."""
|
||||
"""Update the managed add-on options, preserving unspecified options."""
|
||||
addon_info = await self.async_get_addon_info()
|
||||
|
||||
if addon_info.state is AddonState.NOT_INSTALLED:
|
||||
raise AddonError(f"{self.addon_name} app is not installed")
|
||||
|
||||
if addon_config != addon_info.options:
|
||||
await self.async_set_addon_options(addon_config)
|
||||
new_addon_config = addon_info.options | addon_config
|
||||
if new_addon_config != addon_info.options:
|
||||
await self.async_set_addon_options(new_addon_config)
|
||||
|
||||
@callback
|
||||
def async_schedule_install_addon(self, catch_error: bool = False) -> asyncio.Task:
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Test the addon manager."""
|
||||
|
||||
import asyncio
|
||||
from typing import Any
|
||||
from unittest.mock import AsyncMock, call
|
||||
from uuid import uuid4
|
||||
|
||||
@@ -194,6 +195,70 @@ async def test_set_addon_options(
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("addon_installed")
|
||||
@pytest.mark.parametrize(
|
||||
"addon_config",
|
||||
[
|
||||
pytest.param({"new_key": "new"}, id="add-option"),
|
||||
pytest.param({"test_key": "updated"}, id="update-option"),
|
||||
],
|
||||
)
|
||||
async def test_configure_addon_preserves_options(
|
||||
addon_manager: AddonManager,
|
||||
addon_options: dict[str, Any],
|
||||
set_addon_options: AsyncMock,
|
||||
addon_config: dict[str, str],
|
||||
) -> None:
|
||||
"""Test configuring an add-on preserves all options outside the update."""
|
||||
existing_options = {
|
||||
"test_key": "test",
|
||||
"log_level": "debug",
|
||||
"log_to_file": True,
|
||||
"user_option": {"enabled": False, "values": [1, "two", None]},
|
||||
}
|
||||
addon_options.update(existing_options)
|
||||
expected_options = existing_options | addon_config
|
||||
|
||||
await addon_manager.async_configure_addon(addon_config)
|
||||
|
||||
set_addon_options.assert_awaited_once_with(
|
||||
"test_addon", AddonsOptions(config=expected_options)
|
||||
)
|
||||
assert addon_options == expected_options
|
||||
assert addon_options["log_level"] == "debug"
|
||||
assert addon_options["log_to_file"] is True
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("addon_installed")
|
||||
@pytest.mark.parametrize(
|
||||
"addon_config",
|
||||
[
|
||||
pytest.param({}, id="empty-update"),
|
||||
pytest.param({"test_key": "test"}, id="unchanged-option"),
|
||||
],
|
||||
)
|
||||
async def test_configure_addon_unchanged(
|
||||
addon_manager: AddonManager,
|
||||
addon_options: dict[str, Any],
|
||||
set_addon_options: AsyncMock,
|
||||
addon_config: dict[str, str],
|
||||
) -> None:
|
||||
"""Test unchanged managed options do not trigger a write."""
|
||||
existing_options = {
|
||||
"test_key": "test",
|
||||
"log_level": "debug",
|
||||
"log_to_file": True,
|
||||
}
|
||||
addon_options.update(existing_options)
|
||||
|
||||
await addon_manager.async_configure_addon(addon_config)
|
||||
|
||||
set_addon_options.assert_not_awaited()
|
||||
assert addon_options == existing_options
|
||||
assert addon_options["log_level"] == "debug"
|
||||
assert addon_options["log_to_file"] is True
|
||||
|
||||
|
||||
async def test_set_addon_options_error(
|
||||
hass: HomeAssistant, addon_manager: AddonManager, set_addon_options: AsyncMock
|
||||
) -> None:
|
||||
|
||||
@@ -1009,13 +1009,31 @@ async def test_null_name(
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("addon_installed", "addon_info")
|
||||
@pytest.mark.parametrize(
|
||||
"existing_addon_options",
|
||||
[
|
||||
pytest.param({}, id="unconfigured"),
|
||||
pytest.param(
|
||||
{
|
||||
"device": "/old",
|
||||
"log_level": "debug",
|
||||
"log_to_file": True,
|
||||
"rf_region": "Europe",
|
||||
},
|
||||
id="user-configured",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_start_addon(
|
||||
hass: HomeAssistant,
|
||||
install_addon: AsyncMock,
|
||||
set_addon_options: AsyncMock,
|
||||
start_addon: AsyncMock,
|
||||
addon_options: dict[str, Any],
|
||||
existing_addon_options: dict[str, Any],
|
||||
) -> None:
|
||||
"""Test start the Z-Wave JS add-on during entry setup."""
|
||||
"""Test entry setup preserves user options when starting the Z-Wave JS add-on."""
|
||||
addon_options.update(existing_addon_options)
|
||||
device = "/test"
|
||||
s0_legacy_key = "s0_legacy"
|
||||
s2_access_control_key = "s2_access_control"
|
||||
@@ -1023,7 +1041,7 @@ async def test_start_addon(
|
||||
s2_unauthenticated_key = "s2_unauthenticated"
|
||||
lr_s2_access_control_key = "lr_s2_access_control"
|
||||
lr_s2_authenticated_key = "lr_s2_authenticated"
|
||||
addon_options = {
|
||||
expected_options = existing_addon_options | {
|
||||
"device": device,
|
||||
"s0_legacy_key": s0_legacy_key,
|
||||
"s2_access_control_key": s2_access_control_key,
|
||||
@@ -1055,8 +1073,9 @@ async def test_start_addon(
|
||||
assert install_addon.call_count == 0
|
||||
assert set_addon_options.call_count == 1
|
||||
assert set_addon_options.call_args == call(
|
||||
"core_zwave_js", AddonsOptions(config=addon_options)
|
||||
"core_zwave_js", AddonsOptions(config=expected_options)
|
||||
)
|
||||
assert addon_options == expected_options
|
||||
assert start_addon.call_count == 1
|
||||
assert start_addon.call_args == call("core_zwave_js")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user