Improve type hints in registry helper tests (#119302)

This commit is contained in:
epenet 2024-06-10 20:25:34 +02:00 committed by GitHub
parent aa419686cb
commit eb6af2238c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 45 additions and 67 deletions

View File

@ -340,7 +340,7 @@ async def test_load_categories(
@pytest.mark.parametrize("load_registries", [False]) @pytest.mark.parametrize("load_registries", [False])
async def test_loading_categories_from_storage( async def test_loading_categories_from_storage(
hass: HomeAssistant, hass_storage: Any hass: HomeAssistant, hass_storage: dict[str, Any]
) -> None: ) -> None:
"""Test loading stored categories on start.""" """Test loading stored categories on start."""
hass_storage[cr.STORAGE_KEY] = { hass_storage[cr.STORAGE_KEY] = {

View File

@ -534,7 +534,7 @@ async def test_migration_1_3_to_1_5(
hass: HomeAssistant, hass: HomeAssistant,
hass_storage: dict[str, Any], hass_storage: dict[str, Any],
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
): ) -> None:
"""Test migration from version 1.3 to 1.5.""" """Test migration from version 1.3 to 1.5."""
hass_storage[dr.STORAGE_KEY] = { hass_storage[dr.STORAGE_KEY] = {
"version": 1, "version": 1,
@ -659,7 +659,7 @@ async def test_migration_1_4_to_1_5(
hass: HomeAssistant, hass: HomeAssistant,
hass_storage: dict[str, Any], hass_storage: dict[str, Any],
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
): ) -> None:
"""Test migration from version 1.4 to 1.5.""" """Test migration from version 1.4 to 1.5."""
hass_storage[dr.STORAGE_KEY] = { hass_storage[dr.STORAGE_KEY] = {
"version": 1, "version": 1,
@ -1219,7 +1219,7 @@ async def test_format_mac(
config_entry_id=mock_config_entry.entry_id, config_entry_id=mock_config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
) )
for mac in ["123456ABCDEF", "123456abcdef", "12:34:56:ab:cd:ef", "1234.56ab.cdef"]: for mac in ("123456ABCDEF", "123456abcdef", "12:34:56:ab:cd:ef", "1234.56ab.cdef"):
test_entry = device_registry.async_get_or_create( test_entry = device_registry.async_get_or_create(
config_entry_id=mock_config_entry.entry_id, config_entry_id=mock_config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, mac)}, connections={(dr.CONNECTION_NETWORK_MAC, mac)},
@ -1230,14 +1230,14 @@ async def test_format_mac(
} }
# This should not raise # This should not raise
for invalid in [ for invalid in (
"invalid_mac", "invalid_mac",
"123456ABCDEFG", # 1 extra char "123456ABCDEFG", # 1 extra char
"12:34:56:ab:cdef", # not enough : "12:34:56:ab:cdef", # not enough :
"12:34:56:ab:cd:e:f", # too many : "12:34:56:ab:cd:e:f", # too many :
"1234.56abcdef", # not enough . "1234.56abcdef", # not enough .
"123.456.abc.def", # too many . "123.456.abc.def", # too many .
]: ):
invalid_mac_entry = device_registry.async_get_or_create( invalid_mac_entry = device_registry.async_get_or_create(
config_entry_id=mock_config_entry.entry_id, config_entry_id=mock_config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, invalid)}, connections={(dr.CONNECTION_NETWORK_MAC, invalid)},
@ -2235,7 +2235,7 @@ async def test_device_info_configuration_url_validation(
hass: HomeAssistant, hass: HomeAssistant,
device_registry: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
configuration_url: str | URL | None, configuration_url: str | URL | None,
expectation, expectation: AbstractContextManager,
) -> None: ) -> None:
"""Test configuration URL of device info is properly validated.""" """Test configuration URL of device info is properly validated."""
config_entry_1 = MockConfigEntry() config_entry_1 = MockConfigEntry()

View File

@ -30,7 +30,7 @@ from tests.common import (
YAML__OPEN_PATH = "homeassistant.util.yaml.loader.open" YAML__OPEN_PATH = "homeassistant.util.yaml.loader.open"
async def test_get(entity_registry: er.EntityRegistry): async def test_get(entity_registry: er.EntityRegistry) -> None:
"""Test we can get an item.""" """Test we can get an item."""
entry = entity_registry.async_get_or_create("light", "hue", "1234") entry = entity_registry.async_get_or_create("light", "hue", "1234")
@ -620,7 +620,7 @@ async def test_removing_config_entry_id(
async def test_deleted_entity_removing_config_entry_id( async def test_deleted_entity_removing_config_entry_id(
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
): ) -> None:
"""Test that we update config entry id in registry on deleted entity.""" """Test that we update config entry id in registry on deleted entity."""
mock_config = MockConfigEntry(domain="light", entry_id="mock-id-1") mock_config = MockConfigEntry(domain="light", entry_id="mock-id-1")
@ -1528,9 +1528,7 @@ def test_entity_registry_items() -> None:
assert entities.get_entry(entry2.id) is None assert entities.get_entry(entry2.id) is None
async def test_disabled_by_str_not_allowed( async def test_disabled_by_str_not_allowed(entity_registry: er.EntityRegistry) -> None:
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test we need to pass disabled by type.""" """Test we need to pass disabled by type."""
with pytest.raises(ValueError): with pytest.raises(ValueError):
entity_registry.async_get_or_create( entity_registry.async_get_or_create(
@ -1545,7 +1543,7 @@ async def test_disabled_by_str_not_allowed(
async def test_entity_category_str_not_allowed( async def test_entity_category_str_not_allowed(
hass: HomeAssistant, entity_registry: er.EntityRegistry entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test we need to pass entity category type.""" """Test we need to pass entity category type."""
with pytest.raises(ValueError): with pytest.raises(ValueError):
@ -1574,9 +1572,7 @@ async def test_hidden_by_str_not_allowed(entity_registry: er.EntityRegistry) ->
) )
async def test_unique_id_non_hashable( async def test_unique_id_non_hashable(entity_registry: er.EntityRegistry) -> None:
hass: HomeAssistant, entity_registry: er.EntityRegistry
) -> None:
"""Test unique_id which is not hashable.""" """Test unique_id which is not hashable."""
with pytest.raises(TypeError): with pytest.raises(TypeError):
entity_registry.async_get_or_create("light", "hue", ["not", "valid"]) entity_registry.async_get_or_create("light", "hue", ["not", "valid"])
@ -1587,9 +1583,7 @@ async def test_unique_id_non_hashable(
async def test_unique_id_non_string( async def test_unique_id_non_string(
hass: HomeAssistant, entity_registry: er.EntityRegistry, caplog: pytest.LogCaptureFixture
entity_registry: er.EntityRegistry,
caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test unique_id which is not a string.""" """Test unique_id which is not a string."""
entity_registry.async_get_or_create("light", "hue", 1234) entity_registry.async_get_or_create("light", "hue", 1234)
@ -1683,7 +1677,7 @@ async def test_restore_entity(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
): ) -> None:
"""Make sure entity registry id is stable and entity_id is reused if possible.""" """Make sure entity registry id is stable and entity_id is reused if possible."""
update_events = async_capture_events(hass, er.EVENT_ENTITY_REGISTRY_UPDATED) update_events = async_capture_events(hass, er.EVENT_ENTITY_REGISTRY_UPDATED)
config_entry = MockConfigEntry(domain="light") config_entry = MockConfigEntry(domain="light")
@ -1777,7 +1771,7 @@ async def test_restore_entity(
async def test_async_migrate_entry_delete_self( async def test_async_migrate_entry_delete_self(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant, entity_registry: er.EntityRegistry
): ) -> None:
"""Test async_migrate_entry.""" """Test async_migrate_entry."""
config_entry1 = MockConfigEntry(domain="test1") config_entry1 = MockConfigEntry(domain="test1")
config_entry2 = MockConfigEntry(domain="test2") config_entry2 = MockConfigEntry(domain="test2")
@ -1812,7 +1806,7 @@ async def test_async_migrate_entry_delete_self(
async def test_async_migrate_entry_delete_other( async def test_async_migrate_entry_delete_other(
hass: HomeAssistant, entity_registry: er.EntityRegistry hass: HomeAssistant, entity_registry: er.EntityRegistry
): ) -> None:
"""Test async_migrate_entry.""" """Test async_migrate_entry."""
config_entry1 = MockConfigEntry(domain="test1") config_entry1 = MockConfigEntry(domain="test1")
config_entry2 = MockConfigEntry(domain="test2") config_entry2 = MockConfigEntry(domain="test2")

View File

@ -8,14 +8,6 @@ import pytest
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import area_registry as ar, floor_registry as fr from homeassistant.helpers import area_registry as ar, floor_registry as fr
from homeassistant.helpers.floor_registry import (
EVENT_FLOOR_REGISTRY_UPDATED,
STORAGE_KEY,
STORAGE_VERSION_MAJOR,
FloorRegistry,
async_get,
async_load,
)
from tests.common import async_capture_events, flush_store from tests.common import async_capture_events, flush_store
@ -30,7 +22,7 @@ async def test_create_floor(
hass: HomeAssistant, floor_registry: fr.FloorRegistry hass: HomeAssistant, floor_registry: fr.FloorRegistry
) -> None: ) -> None:
"""Make sure that we can create floors.""" """Make sure that we can create floors."""
update_events = async_capture_events(hass, EVENT_FLOOR_REGISTRY_UPDATED) update_events = async_capture_events(hass, fr.EVENT_FLOOR_REGISTRY_UPDATED)
floor = floor_registry.async_create( floor = floor_registry.async_create(
name="First floor", name="First floor",
icon="mdi:home-floor-1", icon="mdi:home-floor-1",
@ -59,7 +51,7 @@ async def test_create_floor_with_name_already_in_use(
hass: HomeAssistant, floor_registry: fr.FloorRegistry hass: HomeAssistant, floor_registry: fr.FloorRegistry
) -> None: ) -> None:
"""Make sure that we can't create a floor with a name already in use.""" """Make sure that we can't create a floor with a name already in use."""
update_events = async_capture_events(hass, EVENT_FLOOR_REGISTRY_UPDATED) update_events = async_capture_events(hass, fr.EVENT_FLOOR_REGISTRY_UPDATED)
floor_registry.async_create("First floor") floor_registry.async_create("First floor")
with pytest.raises( with pytest.raises(
@ -75,7 +67,7 @@ async def test_create_floor_with_name_already_in_use(
async def test_create_floor_with_id_already_in_use( async def test_create_floor_with_id_already_in_use(
hass: HomeAssistant, floor_registry: fr.FloorRegistry floor_registry: fr.FloorRegistry,
) -> None: ) -> None:
"""Make sure that we can't create an floor with an id already in use.""" """Make sure that we can't create an floor with an id already in use."""
floor = floor_registry.async_create("First") floor = floor_registry.async_create("First")
@ -92,7 +84,7 @@ async def test_delete_floor(
hass: HomeAssistant, floor_registry: fr.FloorRegistry hass: HomeAssistant, floor_registry: fr.FloorRegistry
) -> None: ) -> None:
"""Make sure that we can delete a floor.""" """Make sure that we can delete a floor."""
update_events = async_capture_events(hass, EVENT_FLOOR_REGISTRY_UPDATED) update_events = async_capture_events(hass, fr.EVENT_FLOOR_REGISTRY_UPDATED)
floor = floor_registry.async_create("First floor") floor = floor_registry.async_create("First floor")
assert len(floor_registry.floors) == 1 assert len(floor_registry.floors) == 1
@ -127,7 +119,7 @@ async def test_update_floor(
hass: HomeAssistant, floor_registry: fr.FloorRegistry hass: HomeAssistant, floor_registry: fr.FloorRegistry
) -> None: ) -> None:
"""Make sure that we can update floors.""" """Make sure that we can update floors."""
update_events = async_capture_events(hass, EVENT_FLOOR_REGISTRY_UPDATED) update_events = async_capture_events(hass, fr.EVENT_FLOOR_REGISTRY_UPDATED)
floor = floor_registry.async_create("First floor") floor = floor_registry.async_create("First floor")
assert len(floor_registry.floors) == 1 assert len(floor_registry.floors) == 1
@ -171,7 +163,7 @@ async def test_update_floor_with_same_data(
hass: HomeAssistant, floor_registry: fr.FloorRegistry hass: HomeAssistant, floor_registry: fr.FloorRegistry
) -> None: ) -> None:
"""Make sure that we can reapply the same data to a floor and it won't update.""" """Make sure that we can reapply the same data to a floor and it won't update."""
update_events = async_capture_events(hass, EVENT_FLOOR_REGISTRY_UPDATED) update_events = async_capture_events(hass, fr.EVENT_FLOOR_REGISTRY_UPDATED)
floor = floor_registry.async_create( floor = floor_registry.async_create(
"First floor", "First floor",
icon="mdi:home-floor-1", icon="mdi:home-floor-1",
@ -262,7 +254,7 @@ async def test_load_floors(
assert len(floor_registry.floors) == 2 assert len(floor_registry.floors) == 2
registry2 = FloorRegistry(hass) registry2 = fr.FloorRegistry(hass)
await flush_store(floor_registry._store) await flush_store(floor_registry._store)
await registry2.async_load() await registry2.async_load()
@ -288,11 +280,11 @@ async def test_load_floors(
@pytest.mark.parametrize("load_registries", [False]) @pytest.mark.parametrize("load_registries", [False])
async def test_loading_floors_from_storage( async def test_loading_floors_from_storage(
hass: HomeAssistant, hass_storage: Any hass: HomeAssistant, hass_storage: dict[str, Any]
) -> None: ) -> None:
"""Test loading stored floors on start.""" """Test loading stored floors on start."""
hass_storage[STORAGE_KEY] = { hass_storage[fr.STORAGE_KEY] = {
"version": STORAGE_VERSION_MAJOR, "version": fr.STORAGE_VERSION_MAJOR,
"data": { "data": {
"floors": [ "floors": [
{ {
@ -306,8 +298,8 @@ async def test_loading_floors_from_storage(
}, },
} }
await async_load(hass) await fr.async_load(hass)
registry = async_get(hass) registry = fr.async_get(hass)
assert len(registry.floors) == 1 assert len(registry.floors) == 1

View File

@ -12,14 +12,6 @@ from homeassistant.helpers import (
entity_registry as er, entity_registry as er,
label_registry as lr, label_registry as lr,
) )
from homeassistant.helpers.label_registry import (
EVENT_LABEL_REGISTRY_UPDATED,
STORAGE_KEY,
STORAGE_VERSION_MAJOR,
LabelRegistry,
async_get,
async_load,
)
from tests.common import MockConfigEntry, async_capture_events, flush_store from tests.common import MockConfigEntry, async_capture_events, flush_store
@ -34,7 +26,7 @@ async def test_create_label(
hass: HomeAssistant, label_registry: lr.LabelRegistry hass: HomeAssistant, label_registry: lr.LabelRegistry
) -> None: ) -> None:
"""Make sure that we can create labels.""" """Make sure that we can create labels."""
update_events = async_capture_events(hass, EVENT_LABEL_REGISTRY_UPDATED) update_events = async_capture_events(hass, lr.EVENT_LABEL_REGISTRY_UPDATED)
label = label_registry.async_create( label = label_registry.async_create(
name="My Label", name="My Label",
color="#FF0000", color="#FF0000",
@ -63,7 +55,7 @@ async def test_create_label_with_name_already_in_use(
hass: HomeAssistant, label_registry: lr.LabelRegistry hass: HomeAssistant, label_registry: lr.LabelRegistry
) -> None: ) -> None:
"""Make sure that we can't create a label with a ID already in use.""" """Make sure that we can't create a label with a ID already in use."""
update_events = async_capture_events(hass, EVENT_LABEL_REGISTRY_UPDATED) update_events = async_capture_events(hass, lr.EVENT_LABEL_REGISTRY_UPDATED)
label_registry.async_create("mock") label_registry.async_create("mock")
with pytest.raises( with pytest.raises(
@ -95,7 +87,7 @@ async def test_delete_label(
hass: HomeAssistant, label_registry: lr.LabelRegistry hass: HomeAssistant, label_registry: lr.LabelRegistry
) -> None: ) -> None:
"""Make sure that we can delete a label.""" """Make sure that we can delete a label."""
update_events = async_capture_events(hass, EVENT_LABEL_REGISTRY_UPDATED) update_events = async_capture_events(hass, lr.EVENT_LABEL_REGISTRY_UPDATED)
label = label_registry.async_create("Label") label = label_registry.async_create("Label")
assert len(label_registry.labels) == 1 assert len(label_registry.labels) == 1
@ -130,7 +122,7 @@ async def test_update_label(
hass: HomeAssistant, label_registry: lr.LabelRegistry hass: HomeAssistant, label_registry: lr.LabelRegistry
) -> None: ) -> None:
"""Make sure that we can update labels.""" """Make sure that we can update labels."""
update_events = async_capture_events(hass, EVENT_LABEL_REGISTRY_UPDATED) update_events = async_capture_events(hass, lr.EVENT_LABEL_REGISTRY_UPDATED)
label = label_registry.async_create("Mock") label = label_registry.async_create("Mock")
assert len(label_registry.labels) == 1 assert len(label_registry.labels) == 1
@ -174,7 +166,7 @@ async def test_update_label_with_same_data(
hass: HomeAssistant, label_registry: lr.LabelRegistry hass: HomeAssistant, label_registry: lr.LabelRegistry
) -> None: ) -> None:
"""Make sure that we can reapply the same data to the label and it won't update.""" """Make sure that we can reapply the same data to the label and it won't update."""
update_events = async_capture_events(hass, EVENT_LABEL_REGISTRY_UPDATED) update_events = async_capture_events(hass, lr.EVENT_LABEL_REGISTRY_UPDATED)
label = label_registry.async_create( label = label_registry.async_create(
"mock", "mock",
color="#FFFFFF", color="#FFFFFF",
@ -202,7 +194,7 @@ async def test_update_label_with_same_data(
async def test_update_label_with_same_name_change_case( async def test_update_label_with_same_name_change_case(
hass: HomeAssistant, label_registry: lr.LabelRegistry label_registry: lr.LabelRegistry,
) -> None: ) -> None:
"""Make sure that we can reapply the same name with a different case to the label.""" """Make sure that we can reapply the same name with a different case to the label."""
label = label_registry.async_create("mock") label = label_registry.async_create("mock")
@ -268,7 +260,7 @@ async def test_load_labels(
assert len(label_registry.labels) == 2 assert len(label_registry.labels) == 2
registry2 = LabelRegistry(hass) registry2 = lr.LabelRegistry(hass)
await flush_store(label_registry._store) await flush_store(label_registry._store)
await registry2.async_load() await registry2.async_load()
@ -293,11 +285,11 @@ async def test_load_labels(
@pytest.mark.parametrize("load_registries", [False]) @pytest.mark.parametrize("load_registries", [False])
async def test_loading_label_from_storage( async def test_loading_label_from_storage(
hass: HomeAssistant, hass_storage: Any hass: HomeAssistant, hass_storage: dict[str, Any]
) -> None: ) -> None:
"""Test loading stored labels on start.""" """Test loading stored labels on start."""
hass_storage[STORAGE_KEY] = { hass_storage[lr.STORAGE_KEY] = {
"version": STORAGE_VERSION_MAJOR, "version": lr.STORAGE_VERSION_MAJOR,
"data": { "data": {
"labels": [ "labels": [
{ {
@ -311,8 +303,8 @@ async def test_loading_label_from_storage(
}, },
} }
await async_load(hass) await lr.async_load(hass)
registry = async_get(hass) registry = lr.async_get(hass)
assert len(registry.labels) == 1 assert len(registry.labels) == 1

View File

@ -10,12 +10,12 @@ from homeassistant.helpers.normalized_name_base_registry import (
@pytest.fixture @pytest.fixture
def registry_items(): def registry_items() -> NormalizedNameBaseRegistryItems:
"""Fixture for registry items.""" """Fixture for registry items."""
return NormalizedNameBaseRegistryItems[NormalizedNameBaseRegistryEntry]() return NormalizedNameBaseRegistryItems[NormalizedNameBaseRegistryEntry]()
def test_normalize_name(): def test_normalize_name() -> None:
"""Test normalize_name.""" """Test normalize_name."""
assert normalize_name("Hello World") == "helloworld" assert normalize_name("Hello World") == "helloworld"
assert normalize_name("HELLO WORLD") == "helloworld" assert normalize_name("HELLO WORLD") == "helloworld"
@ -24,7 +24,7 @@ def test_normalize_name():
def test_registry_items( def test_registry_items(
registry_items: NormalizedNameBaseRegistryItems[NormalizedNameBaseRegistryEntry], registry_items: NormalizedNameBaseRegistryItems[NormalizedNameBaseRegistryEntry],
): ) -> None:
"""Test registry items.""" """Test registry items."""
entry = NormalizedNameBaseRegistryEntry( entry = NormalizedNameBaseRegistryEntry(
name="Hello World", normalized_name="helloworld" name="Hello World", normalized_name="helloworld"
@ -46,12 +46,12 @@ def test_registry_items(
# test delete entry # test delete entry
del registry_items["key"] del registry_items["key"]
assert "key" not in registry_items assert "key" not in registry_items
assert list(registry_items.values()) == [] assert not registry_items.values()
def test_key_already_in_use( def test_key_already_in_use(
registry_items: NormalizedNameBaseRegistryItems[NormalizedNameBaseRegistryEntry], registry_items: NormalizedNameBaseRegistryItems[NormalizedNameBaseRegistryEntry],
): ) -> None:
"""Test key already in use.""" """Test key already in use."""
entry = NormalizedNameBaseRegistryEntry( entry = NormalizedNameBaseRegistryEntry(
name="Hello World", normalized_name="helloworld" name="Hello World", normalized_name="helloworld"