Fix zwave_js migration logic (#52070)

* Fix zwave_js migration logic

* revert change to move tests to new module

* Update tests/components/zwave_js/test_init.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Raman Gupta 2021-06-21 16:45:47 -04:00 committed by GitHub
parent 077131df1a
commit 8a9a141f3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View file

@ -84,7 +84,11 @@ def async_migrate_old_entity(
if entry.domain != platform or entry.unique_id in registered_unique_ids:
continue
old_ent_value_id = ValueID.from_unique_id(entry.unique_id)
try:
old_ent_value_id = ValueID.from_unique_id(entry.unique_id)
# Skip non value ID based unique ID's (e.g. node status sensor)
except IndexError:
continue
if value_id.is_same_value_different_endpoints(old_ent_value_id):
existing_entity_entries.append(entry)

View file

@ -369,6 +369,47 @@ async def test_old_entity_migration(
assert ent_reg.async_get_entity_id("sensor", DOMAIN, old_unique_id) is None
async def test_different_endpoint_migration_status_sensor(
hass, hank_binary_switch_state, client, integration
):
"""Test that the different endpoint migration logic skips over the status sensor."""
node = Node(client, hank_binary_switch_state)
ent_reg = er.async_get(hass)
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_or_create(
config_entry_id=integration.entry_id, identifiers={get_device_id(client, node)}
)
SENSOR_NAME = "sensor.smart_plug_with_two_usb_ports_status_sensor"
entity_name = SENSOR_NAME.split(".")[1]
# Create entity RegistryEntry using fake endpoint
old_unique_id = f"{client.driver.controller.home_id}.32.node_status"
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,
device_id=device.id,
)
assert entity_entry.entity_id == SENSOR_NAME
assert entity_entry.unique_id == old_unique_id
# Do this twice to make sure re-interview doesn't do anything weird
for i in range(0, 2):
# Add a ready node, unique ID should be migrated
event = {"node": node}
client.driver.controller.emit("node added", event)
await hass.async_block_till_done()
# Check that the RegistryEntry is using the same unique ID
entity_entry = ent_reg.async_get(SENSOR_NAME)
assert entity_entry.unique_id == old_unique_id
async def test_skip_old_entity_migration_for_multiple(
hass, hank_binary_switch_state, client, integration
):