Skip to content

Commit 8b04ce1

Browse files
authored
Bump area registry to version 1.9 and sort areas (home-assistant#157634)
1 parent 39f7678 commit 8b04ce1

File tree

2 files changed

+88
-6
lines changed

2 files changed

+88
-6
lines changed

homeassistant/helpers/area_registry.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
)
4141
STORAGE_KEY = "core.area_registry"
4242
STORAGE_VERSION_MAJOR = 1
43-
STORAGE_VERSION_MINOR = 8
43+
STORAGE_VERSION_MINOR = 9
4444

4545

4646
class _AreaStoreData(TypedDict):
@@ -157,6 +157,13 @@ async def _async_migrate_func(
157157
area["humidity_entity_id"] = None
158158
area["temperature_entity_id"] = None
159159

160+
if old_minor_version < 9:
161+
# Version 1.9 sorts the areas by name
162+
old_data["areas"] = sorted(
163+
old_data["areas"],
164+
key=lambda area: area["name"].casefold(),
165+
)
166+
160167
if old_major_version > 1:
161168
raise NotImplementedError
162169
return old_data # type: ignore[return-value]

tests/helpers/test_area_registry.py

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for the Area Registry."""
22

3-
from datetime import datetime, timedelta
3+
from datetime import UTC, datetime, timedelta
44
from functools import partial
55
from typing import Any
66

@@ -438,16 +438,65 @@ async def test_migration_from_1_1(
438438
"""Test migration from version 1.1."""
439439
hass_storage[ar.STORAGE_KEY] = {
440440
"version": 1,
441-
"data": {"areas": [{"id": "12345A", "name": "mock"}]},
441+
"data": {
442+
"areas": [
443+
{"id": "12345A", "name": "AAA"},
444+
{"id": "12345B", "name": "CCC"},
445+
{"id": "12345C", "name": "bbb"},
446+
]
447+
},
442448
}
443449

444450
await ar.async_load(hass)
445451
registry = ar.async_get(hass)
446452

447453
# Test data was loaded
448-
entry = registry.async_get_or_create("mock")
454+
entry = registry.async_get_or_create("AAA")
449455
assert entry.id == "12345A"
450456

457+
# Check sort order
458+
assert list(registry.async_list_areas()) == [
459+
ar.AreaEntry(
460+
name="AAA",
461+
created_at=datetime(1970, 1, 1, 0, 0, tzinfo=UTC),
462+
modified_at=datetime(1970, 1, 1, 0, 0, tzinfo=UTC),
463+
aliases=set(),
464+
floor_id=None,
465+
humidity_entity_id=None,
466+
icon=None,
467+
id="12345A",
468+
labels=set(),
469+
picture=None,
470+
temperature_entity_id=None,
471+
),
472+
ar.AreaEntry(
473+
name="bbb",
474+
created_at=datetime(1970, 1, 1, 0, 0, tzinfo=UTC),
475+
modified_at=datetime(1970, 1, 1, 0, 0, tzinfo=UTC),
476+
aliases=set(),
477+
floor_id=None,
478+
humidity_entity_id=None,
479+
icon=None,
480+
id="12345C",
481+
labels=set(),
482+
picture=None,
483+
temperature_entity_id=None,
484+
),
485+
ar.AreaEntry(
486+
name="CCC",
487+
created_at=datetime(1970, 1, 1, 0, 0, tzinfo=UTC),
488+
modified_at=datetime(1970, 1, 1, 0, 0, tzinfo=UTC),
489+
aliases=set(),
490+
floor_id=None,
491+
humidity_entity_id=None,
492+
icon=None,
493+
id="12345B",
494+
labels=set(),
495+
picture=None,
496+
temperature_entity_id=None,
497+
),
498+
]
499+
451500
# Check we store migrated data
452501
await flush_store(registry._store)
453502
assert hass_storage[ar.STORAGE_KEY] == {
@@ -458,17 +507,43 @@ async def test_migration_from_1_1(
458507
"areas": [
459508
{
460509
"aliases": [],
510+
"created_at": "1970-01-01T00:00:00+00:00",
461511
"floor_id": None,
512+
"humidity_entity_id": None,
462513
"icon": None,
463514
"id": "12345A",
464515
"labels": [],
465-
"name": "mock",
516+
"modified_at": "1970-01-01T00:00:00+00:00",
517+
"name": "AAA",
466518
"picture": None,
519+
"temperature_entity_id": None,
520+
},
521+
{
522+
"aliases": [],
467523
"created_at": "1970-01-01T00:00:00+00:00",
524+
"floor_id": None,
525+
"humidity_entity_id": None,
526+
"icon": None,
527+
"id": "12345C",
528+
"labels": [],
468529
"modified_at": "1970-01-01T00:00:00+00:00",
530+
"name": "bbb",
531+
"picture": None,
469532
"temperature_entity_id": None,
533+
},
534+
{
535+
"aliases": [],
536+
"created_at": "1970-01-01T00:00:00+00:00",
537+
"floor_id": None,
470538
"humidity_entity_id": None,
471-
}
539+
"icon": None,
540+
"id": "12345B",
541+
"labels": [],
542+
"modified_at": "1970-01-01T00:00:00+00:00",
543+
"name": "CCC",
544+
"picture": None,
545+
"temperature_entity_id": None,
546+
},
472547
]
473548
},
474549
}

0 commit comments

Comments
 (0)