Improve logging when a unique id conflict is detected (#38434)
* fix error when unique id is re-used * add test for the logging * Update homeassistant/helpers/entity_platform.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/helpers/entity_platform.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
ff1709979f
commit
fe69a85386
2 changed files with 22 additions and 3 deletions
|
@ -325,11 +325,13 @@ class EntityPlatform:
|
||||||
entity.platform = None
|
entity.platform = None
|
||||||
return
|
return
|
||||||
|
|
||||||
|
requested_entity_id = None
|
||||||
suggested_object_id = None
|
suggested_object_id = None
|
||||||
|
|
||||||
# Get entity_id from unique ID registration
|
# Get entity_id from unique ID registration
|
||||||
if entity.unique_id is not None:
|
if entity.unique_id is not None:
|
||||||
if entity.entity_id is not None:
|
if entity.entity_id is not None:
|
||||||
|
requested_entity_id = entity.entity_id
|
||||||
suggested_object_id = split_entity_id(entity.entity_id)[1]
|
suggested_object_id = split_entity_id(entity.entity_id)[1]
|
||||||
else:
|
else:
|
||||||
suggested_object_id = entity.name
|
suggested_object_id = entity.name
|
||||||
|
@ -435,9 +437,14 @@ class EntityPlatform:
|
||||||
already_exists = True
|
already_exists = True
|
||||||
|
|
||||||
if already_exists:
|
if already_exists:
|
||||||
msg = f"Entity id already exists - ignoring: {entity.entity_id}"
|
|
||||||
if entity.unique_id is not None:
|
if entity.unique_id is not None:
|
||||||
msg += f". Platform {self.platform_name} does not generate unique IDs"
|
msg = f"Platform {self.platform_name} does not generate unique IDs. "
|
||||||
|
if requested_entity_id:
|
||||||
|
msg += f"ID {entity.unique_id} is already used by {entity.entity_id} - ignoring {requested_entity_id}"
|
||||||
|
else:
|
||||||
|
msg += f"ID {entity.unique_id} already exists - ignoring {entity.entity_id}"
|
||||||
|
else:
|
||||||
|
msg = f"Entity id already exists - ignoring: {entity.entity_id}"
|
||||||
self.logger.error(msg)
|
self.logger.error(msg)
|
||||||
entity.hass = None
|
entity.hass = None
|
||||||
entity.platform = None
|
entity.platform = None
|
||||||
|
|
|
@ -375,8 +375,9 @@ async def test_async_remove_with_platform(hass):
|
||||||
assert len(hass.states.async_entity_ids()) == 0
|
assert len(hass.states.async_entity_ids()) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_not_adding_duplicate_entities_with_unique_id(hass):
|
async def test_not_adding_duplicate_entities_with_unique_id(hass, caplog):
|
||||||
"""Test for not adding duplicate entities."""
|
"""Test for not adding duplicate entities."""
|
||||||
|
caplog.set_level(logging.ERROR)
|
||||||
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||||
|
|
||||||
await component.async_add_entities(
|
await component.async_add_entities(
|
||||||
|
@ -384,9 +385,20 @@ async def test_not_adding_duplicate_entities_with_unique_id(hass):
|
||||||
)
|
)
|
||||||
|
|
||||||
assert len(hass.states.async_entity_ids()) == 1
|
assert len(hass.states.async_entity_ids()) == 1
|
||||||
|
assert not caplog.text
|
||||||
|
|
||||||
ent2 = MockEntity(name="test2", unique_id="not_very_unique")
|
ent2 = MockEntity(name="test2", unique_id="not_very_unique")
|
||||||
await component.async_add_entities([ent2])
|
await component.async_add_entities([ent2])
|
||||||
|
assert "test1" in caplog.text
|
||||||
|
assert DOMAIN in caplog.text
|
||||||
|
|
||||||
|
ent3 = MockEntity(
|
||||||
|
name="test2", entity_id="test_domain.test3", unique_id="not_very_unique"
|
||||||
|
)
|
||||||
|
await component.async_add_entities([ent3])
|
||||||
|
assert "test1" in caplog.text
|
||||||
|
assert "test3" in caplog.text
|
||||||
|
assert DOMAIN in caplog.text
|
||||||
|
|
||||||
assert ent2.hass is None
|
assert ent2.hass is None
|
||||||
assert ent2.platform is None
|
assert ent2.platform is None
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue