Fix Airthings ble migration (#100362)
* Import Platform for tests * Migration bugfix * Store new unique id as a variable in tests * Add comments to testspull/100372/head
parent
8a8f1aff83
commit
b858658516
|
@ -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
|
not matching_reg_entry or "(" not in entry.unique_id
|
||||||
):
|
):
|
||||||
matching_reg_entry = entry
|
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
|
return
|
||||||
entity_id = matching_reg_entry.entity_id
|
entity_id = matching_reg_entry.entity_id
|
||||||
ent_reg.async_update_entity(entity_id=entity_id, new_unique_id=new_unique_id)
|
ent_reg.async_update_entity(entity_id=entity_id, new_unique_id=new_unique_id)
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.airthings_ble.const import DOMAIN
|
from homeassistant.components.airthings_ble.const import DOMAIN
|
||||||
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from tests.components.airthings_ble import (
|
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 entry is not None
|
||||||
assert device 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)
|
entity_registry = hass.helpers.entity_registry.async_get(hass)
|
||||||
|
|
||||||
sensor = entity_registry.async_get_or_create(
|
sensor = entity_registry.async_get_or_create(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
platform="sensor",
|
platform=Platform.SENSOR,
|
||||||
unique_id=TEMPERATURE_V1.unique_id,
|
unique_id=TEMPERATURE_V1.unique_id,
|
||||||
config_entry=entry,
|
config_entry=entry,
|
||||||
device_id=device.id,
|
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 len(hass.states.async_all()) > 0
|
||||||
|
|
||||||
assert (
|
assert entity_registry.async_get(sensor.entity_id).unique_id == new_unique_id
|
||||||
entity_registry.async_get(sensor.entity_id).unique_id
|
|
||||||
== WAVE_DEVICE_INFO.address + "_temperature"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_migration_from_v2_to_v3_unique_id(hass: HomeAssistant):
|
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(
|
sensor = entity_registry.async_get_or_create(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
platform="sensor",
|
platform=Platform.SENSOR,
|
||||||
unique_id=HUMIDITY_V2.unique_id,
|
unique_id=HUMIDITY_V2.unique_id,
|
||||||
config_entry=entry,
|
config_entry=entry,
|
||||||
device_id=device.id,
|
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 len(hass.states.async_all()) > 0
|
||||||
|
|
||||||
assert (
|
# Migration should happen, v2 unique id should be updated to the new format
|
||||||
entity_registry.async_get(sensor.entity_id).unique_id
|
new_unique_id = f"{WAVE_DEVICE_INFO.address}_humidity"
|
||||||
== 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):
|
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(
|
v2 = entity_registry.async_get_or_create(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
platform="sensor",
|
platform=Platform.SENSOR,
|
||||||
unique_id=CO2_V2.unique_id,
|
unique_id=CO2_V2.unique_id,
|
||||||
config_entry=entry,
|
config_entry=entry,
|
||||||
device_id=device.id,
|
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(
|
v1 = entity_registry.async_get_or_create(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
platform="sensor",
|
platform=Platform.SENSOR,
|
||||||
unique_id=CO2_V1.unique_id,
|
unique_id=CO2_V1.unique_id,
|
||||||
config_entry=entry,
|
config_entry=entry,
|
||||||
device_id=device.id,
|
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 len(hass.states.async_all()) > 0
|
||||||
|
|
||||||
assert (
|
# Migration should happen, v1 unique id should be updated to the new format
|
||||||
entity_registry.async_get(v1.entity_id).unique_id
|
new_unique_id = f"{WAVE_DEVICE_INFO.address}_co2"
|
||||||
== 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
|
||||||
assert entity_registry.async_get(v2.entity_id).unique_id == v2.unique_id
|
|
||||||
|
|
||||||
|
|
||||||
async def test_migration_with_all_unique_ids(hass: HomeAssistant):
|
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(
|
v1 = entity_registry.async_get_or_create(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
platform="sensor",
|
platform=Platform.SENSOR,
|
||||||
unique_id=VOC_V1.unique_id,
|
unique_id=VOC_V1.unique_id,
|
||||||
config_entry=entry,
|
config_entry=entry,
|
||||||
device_id=device.id,
|
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(
|
v2 = entity_registry.async_get_or_create(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
platform="sensor",
|
platform=Platform.SENSOR,
|
||||||
unique_id=VOC_V2.unique_id,
|
unique_id=VOC_V2.unique_id,
|
||||||
config_entry=entry,
|
config_entry=entry,
|
||||||
device_id=device.id,
|
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(
|
v3 = entity_registry.async_get_or_create(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
platform="sensor",
|
platform=Platform.SENSOR,
|
||||||
unique_id=VOC_V3.unique_id,
|
unique_id=VOC_V3.unique_id,
|
||||||
config_entry=entry,
|
config_entry=entry,
|
||||||
device_id=device.id,
|
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 len(hass.states.async_all()) > 0
|
||||||
|
|
||||||
assert entity_registry.async_get(v1.entity_id).unique_id == v1.unique_id
|
# No migration should happen, unique id should be the same as before
|
||||||
assert entity_registry.async_get(v2.entity_id).unique_id == v2.unique_id
|
assert entity_registry.async_get(v1.entity_id).unique_id == VOC_V1.unique_id
|
||||||
assert entity_registry.async_get(v3.entity_id).unique_id == v3.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
|
||||||
|
|
Loading…
Reference in New Issue