Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion custom_components/power_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
57 changes: 57 additions & 0 deletions tests/test_dashboard_reconfigure.py
Original file line number Diff line number Diff line change
@@ -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()