|
2 | 2 | import asyncio |
3 | 3 | import os |
4 | 4 | from pathlib import Path |
| 5 | +from unittest.mock import patch |
5 | 6 |
|
6 | 7 | from custom_components.watchman.utils.parser_core import WatchmanParser |
7 | 8 | import pytest |
| 9 | +from custom_components.watchman.const import ( |
| 10 | + CONF_IGNORED_FILES, |
| 11 | + CONF_IGNORED_STATES, |
| 12 | + CONF_SECTION_APPEARANCE_LOCATION, |
| 13 | + CONF_REPORT_PATH, |
| 14 | + CONF_HEADER, |
| 15 | + CONF_COLUMNS_WIDTH, |
| 16 | + CONF_STARTUP_DELAY, |
| 17 | + DOMAIN, |
| 18 | + STATE_IDLE, |
| 19 | + STATE_SAFE_MODE, |
| 20 | +) |
| 21 | +from homeassistant.core import HomeAssistant |
| 22 | +from homeassistant.data_entry_flow import FlowResultType |
| 23 | +from tests import async_init_integration |
8 | 24 |
|
9 | 25 | @pytest.fixture |
10 | 26 | def parser_client(tmp_path): |
@@ -62,3 +78,137 @@ def test_storage_ignored_files(parser_client, tmp_path): |
62 | 78 |
|
63 | 79 | # THIS SHOULD FAIL INITIALLY |
64 | 80 | assert not any("lovelace.dashboard_test" in p for p in processed_paths), "lovelace.dashboard_test should be IGNORED" |
| 81 | + |
| 82 | + |
| 83 | +async def _force_parse_and_wait(hass: HomeAssistant, config_entry_id: str) -> None: |
| 84 | + """Force an immediate re-parse on the current coordinator and wait for it to finish. |
| 85 | +
|
| 86 | + After an options-flow reload, the coordinator is in STATE_PENDING waiting on a |
| 87 | + call_later(0) handle that the asyncio test loop does not automatically drain. |
| 88 | + Calling request_parser_rescan(force=True) cancels the pending timer and creates a |
| 89 | + real asyncio Task that async_block_till_done() can observe. |
| 90 | + """ |
| 91 | + coordinator = hass.data[DOMAIN][config_entry_id] |
| 92 | + coordinator.request_parser_rescan(force=True) |
| 93 | + await hass.async_block_till_done() |
| 94 | + # Executor jobs (hass.async_add_executor_job) may not all be captured by a single |
| 95 | + # async_block_till_done; use a short polling loop as a safety net. |
| 96 | + for _ in range(50): |
| 97 | + if coordinator.status in [STATE_IDLE, STATE_SAFE_MODE]: |
| 98 | + break |
| 99 | + await asyncio.sleep(0.1) |
| 100 | + else: |
| 101 | + raise TimeoutError(f"Coordinator stuck in '{coordinator.status}' after forced parse") |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.asyncio |
| 105 | +async def test_options_flow_add_ignored_files(hass: HomeAssistant): |
| 106 | + """Test that adding an ignored file pattern via options flow hides its entities.""" |
| 107 | + # Init with default settings (basic_config.yaml is included, sensor.skylight is reported) |
| 108 | + config_entry = await async_init_integration( |
| 109 | + hass, |
| 110 | + add_params={ |
| 111 | + CONF_IGNORED_FILES: [], |
| 112 | + CONF_IGNORED_STATES: [], |
| 113 | + }, |
| 114 | + ) |
| 115 | + |
| 116 | + try: |
| 117 | + coordinator = hass.data[DOMAIN][config_entry.entry_id] |
| 118 | + entity_ids_before = [e["id"] for e in coordinator.data.get("entity_attrs", [])] |
| 119 | + assert "sensor.skylight" in entity_ids_before, ( |
| 120 | + "sensor.skylight must be present before ignoring basic_config.yaml" |
| 121 | + ) |
| 122 | + |
| 123 | + # Open options flow and ignore basic_config.yaml |
| 124 | + result = await hass.config_entries.options.async_init(config_entry.entry_id) |
| 125 | + assert result["type"] == FlowResultType.FORM |
| 126 | + |
| 127 | + user_input = { |
| 128 | + CONF_IGNORED_FILES: ["*/basic_config.yaml"], |
| 129 | + CONF_STARTUP_DELAY: 0, |
| 130 | + CONF_SECTION_APPEARANCE_LOCATION: { |
| 131 | + CONF_REPORT_PATH: hass.config.path("watchman_report.txt"), |
| 132 | + CONF_HEADER: "-== Watchman Report ==-", |
| 133 | + CONF_COLUMNS_WIDTH: "30, 8, 60", |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + with patch("custom_components.watchman.config_flow.async_is_valid_path", return_value=True): |
| 138 | + result = await hass.config_entries.options.async_configure( |
| 139 | + result["flow_id"], user_input=user_input |
| 140 | + ) |
| 141 | + await hass.async_block_till_done() |
| 142 | + |
| 143 | + assert result["type"] == FlowResultType.CREATE_ENTRY |
| 144 | + assert config_entry.data[CONF_IGNORED_FILES] == ["*/basic_config.yaml"] |
| 145 | + |
| 146 | + # Force an immediate re-parse (which skips basic_config.yaml) and wait |
| 147 | + await _force_parse_and_wait(hass, config_entry.entry_id) |
| 148 | + |
| 149 | + coordinator = hass.data[DOMAIN][config_entry.entry_id] |
| 150 | + entity_ids_after = [e["id"] for e in coordinator.data.get("entity_attrs", [])] |
| 151 | + assert "sensor.skylight" not in entity_ids_after, ( |
| 152 | + "sensor.skylight must be absent after ignoring basic_config.yaml" |
| 153 | + ) |
| 154 | + assert "switch.skylight" not in entity_ids_after, ( |
| 155 | + "switch.skylight must be absent after ignoring basic_config.yaml" |
| 156 | + ) |
| 157 | + finally: |
| 158 | + await hass.config_entries.async_unload(config_entry.entry_id) |
| 159 | + await hass.async_block_till_done() |
| 160 | + |
| 161 | + |
| 162 | +@pytest.mark.asyncio |
| 163 | +async def test_options_flow_clear_ignored_files(hass: HomeAssistant): |
| 164 | + """Test that clearing ignored files via options flow restores their entities.""" |
| 165 | + # Init with basic_config.yaml already ignored — sensor.skylight is NOT parsed |
| 166 | + config_entry = await async_init_integration( |
| 167 | + hass, |
| 168 | + add_params={ |
| 169 | + CONF_IGNORED_FILES: ["*/basic_config.yaml"], |
| 170 | + CONF_IGNORED_STATES: [], |
| 171 | + }, |
| 172 | + ) |
| 173 | + |
| 174 | + try: |
| 175 | + coordinator = hass.data[DOMAIN][config_entry.entry_id] |
| 176 | + entity_ids_before = [e["id"] for e in coordinator.data.get("entity_attrs", [])] |
| 177 | + assert "sensor.skylight" not in entity_ids_before, ( |
| 178 | + "sensor.skylight must be absent when basic_config.yaml is ignored" |
| 179 | + ) |
| 180 | + |
| 181 | + # Open options flow and clear ignored files |
| 182 | + result = await hass.config_entries.options.async_init(config_entry.entry_id) |
| 183 | + assert result["type"] == FlowResultType.FORM |
| 184 | + |
| 185 | + user_input = { |
| 186 | + CONF_IGNORED_FILES: [], |
| 187 | + CONF_STARTUP_DELAY: 0, |
| 188 | + CONF_SECTION_APPEARANCE_LOCATION: { |
| 189 | + CONF_REPORT_PATH: hass.config.path("watchman_report.txt"), |
| 190 | + CONF_HEADER: "-== Watchman Report ==-", |
| 191 | + CONF_COLUMNS_WIDTH: "30, 8, 60", |
| 192 | + }, |
| 193 | + } |
| 194 | + |
| 195 | + with patch("custom_components.watchman.config_flow.async_is_valid_path", return_value=True): |
| 196 | + result = await hass.config_entries.options.async_configure( |
| 197 | + result["flow_id"], user_input=user_input |
| 198 | + ) |
| 199 | + await hass.async_block_till_done() |
| 200 | + |
| 201 | + assert result["type"] == FlowResultType.CREATE_ENTRY |
| 202 | + assert config_entry.data[CONF_IGNORED_FILES] == [] |
| 203 | + |
| 204 | + # Force an immediate re-parse (which now includes basic_config.yaml) and wait |
| 205 | + await _force_parse_and_wait(hass, config_entry.entry_id) |
| 206 | + |
| 207 | + coordinator = hass.data[DOMAIN][config_entry.entry_id] |
| 208 | + entity_ids_after = [e["id"] for e in coordinator.data.get("entity_attrs", [])] |
| 209 | + assert "sensor.skylight" in entity_ids_after, ( |
| 210 | + "sensor.skylight must reappear after clearing ignored files" |
| 211 | + ) |
| 212 | + finally: |
| 213 | + await hass.config_entries.async_unload(config_entry.entry_id) |
| 214 | + await hass.async_block_till_done() |
0 commit comments