From 7abffd7cc8842ce811fc4f384d5f024a92e0f779 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 29 May 2024 08:32:39 +0200 Subject: [PATCH] Don't report entities with invalid unique id when loading the entity registry (#118290) --- homeassistant/helpers/entity_registry.py | 23 ++++++++++++++++++----- tests/helpers/test_entity_registry.py | 8 ++++++-- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/homeassistant/helpers/entity_registry.py b/homeassistant/helpers/entity_registry.py index ebca6f17d43..dabe2e61917 100644 --- a/homeassistant/helpers/entity_registry.py +++ b/homeassistant/helpers/entity_registry.py @@ -618,17 +618,22 @@ def _validate_item( hass: HomeAssistant, domain: str, platform: str, - unique_id: str | Hashable | UndefinedType | Any, *, disabled_by: RegistryEntryDisabler | None | UndefinedType = None, entity_category: EntityCategory | None | UndefinedType = None, hidden_by: RegistryEntryHider | None | UndefinedType = 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): raise TypeError(f"unique_id must be a string, got {unique_id}") - if unique_id is not UNDEFINED and not isinstance(unique_id, str): - # In HA Core 2025.4, we should fail if unique_id is not a string + 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"), @@ -1227,7 +1232,11 @@ class EntityRegistry(BaseRegistry): try: domain = split_entity_id(entity["entity_id"])[0] _validate_item( - self.hass, domain, entity["platform"], entity["unique_id"] + self.hass, + domain, + entity["platform"], + report_non_string_unique_id=False, + unique_id=entity["unique_id"], ) except (TypeError, ValueError) as err: report_issue = async_suggest_report_issue( @@ -1283,7 +1292,11 @@ class EntityRegistry(BaseRegistry): try: domain = split_entity_id(entity["entity_id"])[0] _validate_item( - self.hass, domain, entity["platform"], entity["unique_id"] + self.hass, + domain, + entity["platform"], + report_non_string_unique_id=False, + unique_id=entity["unique_id"], ) except (TypeError, ValueError): continue diff --git a/tests/helpers/test_entity_registry.py b/tests/helpers/test_entity_registry.py index f158dc5b0de..4256707b7b1 100644 --- a/tests/helpers/test_entity_registry.py +++ b/tests/helpers/test_entity_registry.py @@ -511,7 +511,7 @@ async def test_load_bad_data( "id": "00003", "orphaned_timestamp": None, "platform": "super_platform", - "unique_id": 234, # Should trigger warning + "unique_id": 234, # Should not load }, { "config_entry_id": None, @@ -536,7 +536,11 @@ async def test_load_bad_data( assert ( "'test' from integration super_platform has a non string unique_id '123', " - "please create a bug report" in caplog.text + "please create a bug report" not 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 ) assert ( "Entity registry entry 'test.test2' from integration super_platform could not "