|
| 1 | +"""Regression tests for HA 2025.12+ Purpose-Specific Condition Intents (#306). |
| 2 | +
|
| 3 | +String values directly under a `condition:` key that look like entity IDs |
| 4 | +(e.g. `condition: person.is_not_home`) are Purpose-specific condition intents, |
| 5 | +NOT entity references, and must not be reported as false positives. |
| 6 | +""" |
| 7 | +import asyncio |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from custom_components.watchman.utils.parser_core import WatchmanParser |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def parser_client(tmp_path): |
| 17 | + """Create a WatchmanParser instance with a temporary database.""" |
| 18 | + db_path = tmp_path / "watchman.db" |
| 19 | + return WatchmanParser(str(db_path)) |
| 20 | + |
| 21 | + |
| 22 | +def _parse_and_get_entities(parser_client, yaml_dir): |
| 23 | + """Helper: parse a directory and return list of extracted entity IDs.""" |
| 24 | + asyncio.run(parser_client.async_parse(yaml_dir, [])) |
| 25 | + items = parser_client.get_found_items(item_type="all") |
| 26 | + return [item[0] for item in items if item[3] == "entity"] |
| 27 | + |
| 28 | + |
| 29 | +def test_purpose_specific_condition_not_extracted(parser_client, new_test_data_dir): |
| 30 | + """Test 1: condition: person.is_not_home must NOT appear in extracted entities. |
| 31 | +
|
| 32 | + 'person.is_not_home' is a Purpose-specific condition intent (HA 2025.12+), |
| 33 | + not an entity reference. IGNORED_VALUE_KEYS must suppress it. |
| 34 | + """ |
| 35 | + yaml_dir = str(Path(new_test_data_dir) / "yaml_config") |
| 36 | + entities = _parse_and_get_entities(parser_client, yaml_dir) |
| 37 | + assert "person.is_not_home" not in entities, ( |
| 38 | + "person.is_not_home is a condition intent and must not be reported as an entity" |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def test_nested_entity_id_still_extracted(parser_client, new_test_data_dir): |
| 43 | + """Test 2: entity_id nested inside a condition block must still be extracted. |
| 44 | +
|
| 45 | + IGNORED_VALUE_KEYS suppresses only the immediate string value of 'condition:'. |
| 46 | + Recursion into child structures must continue so that |
| 47 | + target.entity_id: person.xxxxxxxxxx is still found. |
| 48 | + """ |
| 49 | + yaml_dir = str(Path(new_test_data_dir) / "yaml_config") |
| 50 | + entities = _parse_and_get_entities(parser_client, yaml_dir) |
| 51 | + assert "person.xxxxxxxxxx" in entities, ( |
| 52 | + "person.xxxxxxxxxx is a real entity under target.entity_id and must be extracted" |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +def test_jinja2_template_in_condition_extracted(parser_client, new_test_data_dir): |
| 57 | + """Test 3: Jinja2 template under condition: must still yield entities (Heuristic 21). |
| 58 | +
|
| 59 | + condition: "{{ is_state('light.kitchen', 'on') }}" |
| 60 | + light.kitchen must be extracted — is_template() guard must bypass suppression. |
| 61 | + """ |
| 62 | + yaml_dir = str(Path(new_test_data_dir) / "yaml_config") |
| 63 | + entities = _parse_and_get_entities(parser_client, yaml_dir) |
| 64 | + assert "light.kitchen" in entities, ( |
| 65 | + "light.kitchen inside a Jinja2 template under condition: must be extracted" |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def test_jinja2_template_in_trigger_extracted(parser_client, new_test_data_dir): |
| 70 | + """Test 4: Jinja2 template under trigger: must still yield entities. |
| 71 | +
|
| 72 | + trigger: "{{ states('sensor.outdoor_temp') | float > 30 }}" |
| 73 | + sensor.outdoor_temp must be extracted — verifies that the is_template() |
| 74 | + exception that fixes condition: also works correctly for trigger:. |
| 75 | + """ |
| 76 | + yaml_dir = str(Path(new_test_data_dir) / "yaml_config") |
| 77 | + entities = _parse_and_get_entities(parser_client, yaml_dir) |
| 78 | + assert "sensor.outdoor_temp" in entities, ( |
| 79 | + "sensor.outdoor_temp inside a Jinja2 template under trigger: must be extracted" |
| 80 | + ) |
0 commit comments