From 1902e1eecef4feeeff2401e89b32358da384c3d2 Mon Sep 17 00:00:00 2001 From: andbad Date: Thu, 2 Jul 2026 12:58:35 +0200 Subject: [PATCH] Commit: test: rebuild dashboard on reconfigure unless user_controlled --- custom_components/power_control/__init__.py | 3 +- tests/test_dashboard_reconfigure.py | 57 +++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/test_dashboard_reconfigure.py diff --git a/custom_components/power_control/__init__.py b/custom_components/power_control/__init__.py index e60a60f..28d71d6 100644 --- a/custom_components/power_control/__init__.py +++ b/custom_components/power_control/__init__.py @@ -401,7 +401,8 @@ async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> Non coordinator.rebuild_loads() coordinator.setup_global_sensor_listener() await coordinator.async_request_refresh() - await async_rebuild_dashboard(hass, entry) + if not entry.data.get(CONF_DASHBOARD_USER_CONTROLLED, False): + await async_rebuild_dashboard(hass, entry) _LOGGER.debug("[%s] Loads rebuilt after options update", DOMAIN) diff --git a/tests/test_dashboard_reconfigure.py b/tests/test_dashboard_reconfigure.py new file mode 100644 index 0000000..b317b5a --- /dev/null +++ b/tests/test_dashboard_reconfigure.py @@ -0,0 +1,57 @@ +"""Tests for dashboard rebuild behavior after options reconfigure.""" +from __future__ import annotations + +from unittest.mock import patch + +import pytest +from pytest_homeassistant_custom_component.common import MockConfigEntry + +from custom_components.power_control.const import DOMAIN, CONF_DASHBOARD_USER_CONTROLLED + +import sys, os +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) + +from tests.conftest import SAMPLE_DATA + + +async def _setup(hass, enable_custom_integrations, user_controlled: bool): + data = {**SAMPLE_DATA, "create_dashboard": True, CONF_DASHBOARD_USER_CONTROLLED: user_controlled} + entry = MockConfigEntry(domain=DOMAIN, data=data, options={}, title="PC") + entry.add_to_hass(hass) + with patch( + "custom_components.power_control._async_handle_dashboard_setup", + return_value=None, + ): + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + return entry + + +class TestDashboardRebuildOnReconfigure: + @pytest.mark.asyncio + async def test_rebuilds_when_not_user_controlled( + self, hass, enable_custom_integrations + ): + entry = await _setup(hass, enable_custom_integrations, user_controlled=False) + + from custom_components.power_control import _async_update_listener + + with patch( + "custom_components.power_control.async_rebuild_dashboard" + ) as mock_rebuild: + await _async_update_listener(hass, entry) + mock_rebuild.assert_called_once() + + @pytest.mark.asyncio + async def test_skips_when_user_controlled( + self, hass, enable_custom_integrations + ): + entry = await _setup(hass, enable_custom_integrations, user_controlled=True) + + from custom_components.power_control import _async_update_listener + + with patch( + "custom_components.power_control.async_rebuild_dashboard" + ) as mock_rebuild: + await _async_update_listener(hass, entry) + mock_rebuild.assert_not_called()