1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 04:50:05 +00:00

Fix zwave_js unique ID migration logic (#47031)

This commit is contained in:
Raman Gupta
2021-02-25 02:14:32 -05:00
committed by GitHub
parent 7dc9071776
commit a43f3c1a0c
4 changed files with 130 additions and 28 deletions

View File

@@ -18,7 +18,7 @@ from homeassistant.config_entries import (
from homeassistant.const import STATE_UNAVAILABLE
from homeassistant.helpers import device_registry, entity_registry
from .common import AIR_TEMPERATURE_SENSOR
from .common import AIR_TEMPERATURE_SENSOR, NOTIFICATION_MOTION_BINARY_SENSOR
from tests.common import MockConfigEntry
@@ -124,13 +124,15 @@ async def test_on_node_added_ready(
)
async def test_unique_id_migration(hass, multisensor_6_state, client, integration):
"""Test unique ID is migrated from old format to new."""
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)
# Migrate version 1
entity_name = AIR_TEMPERATURE_SENSOR.split(".")[1]
# Create entity RegistryEntry using old unique ID format
old_unique_id = f"{client.driver.controller.home_id}.52-49-00-Air temperature-00"
old_unique_id = f"{client.driver.controller.home_id}.52.52-49-00-Air temperature-00"
entity_entry = ent_reg.async_get_or_create(
"sensor",
DOMAIN,
@@ -155,6 +157,73 @@ async def test_unique_id_migration(hass, multisensor_6_state, client, integratio
assert entity_entry.unique_id == new_unique_id
async def test_unique_id_migration_v2(hass, multisensor_6_state, client, integration):
"""Test unique ID is migrated from old format to new (version 2)."""
ent_reg = entity_registry.async_get(hass)
# Migrate version 2
ILLUMINANCE_SENSOR = "sensor.multisensor_6_illuminance"
entity_name = ILLUMINANCE_SENSOR.split(".")[1]
# Create entity RegistryEntry using old unique ID format
old_unique_id = f"{client.driver.controller.home_id}.52.52-49-0-Illuminance-00-00"
entity_entry = ent_reg.async_get_or_create(
"sensor",
DOMAIN,
old_unique_id,
suggested_object_id=entity_name,
config_entry=integration,
original_name=entity_name,
)
assert entity_entry.entity_id == ILLUMINANCE_SENSOR
assert entity_entry.unique_id == old_unique_id
# 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(ILLUMINANCE_SENSOR)
new_unique_id = f"{client.driver.controller.home_id}.52-49-0-Illuminance-00-00"
assert entity_entry.unique_id == new_unique_id
async def test_unique_id_migration_notification_binary_sensor(
hass, multisensor_6_state, client, integration
):
"""Test unique ID is migrated from old format to new for a notification binary sensor."""
ent_reg = entity_registry.async_get(hass)
entity_name = NOTIFICATION_MOTION_BINARY_SENSOR.split(".")[1]
# Create entity RegistryEntry using old unique ID format
old_unique_id = f"{client.driver.controller.home_id}.52.52-113-00-Home Security-Motion sensor status.8"
entity_entry = ent_reg.async_get_or_create(
"binary_sensor",
DOMAIN,
old_unique_id,
suggested_object_id=entity_name,
config_entry=integration,
original_name=entity_name,
)
assert entity_entry.entity_id == NOTIFICATION_MOTION_BINARY_SENSOR
assert entity_entry.unique_id == old_unique_id
# 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(NOTIFICATION_MOTION_BINARY_SENSOR)
new_unique_id = f"{client.driver.controller.home_id}.52-113-0-Home Security-Motion sensor status-Motion sensor status.8"
assert entity_entry.unique_id == new_unique_id
async def test_on_node_added_not_ready(
hass, multisensor_6_state, client, integration, device_registry
):