|
13 | 13 |
|
14 | 14 | import pytest |
15 | 15 |
|
16 | | -from ...utilities.assertions import assert_mcp_success, parse_mcp_result, safe_call_tool |
| 16 | +from ...utilities.assertions import ( |
| 17 | + MCPAssertions, |
| 18 | + assert_mcp_success, |
| 19 | + parse_mcp_result, |
| 20 | + safe_call_tool, |
| 21 | +) |
| 22 | +from ...utilities.wait_helpers import wait_for_entity_state |
17 | 23 |
|
18 | 24 | logger = logging.getLogger(__name__) |
19 | 25 |
|
@@ -46,56 +52,78 @@ def _extract_bulk_boolean_entity_id(data: dict) -> str | None: |
46 | 52 | class TestBulkControl: |
47 | 53 | """Test ha_bulk_control tool functionality.""" |
48 | 54 |
|
49 | | - async def test_selector_dry_run_resolves_area_and_exclusion( |
50 | | - self, mcp_client, cleanup_tracker |
51 | | - ): |
| 55 | + async def test_selector_dry_run_resolves_area_and_exclusion(self, mcp_client): |
52 | 56 | """Preview exact area leaves after applying an entity exclusion.""" |
53 | 57 | suffix = uuid4().hex[:8] |
54 | | - area_result = await mcp_client.call_tool( |
55 | | - "ha_set_area_or_floor", |
56 | | - {"kind": "area", "name": f"Bulk selector {suffix}"}, |
57 | | - ) |
58 | | - area_data = assert_mcp_success(area_result, "Create selector area") |
59 | | - area_id = area_data["area_id"] |
60 | | - cleanup_tracker.track("area", area_id) |
61 | | - |
62 | | - entity_ids = [] |
63 | | - for label in ("included", "excluded"): |
64 | | - create_result = await mcp_client.call_tool( |
65 | | - "ha_config_set_helper", |
66 | | - { |
67 | | - "helper_type": "input_boolean", |
68 | | - "name": f"Bulk selector {label} {suffix}", |
69 | | - }, |
70 | | - ) |
71 | | - create_data = assert_mcp_success(create_result, "Create selector helper") |
72 | | - entity_id = _extract_bulk_boolean_entity_id(create_data) |
73 | | - assert entity_id, f"Missing helper entity_id: {create_data}" |
74 | | - cleanup_tracker.track("input_boolean", entity_id) |
75 | | - entity_ids.append(entity_id) |
76 | | - assign_result = await mcp_client.call_tool( |
77 | | - "ha_set_entity", {"entity_id": entity_id, "area_id": area_id} |
| 58 | + area_id: str | None = None |
| 59 | + entity_ids: list[str] = [] |
| 60 | + try: |
| 61 | + area_result = await mcp_client.call_tool( |
| 62 | + "ha_set_area_or_floor", |
| 63 | + {"kind": "area", "name": f"Bulk selector {suffix}"}, |
78 | 64 | ) |
79 | | - assert_mcp_success(assign_result, "Assign selector helper to area") |
| 65 | + area_data = assert_mcp_success(area_result, "Create selector area") |
| 66 | + area_id = area_data["area_id"] |
| 67 | + |
| 68 | + for label in ("included", "excluded"): |
| 69 | + create_result = await mcp_client.call_tool( |
| 70 | + "ha_config_set_helper", |
| 71 | + { |
| 72 | + "helper_type": "input_boolean", |
| 73 | + "name": f"Bulk selector {label} {suffix}", |
| 74 | + }, |
| 75 | + ) |
| 76 | + create_data = assert_mcp_success( |
| 77 | + create_result, "Create selector helper" |
| 78 | + ) |
| 79 | + entity_id = _extract_bulk_boolean_entity_id(create_data) |
| 80 | + assert entity_id, f"Missing helper entity_id: {create_data}" |
| 81 | + entity_ids.append(entity_id) |
| 82 | + assign_result = await mcp_client.call_tool( |
| 83 | + "ha_set_entity", {"entity_id": entity_id, "area_id": area_id} |
| 84 | + ) |
| 85 | + assert_mcp_success(assign_result, "Assign selector helper to area") |
80 | 86 |
|
81 | | - result = await mcp_client.call_tool( |
82 | | - "ha_bulk_control", |
83 | | - { |
84 | | - "selector": { |
85 | | - "domain": "input_boolean", |
86 | | - "area_ids": [area_id], |
87 | | - "exclude_entity_ids": [entity_ids[1]], |
88 | | - }, |
89 | | - "action": "off", |
90 | | - "dry_run": True, |
91 | | - }, |
92 | | - ) |
93 | | - data = assert_mcp_success(result, "Preview structural bulk selection") |
| 87 | + for entity_id in entity_ids: |
| 88 | + assert await wait_for_entity_state(mcp_client, entity_id, "off"), ( |
| 89 | + f"Selector helper {entity_id} was not registered in time" |
| 90 | + ) |
94 | 91 |
|
95 | | - assert data["dry_run"] is True |
96 | | - assert data["dispatched"] is False |
97 | | - assert data["resolution"]["resolved_entity_ids"] == [entity_ids[0]] |
98 | | - assert data["resolution"]["excluded_entity_ids"] == [entity_ids[1]] |
| 92 | + async with MCPAssertions(mcp_client) as mcp: |
| 93 | + data = await mcp.call_tool_success( |
| 94 | + "ha_bulk_control", |
| 95 | + { |
| 96 | + "selector": { |
| 97 | + "domain": "input_boolean", |
| 98 | + "area_ids": [area_id], |
| 99 | + "exclude_entity_ids": [entity_ids[1]], |
| 100 | + }, |
| 101 | + "action": "off", |
| 102 | + "dry_run": True, |
| 103 | + }, |
| 104 | + ) |
| 105 | + |
| 106 | + assert data["dry_run"] is True |
| 107 | + assert data["dispatched"] is False |
| 108 | + assert data["resolution"]["resolved_entity_ids"] == [entity_ids[0]] |
| 109 | + assert data["resolution"]["excluded_entity_ids"] == [entity_ids[1]] |
| 110 | + finally: |
| 111 | + for entity_id in entity_ids: |
| 112 | + await safe_call_tool( |
| 113 | + mcp_client, |
| 114 | + "ha_remove_helpers_integrations", |
| 115 | + { |
| 116 | + "helper_type": "input_boolean", |
| 117 | + "target": entity_id, |
| 118 | + "confirm": True, |
| 119 | + }, |
| 120 | + ) |
| 121 | + if area_id is not None: |
| 122 | + await safe_call_tool( |
| 123 | + mcp_client, |
| 124 | + "ha_remove_area_or_floor", |
| 125 | + {"kind": "area", "id": area_id}, |
| 126 | + ) |
99 | 127 |
|
100 | 128 | async def test_bulk_turn_on_single_light(self, mcp_client, test_light_entity): |
101 | 129 | """Test bulk_control with a single light entity.""" |
|
0 commit comments