catch ValueError when unique ID update fails because its taken and remove the duplicate entity (#47072)

This commit is contained in:
Raman Gupta 2021-02-25 20:41:54 -05:00 committed by GitHub
parent 989d3e5c87
commit 7118b7169c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 4 deletions

View File

@ -95,10 +95,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
old_unique_id,
new_unique_id,
)
ent_reg.async_update_entity(
entity_id,
new_unique_id=new_unique_id,
)
try:
ent_reg.async_update_entity(
entity_id,
new_unique_id=new_unique_id,
)
except ValueError:
LOGGER.debug(
(
"Entity %s can't be migrated because the unique ID is taken. "
"Cleaning it up since it is likely no longer valid."
),
entity_id,
)
ent_reg.async_remove(entity_id)
@callback
def async_on_node_ready(node: ZwaveNode) -> None:

View File

@ -124,6 +124,59 @@ async def test_on_node_added_ready(
)
async def test_unique_id_migration_dupes(
hass, multisensor_6_state, client, integration
):
"""Test we remove an entity when ."""
ent_reg = entity_registry.async_get(hass)
entity_name = AIR_TEMPERATURE_SENSOR.split(".")[1]
# Create entity RegistryEntry using old unique ID format
old_unique_id_1 = (
f"{client.driver.controller.home_id}.52.52-49-00-Air temperature-00"
)
entity_entry = ent_reg.async_get_or_create(
"sensor",
DOMAIN,
old_unique_id_1,
suggested_object_id=entity_name,
config_entry=integration,
original_name=entity_name,
)
assert entity_entry.entity_id == AIR_TEMPERATURE_SENSOR
assert entity_entry.unique_id == old_unique_id_1
# Create entity RegistryEntry using b0 unique ID format
old_unique_id_2 = (
f"{client.driver.controller.home_id}.52.52-49-0-Air temperature-00-00"
)
entity_entry = ent_reg.async_get_or_create(
"sensor",
DOMAIN,
old_unique_id_2,
suggested_object_id=f"{entity_name}_1",
config_entry=integration,
original_name=entity_name,
)
assert entity_entry.entity_id == f"{AIR_TEMPERATURE_SENSOR}_1"
assert entity_entry.unique_id == old_unique_id_2
# Add a ready node, unique ID should be migrated
node = Node(client, multisensor_6_state)
event = {"node": node}
client.driver.controller.emit("node added", event)
await hass.async_block_till_done()
# Check that new RegistryEntry is using new unique ID format
entity_entry = ent_reg.async_get(AIR_TEMPERATURE_SENSOR)
new_unique_id = f"{client.driver.controller.home_id}.52-49-0-Air temperature-00-00"
assert entity_entry.unique_id == new_unique_id
assert ent_reg.async_get(f"{AIR_TEMPERATURE_SENSOR}_1") is None
async def test_unique_id_migration_v1(hass, multisensor_6_state, client, integration):
"""Test unique ID is migrated from old format to new (version 1)."""
ent_reg = entity_registry.async_get(hass)