From b85865851636e84af82a2f0e69163902878ace36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A5le=20Stor=C3=B8=20Hauknes?= Date: Thu, 14 Sep 2023 12:51:06 +0200 Subject: [PATCH] Fix Airthings ble migration (#100362) * Import Platform for tests * Migration bugfix * Store new unique id as a variable in tests * Add comments to tests --- .../components/airthings_ble/sensor.py | 3 +- tests/components/airthings_ble/test_sensor.py | 45 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/airthings_ble/sensor.py b/homeassistant/components/airthings_ble/sensor.py index b66d6b8f810..28b5fa3a7a6 100644 --- a/homeassistant/components/airthings_ble/sensor.py +++ b/homeassistant/components/airthings_ble/sensor.py @@ -144,7 +144,8 @@ def async_migrate(hass: HomeAssistant, address: str, sensor_name: str) -> None: not matching_reg_entry or "(" not in entry.unique_id ): matching_reg_entry = entry - if not matching_reg_entry: + if not matching_reg_entry or matching_reg_entry.unique_id == new_unique_id: + # Already has the newest unique id format return entity_id = matching_reg_entry.entity_id ent_reg.async_update_entity(entity_id=entity_id, new_unique_id=new_unique_id) diff --git a/tests/components/airthings_ble/test_sensor.py b/tests/components/airthings_ble/test_sensor.py index 68efd4d25f6..1bf036b735d 100644 --- a/tests/components/airthings_ble/test_sensor.py +++ b/tests/components/airthings_ble/test_sensor.py @@ -2,6 +2,7 @@ import logging from homeassistant.components.airthings_ble.const import DOMAIN +from homeassistant.const import Platform from homeassistant.core import HomeAssistant from tests.components.airthings_ble import ( @@ -31,11 +32,13 @@ async def test_migration_from_v1_to_v3_unique_id(hass: HomeAssistant): assert entry is not None assert device is not None + new_unique_id = f"{WAVE_DEVICE_INFO.address}_temperature" + entity_registry = hass.helpers.entity_registry.async_get(hass) sensor = entity_registry.async_get_or_create( domain=DOMAIN, - platform="sensor", + platform=Platform.SENSOR, unique_id=TEMPERATURE_V1.unique_id, config_entry=entry, device_id=device.id, @@ -57,10 +60,7 @@ async def test_migration_from_v1_to_v3_unique_id(hass: HomeAssistant): assert len(hass.states.async_all()) > 0 - assert ( - entity_registry.async_get(sensor.entity_id).unique_id - == WAVE_DEVICE_INFO.address + "_temperature" - ) + assert entity_registry.async_get(sensor.entity_id).unique_id == new_unique_id async def test_migration_from_v2_to_v3_unique_id(hass: HomeAssistant): @@ -77,7 +77,7 @@ async def test_migration_from_v2_to_v3_unique_id(hass: HomeAssistant): sensor = entity_registry.async_get_or_create( domain=DOMAIN, - platform="sensor", + platform=Platform.SENSOR, unique_id=HUMIDITY_V2.unique_id, config_entry=entry, device_id=device.id, @@ -99,10 +99,9 @@ async def test_migration_from_v2_to_v3_unique_id(hass: HomeAssistant): assert len(hass.states.async_all()) > 0 - assert ( - entity_registry.async_get(sensor.entity_id).unique_id - == WAVE_DEVICE_INFO.address + "_humidity" - ) + # Migration should happen, v2 unique id should be updated to the new format + new_unique_id = f"{WAVE_DEVICE_INFO.address}_humidity" + assert entity_registry.async_get(sensor.entity_id).unique_id == new_unique_id async def test_migration_from_v1_and_v2_to_v3_unique_id(hass: HomeAssistant): @@ -119,7 +118,7 @@ async def test_migration_from_v1_and_v2_to_v3_unique_id(hass: HomeAssistant): v2 = entity_registry.async_get_or_create( domain=DOMAIN, - platform="sensor", + platform=Platform.SENSOR, unique_id=CO2_V2.unique_id, config_entry=entry, device_id=device.id, @@ -127,7 +126,7 @@ async def test_migration_from_v1_and_v2_to_v3_unique_id(hass: HomeAssistant): v1 = entity_registry.async_get_or_create( domain=DOMAIN, - platform="sensor", + platform=Platform.SENSOR, unique_id=CO2_V1.unique_id, config_entry=entry, device_id=device.id, @@ -149,11 +148,10 @@ async def test_migration_from_v1_and_v2_to_v3_unique_id(hass: HomeAssistant): assert len(hass.states.async_all()) > 0 - assert ( - entity_registry.async_get(v1.entity_id).unique_id - == WAVE_DEVICE_INFO.address + "_co2" - ) - assert entity_registry.async_get(v2.entity_id).unique_id == v2.unique_id + # Migration should happen, v1 unique id should be updated to the new format + new_unique_id = f"{WAVE_DEVICE_INFO.address}_co2" + assert entity_registry.async_get(v1.entity_id).unique_id == new_unique_id + assert entity_registry.async_get(v2.entity_id).unique_id == CO2_V2.unique_id async def test_migration_with_all_unique_ids(hass: HomeAssistant): @@ -170,7 +168,7 @@ async def test_migration_with_all_unique_ids(hass: HomeAssistant): v1 = entity_registry.async_get_or_create( domain=DOMAIN, - platform="sensor", + platform=Platform.SENSOR, unique_id=VOC_V1.unique_id, config_entry=entry, device_id=device.id, @@ -178,7 +176,7 @@ async def test_migration_with_all_unique_ids(hass: HomeAssistant): v2 = entity_registry.async_get_or_create( domain=DOMAIN, - platform="sensor", + platform=Platform.SENSOR, unique_id=VOC_V2.unique_id, config_entry=entry, device_id=device.id, @@ -186,7 +184,7 @@ async def test_migration_with_all_unique_ids(hass: HomeAssistant): v3 = entity_registry.async_get_or_create( domain=DOMAIN, - platform="sensor", + platform=Platform.SENSOR, unique_id=VOC_V3.unique_id, config_entry=entry, device_id=device.id, @@ -208,6 +206,7 @@ async def test_migration_with_all_unique_ids(hass: HomeAssistant): assert len(hass.states.async_all()) > 0 - assert entity_registry.async_get(v1.entity_id).unique_id == v1.unique_id - assert entity_registry.async_get(v2.entity_id).unique_id == v2.unique_id - assert entity_registry.async_get(v3.entity_id).unique_id == v3.unique_id + # No migration should happen, unique id should be the same as before + assert entity_registry.async_get(v1.entity_id).unique_id == VOC_V1.unique_id + assert entity_registry.async_get(v2.entity_id).unique_id == VOC_V2.unique_id + assert entity_registry.async_get(v3.entity_id).unique_id == VOC_V3.unique_id