Remove airthings_ble unique_id migration from 2023.9 (#184449)

This commit is contained in:
Ståle Storø Hauknes
2026-10-06 14:18:51 +02:00
committed by GitHub
parent f73d54ce85
commit ecb80e9e2f
3 changed files with 2 additions and 297 deletions
@@ -16,25 +16,18 @@ from homeassistant.components.sensor import (
from homeassistant.const import (
LIGHT_LUX,
EntityCategory,
Platform,
UnitOfPressure,
UnitOfRadiationConcentration,
UnitOfRatio,
UnitOfSoundPressure,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import CONNECTION_BLUETOOTH, DeviceInfo
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.entity_registry import (
RegistryEntry,
async_entries_for_device,
)
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import AirthingsBLEConfigEntry, AirthingsBLEDataUpdateCoordinator
_LOGGER = logging.getLogger(__name__)
@@ -168,43 +161,6 @@ SENSORS_MAPPING_TEMPLATE: dict[str, AirthingsBLESensorEntityDescription] = {
PARALLEL_UPDATES = 0
@callback
def async_migrate(
hass: HomeAssistant, entry_id: str, address: str, sensor_name: str
) -> None:
"""Migrate entities to new unique ids (with BLE Address)."""
ent_reg = er.async_get(hass)
unique_id_trailer = f"_{sensor_name}"
new_unique_id = f"{address}{unique_id_trailer}"
if ent_reg.async_get_entity_id(DOMAIN, Platform.SENSOR, new_unique_id):
# New unique id already exists
return
dev_reg = dr.async_get(hass)
if not (
device := dev_reg.async_get_device_by_connection(
(CONNECTION_BLUETOOTH, address), entry_id
)
):
return
entities = async_entries_for_device(
ent_reg,
device_id=device.id,
include_disabled_entities=True,
)
matching_reg_entry: RegistryEntry | None = None
for entry in entities:
if entry.unique_id.endswith(unique_id_trailer) and (
not matching_reg_entry or "(" not in entry.unique_id
):
matching_reg_entry = 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)
_LOGGER.debug("Migrated entity '%s' to unique id '%s'", entity_id, new_unique_id)
async def async_setup_entry(
hass: HomeAssistant,
entry: AirthingsBLEConfigEntry,
@@ -223,7 +179,6 @@ async def async_setup_entry(
sensor_value,
)
continue
async_migrate(hass, entry.entry_id, coordinator.data.address, sensor_type)
entities.append(
AirthingsSensor(
coordinator, coordinator.data, SENSORS_MAPPING_TEMPLATE[sensor_type]
+1 -44
View File
@@ -19,7 +19,7 @@ from homeassistant.helpers.device_registry import (
DeviceRegistry,
)
from tests.common import MockConfigEntry, MockEntity
from tests.common import MockConfigEntry
from tests.components.bluetooth import generate_advertisement_data, generate_ble_device
@@ -57,14 +57,6 @@ def patch_airthings_ble(return_value=AirthingsDevice, side_effect=None):
)
def patch_airthings_device_update():
"""Patch airthings-ble device."""
return patch(
"homeassistant.components.airthings_ble.coordinator.AirthingsBluetoothDeviceData.update_device",
return_value=WAVE_DEVICE_INFO,
)
WAVE_SERVICE_INFO = BluetoothServiceInfoBleak(
name="cc-cc-cc-cc-cc-cc",
address="cc:cc:cc:cc:cc:cc",
@@ -303,41 +295,6 @@ CORENTIUM_HOME_2_DEVICE_INFO = AirthingsDevice(
address="cc:cc:cc:cc:cc:cc",
)
TEMPERATURE_V1 = MockEntity(
unique_id="Airthings Wave Plus 123456_temperature",
name="Airthings Wave Plus 123456 Temperature",
)
HUMIDITY_V2 = MockEntity(
unique_id="Airthings Wave Plus (123456)_humidity",
name="Airthings Wave Plus (123456) Humidity",
)
CO2_V1 = MockEntity(
unique_id="Airthings Wave Plus 123456_co2",
name="Airthings Wave Plus 123456 CO2",
)
CO2_V2 = MockEntity(
unique_id="Airthings Wave Plus (123456)_co2",
name="Airthings Wave Plus (123456) CO2",
)
VOC_V1 = MockEntity(
unique_id="Airthings Wave Plus 123456_voc",
name="Airthings Wave Plus 123456 CO2",
)
VOC_V2 = MockEntity(
unique_id="Airthings Wave Plus (123456)_voc",
name="Airthings Wave Plus (123456) VOC",
)
VOC_V3 = MockEntity(
unique_id="cc:cc:cc:cc:cc:cc_voc",
name="Airthings Wave Plus (123456) VOC",
)
def create_entry(
hass: HomeAssistant,
@@ -19,15 +19,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from . import (
CO2_V1,
CO2_V2,
CORENTIUM_HOME_2_DEVICE_INFO,
CORENTIUM_HOME_2_SERVICE_INFO,
HUMIDITY_V2,
TEMPERATURE_V1,
VOC_V1,
VOC_V2,
VOC_V3,
WAVE_DEVICE_INFO,
WAVE_ENHANCE_DEVICE_INFO,
WAVE_ENHANCE_SERVICE_INFO,
@@ -37,7 +30,6 @@ from . import (
create_device,
create_entry,
patch_airthings_ble,
patch_airthings_device_update,
patch_async_ble_device_from_address,
patch_async_discovered_service_info,
)
@@ -48,205 +40,6 @@ from tests.components.bluetooth import inject_bluetooth_service_info
_LOGGER = logging.getLogger(__name__)
async def test_migration_from_v1_to_v3_unique_id(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Verify migration from v1 (pre 2023.9.0).
Migrates to the latest unique id format.
"""
entry = create_entry(hass, WAVE_SERVICE_INFO, WAVE_DEVICE_INFO)
device = create_device(entry, device_registry, WAVE_SERVICE_INFO, WAVE_DEVICE_INFO)
assert entry is not None
assert device is not None
new_unique_id = f"{WAVE_DEVICE_INFO.address}_temperature"
sensor = entity_registry.async_get_or_create(
domain=DOMAIN,
platform=Platform.SENSOR,
unique_id=TEMPERATURE_V1.unique_id,
config_entry=entry,
device_id=device.id,
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 0
inject_bluetooth_service_info(
hass,
WAVE_SERVICE_INFO,
)
await hass.async_block_till_done()
with patch_airthings_device_update():
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert len(hass.states.async_all()) > 0
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,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Verify migration from v2 (introduced in 2023.9.0).
Migrates to the latest unique id format.
"""
entry = create_entry(hass, WAVE_SERVICE_INFO, WAVE_DEVICE_INFO)
device = create_device(entry, device_registry, WAVE_SERVICE_INFO, WAVE_DEVICE_INFO)
assert entry is not None
assert device is not None
sensor = entity_registry.async_get_or_create(
domain=DOMAIN,
platform=Platform.SENSOR,
unique_id=HUMIDITY_V2.unique_id,
config_entry=entry,
device_id=device.id,
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 0
inject_bluetooth_service_info(
hass,
WAVE_SERVICE_INFO,
)
await hass.async_block_till_done()
with patch_airthings_device_update():
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert len(hass.states.async_all()) > 0
# 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,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test migration with both v1 and v2 unique ids.
v1 is pre 2023.9.0, v2 introduced in 2023.9.0.
"""
entry = create_entry(hass, WAVE_SERVICE_INFO, WAVE_DEVICE_INFO)
device = create_device(entry, device_registry, WAVE_SERVICE_INFO, WAVE_DEVICE_INFO)
assert entry is not None
assert device is not None
v2 = entity_registry.async_get_or_create(
domain=DOMAIN,
platform=Platform.SENSOR,
unique_id=CO2_V2.unique_id,
config_entry=entry,
device_id=device.id,
)
v1 = entity_registry.async_get_or_create(
domain=DOMAIN,
platform=Platform.SENSOR,
unique_id=CO2_V1.unique_id,
config_entry=entry,
device_id=device.id,
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 0
inject_bluetooth_service_info(
hass,
WAVE_SERVICE_INFO,
)
await hass.async_block_till_done()
with patch_airthings_device_update():
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert len(hass.states.async_all()) > 0
# 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,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test if migration works when we have all unique ids."""
entry = create_entry(hass, WAVE_SERVICE_INFO, WAVE_DEVICE_INFO)
device = create_device(entry, device_registry, WAVE_SERVICE_INFO, WAVE_DEVICE_INFO)
assert entry is not None
assert device is not None
v1 = entity_registry.async_get_or_create(
domain=DOMAIN,
platform=Platform.SENSOR,
unique_id=VOC_V1.unique_id,
config_entry=entry,
device_id=device.id,
)
v2 = entity_registry.async_get_or_create(
domain=DOMAIN,
platform=Platform.SENSOR,
unique_id=VOC_V2.unique_id,
config_entry=entry,
device_id=device.id,
)
v3 = entity_registry.async_get_or_create(
domain=DOMAIN,
platform=Platform.SENSOR,
unique_id=VOC_V3.unique_id,
config_entry=entry,
device_id=device.id,
)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 0
inject_bluetooth_service_info(
hass,
WAVE_SERVICE_INFO,
)
await hass.async_block_till_done()
with patch_airthings_device_update():
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert len(hass.states.async_all()) > 0
# 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
@pytest.mark.parametrize(
("unique_suffix", "expected_sensor_name"),
[