Non string unique id in entity registry now raise

This commit is contained in:
G Johansson
2025-10-01 19:06:06 +00:00
parent a3c8760b3f
commit 6d76ecf992
2 changed files with 24 additions and 48 deletions

View File

@@ -733,26 +733,12 @@ def _validate_item(
entity_category: EntityCategory | None | UndefinedType = None,
hidden_by: RegistryEntryHider | None | UndefinedType = None,
old_config_subentry_id: str | None = None,
report_non_string_unique_id: bool = True,
unique_id: str | Hashable | UndefinedType | Any,
) -> None:
"""Validate entity registry item."""
if unique_id is not UNDEFINED and not isinstance(unique_id, Hashable):
if unique_id is not UNDEFINED and not isinstance(unique_id, str):
raise TypeError(f"unique_id must be a string, got {unique_id}")
if (
report_non_string_unique_id
and unique_id is not UNDEFINED
and not isinstance(unique_id, str)
):
# In HA Core 2025.10, we should fail if unique_id is not a string
report_issue = async_suggest_report_issue(hass, integration_domain=platform)
_LOGGER.error(
"'%s' from integration %s has a non string unique_id '%s', please %s",
domain,
platform,
unique_id,
report_issue,
)
if config_entry_id and config_entry_id is not UNDEFINED:
if not hass.config_entries.async_get_entry(config_entry_id):
raise ValueError(
@@ -1506,7 +1492,6 @@ class EntityRegistry(BaseRegistry):
self.hass,
domain,
entity["platform"],
report_non_string_unique_id=False,
unique_id=entity["unique_id"],
)
except (TypeError, ValueError) as err:
@@ -1584,7 +1569,6 @@ class EntityRegistry(BaseRegistry):
self.hass,
domain,
entity["platform"],
report_non_string_unique_id=False,
unique_id=entity["unique_id"],
)
except (TypeError, ValueError):

View File

@@ -657,24 +657,29 @@ async def test_load_bad_data(
await er.async_load(hass)
registry = er.async_get(hass)
assert len(registry.entities) == 1
assert set(registry.entities.keys()) == {"test.test1"}
assert len(registry.entities) == 0
assert set(registry.entities.keys()) == set()
assert len(registry.deleted_entities) == 1
assert set(registry.deleted_entities.keys()) == {("test", "super_platform", 234)}
assert len(registry.deleted_entities) == 0
assert set(registry.deleted_entities.keys()) == set()
assert (
"'test' from integration super_platform has a non string unique_id '123', "
"please create a bug report" not in caplog.text
"'test.test1' from integration super_platform could not be loaded:"
" 'unique_id must be a string, got 123', please create a bug report"
in caplog.text
)
assert (
"'test' from integration super_platform has a non string unique_id '234', "
"please create a bug report" not in caplog.text
"'test.test2' from integration super_platform could not be loaded:"
" 'unique_id must be a string, got ['not', 'valid']', please create a bug report"
in caplog.text
)
assert (
"Entity registry entry 'test.test2' from integration super_platform could not "
"be loaded: 'unique_id must be a string, got ['not', 'valid']', please create "
"a bug report" in caplog.text
"'test.test3' from integration super_platform could not be loaded:"
not in caplog.text
)
assert (
"'test.test4' from integration super_platform could not be loaded:"
not in caplog.text
)
@@ -2902,32 +2907,19 @@ async def test_hidden_by_str_not_allowed(entity_registry: er.EntityRegistry) ->
)
async def test_unique_id_non_hashable(entity_registry: er.EntityRegistry) -> None:
async def test_unique_id_non_string(entity_registry: er.EntityRegistry) -> None:
"""Test unique_id which is not hashable."""
with pytest.raises(TypeError):
entity_registry.async_get_or_create("light", "hue", ["not", "valid"])
with pytest.raises(TypeError):
entity_registry.async_get_or_create("light", "hue", 1234)
entity_id = entity_registry.async_get_or_create("light", "hue", "1234").entity_id
with pytest.raises(TypeError):
entity_registry.async_update_entity(entity_id, new_unique_id=["not", "valid"])
async def test_unique_id_non_string(
entity_registry: er.EntityRegistry, caplog: pytest.LogCaptureFixture
) -> None:
"""Test unique_id which is not a string."""
entity_registry.async_get_or_create("light", "hue", 1234)
assert (
"'light' from integration hue has a non string unique_id '1234', "
"please create a bug report" in caplog.text
)
entity_id = entity_registry.async_get_or_create("light", "hue", "1234").entity_id
entity_registry.async_update_entity(entity_id, new_unique_id=2345)
assert (
"'light' from integration hue has a non string unique_id '2345', "
"please create a bug report" in caplog.text
)
with pytest.raises(TypeError):
entity_registry.async_update_entity(entity_id, new_unique_id=1234)
@pytest.mark.parametrize(