|
1 | 1 | """Test area_registry API.""" |
2 | 2 |
|
3 | 3 | from datetime import datetime |
| 4 | +from typing import Any |
4 | 5 |
|
5 | 6 | from freezegun.api import FrozenDateTimeFactory |
6 | 7 | import pytest |
@@ -346,3 +347,92 @@ async def test_update_area_with_name_already_in_use( |
346 | 347 | assert msg["error"]["code"] == "invalid_info" |
347 | 348 | assert msg["error"]["message"] == "The name mock 2 (mock2) is already in use" |
348 | 349 | assert len(area_registry.areas) == 2 |
| 350 | + |
| 351 | + |
| 352 | +async def test_reorder_areas( |
| 353 | + client: MockHAClientWebSocket, area_registry: ar.AreaRegistry |
| 354 | +) -> None: |
| 355 | + """Test reorder areas.""" |
| 356 | + area1 = area_registry.async_create("mock 1") |
| 357 | + area2 = area_registry.async_create("mock 2") |
| 358 | + area3 = area_registry.async_create("mock 3") |
| 359 | + |
| 360 | + await client.send_json_auto_id({"type": "config/area_registry/list"}) |
| 361 | + msg = await client.receive_json() |
| 362 | + assert [area["area_id"] for area in msg["result"]] == [area1.id, area2.id, area3.id] |
| 363 | + |
| 364 | + await client.send_json_auto_id( |
| 365 | + { |
| 366 | + "type": "config/area_registry/reorder", |
| 367 | + "area_ids": [area3.id, area1.id, area2.id], |
| 368 | + } |
| 369 | + ) |
| 370 | + msg = await client.receive_json() |
| 371 | + assert msg["success"] |
| 372 | + |
| 373 | + await client.send_json_auto_id({"type": "config/area_registry/list"}) |
| 374 | + msg = await client.receive_json() |
| 375 | + assert [area["area_id"] for area in msg["result"]] == [area3.id, area1.id, area2.id] |
| 376 | + |
| 377 | + |
| 378 | +async def test_reorder_areas_invalid_area_ids( |
| 379 | + client: MockHAClientWebSocket, area_registry: ar.AreaRegistry |
| 380 | +) -> None: |
| 381 | + """Test reorder with invalid area IDs.""" |
| 382 | + area1 = area_registry.async_create("mock 1") |
| 383 | + area_registry.async_create("mock 2") |
| 384 | + |
| 385 | + await client.send_json_auto_id( |
| 386 | + { |
| 387 | + "type": "config/area_registry/reorder", |
| 388 | + "area_ids": [area1.id], |
| 389 | + } |
| 390 | + ) |
| 391 | + msg = await client.receive_json() |
| 392 | + assert not msg["success"] |
| 393 | + assert msg["error"]["code"] == "invalid_format" |
| 394 | + assert "must contain all existing area IDs" in msg["error"]["message"] |
| 395 | + |
| 396 | + |
| 397 | +async def test_reorder_areas_with_nonexistent_id( |
| 398 | + client: MockHAClientWebSocket, area_registry: ar.AreaRegistry |
| 399 | +) -> None: |
| 400 | + """Test reorder with nonexistent area ID.""" |
| 401 | + area1 = area_registry.async_create("mock 1") |
| 402 | + area2 = area_registry.async_create("mock 2") |
| 403 | + |
| 404 | + await client.send_json_auto_id( |
| 405 | + { |
| 406 | + "type": "config/area_registry/reorder", |
| 407 | + "area_ids": [area1.id, area2.id, "nonexistent"], |
| 408 | + } |
| 409 | + ) |
| 410 | + msg = await client.receive_json() |
| 411 | + assert not msg["success"] |
| 412 | + assert msg["error"]["code"] == "invalid_format" |
| 413 | + |
| 414 | + |
| 415 | +async def test_reorder_areas_persistence( |
| 416 | + hass: HomeAssistant, |
| 417 | + client: MockHAClientWebSocket, |
| 418 | + area_registry: ar.AreaRegistry, |
| 419 | + hass_storage: dict[str, Any], |
| 420 | +) -> None: |
| 421 | + """Test that area reordering is persisted.""" |
| 422 | + area1 = area_registry.async_create("mock 1") |
| 423 | + area2 = area_registry.async_create("mock 2") |
| 424 | + area3 = area_registry.async_create("mock 3") |
| 425 | + |
| 426 | + await client.send_json_auto_id( |
| 427 | + { |
| 428 | + "type": "config/area_registry/reorder", |
| 429 | + "area_ids": [area2.id, area3.id, area1.id], |
| 430 | + } |
| 431 | + ) |
| 432 | + msg = await client.receive_json() |
| 433 | + assert msg["success"] |
| 434 | + |
| 435 | + await hass.async_block_till_done() |
| 436 | + |
| 437 | + area_ids = [area.id for area in area_registry.async_list_areas()] |
| 438 | + assert area_ids == [area2.id, area3.id, area1.id] |
0 commit comments